* converters: allow wrapping & takes_self
* Add takes field
* Add news fragment
* Refactor name and call creation
* Make argument names more idiomatic
* Add missing hints
* Make 3-arg converters zero-overhead at runtime
* Remove unnecessary changes
* More idiomatic name
* Explain method
* Make pickle test more meaningful
* Add unit tests for fmt_converter_call
* Check our eq works too
* Convert Converter docstring to Napoleon
* Fix rebase fubar
* Add ~types~
* Comment out failing mypy tests for now
* Fix mypy tests
* Add warning
* Add narrative docs
* Fix converter overloads, add tests
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---------
Co-authored-by: Tin Tvrtković <tinchester@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Speed up __eq__
* Changelog, disallow nans in tests
* Fix `eq_key` comparisons
* Rework changlelog
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Make docs more precise
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Implement attrs.validators.or_ validator
* Add tests/test_validators.py::TestOr test cases
* Add test for _OrValidator.__repr__ method
* Add description of attrs.validators.or_ to docs/api.rst
* Add changelog entry
* Swap double quotes for single because doctests don't like it
* Rename changelog fragment pointing to incorrect number
* Silence ruff linter warnings
Although good warning in general, in this particular code, they
do not fit here.
* BLE001 Do not catch blind exception: `Exception`
`validators` usually raise `ValueError` upon their violation.
However, it's not the only `Exception` they can raise - cf.
`attrs.validators.not_` - therefore, it is desirable to catch
them all!
* PERF203 `try`-`except` within a loop incurs performance overhead
Fair point, but the loop is written in a way to short-circuit,
ie. it will finish when a first validator is satisfied.
* S112 `try`-`except`-`continue` detected, consider logging the exception
Not applicable here, we care only if **all** validators raise
an exception, which is already accomodated for after the `for`
loop.
* Apply suggestions from code review
Co-authored-by: Hynek Schlawack <hs@ox.cx>
* Rework example of or_ validator in api/docs.rst
* Update docs/api.rst
---------
Co-authored-by: Hynek Schlawack <hs@ox.cx>
Co-authored-by: Libor <libas.martinek@protonmail.com>
* Preserve AttributeError in slotted classes with cached_property
In slotted classes' generated __getattr__(), we try __getattribute__()
before __getattr__(), if available, and eventually let AttributeError
propagate. This matches better with the behaviour described in Python's
documentation "Customizing attribute access":
https://docs.python.org/3/reference/datamodel.html#customizing-attribute-access
Fix https://github.com/python-attrs/attrs/issues/1230
* Update changelog.d/1253.change.md
---------
Co-authored-by: Hynek Schlawack <hs@ox.cx>
* Tweak stubs for better VS Code import
* Modernize the rest of the stubs
* Fix dataclass_transform import
* Changelog
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update changelog.d/1234.change.md
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Hynek Schlawack <hs@ox.cx>
* Remove undesirable original class reference when using slotted cached_property
* Update closure cells for slotted cached property functions
* Changelog
* fixup py 3.7 test
* Add support for cached_properties to slotted attrs classes.
* Remove locking from implementation
* Add test for multiple cached properties and fix bug
* Add changelog file
* Document slotted cached properties
* Add cached_property hypothesis check.
* Only run cached_property imports on python 3.8+
* Use cached _obj_setattr instead of `object.__setattr__`
* Correctly resolve mro for __getattr__ in cached properties
* Use _get_annotations rather than branching on class dict entry
* Optimise __getattr__ code by front loading branching, and injecting locasl variables
* Remove unnecessary `__attrs_original_getattr__` from class dictionary.
---------
Co-authored-by: Hynek Schlawack <hs@ox.cx>
* Pass args from `__init__` to `__attrs_pre_init__` if requested
Detect if `__attrs_pre_init__` has arguments besides `self`
using `inspect.signature`. If so, pass `__attrs_pre_init__`
the same arguments that `__init__` (or `__attrs_init__`)
expects.
* Add changelog entry for `__attrs_pre_init__` args changes
* Don't use monkeypatch in new code
* Clarify docs
* Missed one monkeypatching
---------
Co-authored-by: Hynek Schlawack <hs@ox.cx>
* Fix TypeError for asdict with namedtuples and retain_collection_types=True
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add news fragment in `changelog.d`
* fix pre-commit interrogate checker
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix flake8 issue
* also fixed `astuple`
* Add `_is_namedtuple` function
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix SyntaxError for python 3.7
* use `issubclass(..., tuple)`
* use issubclass(cf, tuple) if case of TypeError
* pragma: no cover
* Get rid of the `# no cover`
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* simplify a bit
* Update tests/test_funcs.py
* Update tests/test_funcs.py
* Update tests/test_funcs.py
* Update tests/test_funcs.py
* Update tests/test_funcs.py
* Update tests/test_funcs.py
* Update tests/test_funcs.py
* Update tests/test_funcs.py
* Update changelog.d/1165.change.md
* Escape patterns
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Hynek Schlawack <hs@ox.cx>
* Add literal string support to includer and exclude filters
* Add docs and changelog for new feature
* Fix typo in `typing_example`
* Add a note to document typo issues while using literal name strings as filter args
* Add more docs
* Add code mark for `AttributeError`
* Fix grammar error and upgrade `versionchanged` info
* Improve docs and examples from
hynek's comments
* Keep example cases the same
* More examples
* Apply suggestions from code review
Co-authored-by: chrysle <fritzihab@posteo.de>
---------
Co-authored-by: Hynek Schlawack <hs@ox.cx>
Co-authored-by: chrysle <fritzihab@posteo.de>
* Raise a deprecation warning when evolve receives insta as a kw arg
Fixes#1109
* Add news fragment
* Raise a better error
* Handle too many pos args
* Lazy import
* Trim traceback
* Add test evolving a field named inst
* Spelling
* Spin positively
* Added `type` parameter to `attrs.field` and test
* Added notice in examples.md
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Added changelog entry
* Fixed docs
* Apply suggestions from code review
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Hynek Schlawack <hs@ox.cx>
* compat: move check for closure cellvar update wrapper function
This sanity checking code ended up getting decoupled from the old
implementation in a18b3957d5 when a new,
much simpler method was added. This check can fail spuriously in
certain environments where it doesn't even need to be run because the
newer approach is used.
This is noticeable when using python scripts compiled by nuitka. Since
next-gen attrs uses slots on everything by default, the use of this
fixup function is now pervasive, as well.
* changelog: note fix
* Semantic newlines
---------
Co-authored-by: Hynek Schlawack <hs@ox.cx>
* Remove unnecessary parameterization of tests
* Fix backward compatibility with pickles before v22.2.0
Unfortunately we're using `django-redis` to cache some `attrs` instances
and the change to make `__getstate__` and `__setstate__` use `dict`
values instead of `tuple` values prevents reading any values from the
cache when updating to `attrs>=22.2.0`. We get an `AttributeError`
raised for all attributes.
This happens because the tuple of values stored in the original pickle
is checked for the name of the attribute and so none of the attributes
are set on unpickle.
To address this I've made a change so that `__setstate__` will still be
able to handle values pickled as `tuple`s allowing existing pickles to
be unpickled.
The test just contains a pre-pickled value due to the complexities
around tying to mock methods on a slotted as documented[^1].
I realise that documentation also mentions that we should "think twice"
before using pickle, but sometimes those decisions predate our
involvement in a project! In addition, pretty much all of Django's cache
implementations are built around using pickle by default. This change
will allow an easier path to upgrade by unpickling and repickling all
values in the cache.
Regression in 89a890a326.
[^1]: https://www.attrs.org/en/latest/glossary.html#term-slotted-classes
* Update 1085.change.md
Co-authored-by: Hynek Schlawack <hs@ox.cx>
* Add AttrsInstance protocol to forgotten attrs.asdict and attrs.astuple
* Add mypy test for attrs.asdict
* Add changelog entry for attrs.asdict
* Clarify it's about type stubs
Co-authored-by: Hynek Schlawack <hs@ox.cx>
* Switch changelog to markdown
* Add changelog except back to long description
* Fix MANIFEST.in
* Re-add towncrier draft entries
* Write the title outselves
* Add warning box
* Make changelog format more KeepAChangelog-ish
* Link latest changes to main tree
* Move link into warning
* KAC has no empty lines between entries
* There's no more .rst in the root dir
* Add unsafe_hash alias for class-wide hash
Fixes#1003
* Add news fragment
* Add type hints / type examples
* Complete attr.s's api string in api.rst
* Address feedback
* Clarify
* Shuffle around
* Spike `alias` implementation.
* Move default alias init to after field_transformer.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fixup docs.
* Update docs/extending.rst
* Pre-commit fixes
* Partially fix doctest
* Add test docstrings.
* Add typing_example tests
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Tidy typing_example
* Add note in init.rst on private aliases
* Add alias example to examples.rst
* Assert to comment
* Add changelog entry
* Fixup doc error
* Tidy dataclass_transform docs
* Lil' spice for the changelog.
* Fix doctest
* Update extending.rst
* Make alias introspection more explicit
* Update src/attr/_make.py
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Hynek Schlawack <hs@ox.cx>
* Use enum for typing NOTHING.
This will allow those extending attrs
to type NOTHING as Literal[NOTHING].
* Make test_dunders test `NOTHING` directly
* Reflect NOTHING enum typing in __init__.pyi
* Fix docs for NOTHING
* Add changelog entry
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
Co-authored-by: Hynek Schlawack <hs@ox.cx>
Co-authored-by: Tin Tvrtković <tinchester@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>