Commit Graph

266 Commits

Author SHA1 Message Date
Hynek Schlawack f5683b852b
converters: allow wrapping and passing self and fields (#1267)
* 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>
2024-07-28 13:55:24 +00:00
Tin Tvrtković a8808a2b3e
Speed up __eq__ (#1310)
* 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>
2024-07-21 09:53:35 +00:00
Libor Martínek 7c9b31ea7b
Add `attrs.validators.or_` validator (#1303)
* 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>
2024-07-17 12:01:41 +00:00
Stefan Scherfke edcaf04a4d
make_class(): Add "__annotations_" to generated class (#1285)
* make_class(): Add "__annotations_" to generated class

Fixes: #1271

* Fix PR#

* Flip

---------

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2024-07-13 10:52:46 +00:00
Hynek Schlawack 5618e6fb8d
Add missing empty lines to changelog btw releases 2024-05-27 20:11:05 +02:00
Denis Laxalde 88e2896ca9
Preserve AttributeError in slotted classes with cached_property (#1253)
* 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>
2024-04-02 04:57:25 +00:00
Hynek Schlawack 2446e640ca
Remove non-version metadata hack (#1268)
* Remove non-version metadata hack

As per our deprecation policy.

* Add news fragment

* It’s a pkg
2024-03-18 15:19:38 +01:00
Hynek Schlawack 8460190b29
Remove attrs.validators.provides (#1265)
* Remove attrs.validators.provides

* Add news fragment
2024-03-16 21:15:47 +00:00
Hynek Schlawack 278d3a5bc6
evolve: remove support for passing instances per kw (#1264)
As per our deprecation policy.
2024-03-16 18:23:17 +00:00
Hynek Schlawack d9ed5937fe
Deprecate attr.s's repr_ns (#1263)
* Deprecate attr.s's repr_ns

It's pointless in Python 3. define et al never had it.

* Add news fragment

* Fix typo
2024-03-16 15:55:37 +00:00
Tin Tvrtković 06dc279098
Tweak stubs for better VS Code import (#1234)
* 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>
2024-02-04 06:00:30 +00:00
Hynek Schlawack 6bdc41ce0b
Update 1221.change.md 2024-01-07 08:13:27 +01:00
diabolo-dan 26d8dd7957
Slotted cached property reference (#1221)
* Remove undesirable original class reference when using slotted cached_property

* Update closure cells for slotted cached property functions

* Changelog

* fixup py 3.7 test
2024-01-04 10:52:49 +00:00
Hynek Schlawack 9e443b1852
Prepare 23.2.0 2023-12-31 07:20:03 +01:00
diabolo-dan 597c3a8adb
Add support for cached_properties to slotted attrs classes. (#1200)
* 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>
2023-12-08 18:24:25 +00:00
Aaron Pham 1fcd29f864
Add optional namespace arguments for `make_class` (#1203)
* feat(make_class): add optional namespace arguments to pass through to
`make_class`

Signed-off-by: Aaron <29749331+aarnphm@users.noreply.github.com>

* chore: add changelog

Signed-off-by: Aaron <29749331+aarnphm@users.noreply.github.com>

* Rename `namespaces` to `class_body`

Signed-off-by: Aaron <29749331+aarnphm@users.noreply.github.com>

* Fix versionchanged for correct features

Signed-off-by: Aaron <29749331+aarnphm@users.noreply.github.com>

* Correctly name test case

Signed-off-by: Aaron <29749331+aarnphm@users.noreply.github.com>

* Update changelog

Signed-off-by: Aaron <29749331+aarnphm@users.noreply.github.com>

* Adjust changelog

---------

Signed-off-by: Aaron <29749331+aarnphm@users.noreply.github.com>
Co-authored-by: Hynek Schlawack <hs@ox.cx>
2023-11-28 12:05:46 +00:00
Victor Song c2824ac30f
Pass args from `__init__` to `__attrs_pre_init__` if requested (#1187)
* 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>
2023-09-29 08:47:51 +02:00
Hynek Schlawack c824bbe103
Allow subclassing of AttrsInstance and Protocol at once (#1172) 2023-08-06 21:33:03 +02:00
Vitaliy Yelnik 0bf0678a22
Fix TypeError for `asdict(<class with namedtuple>, retain_collection_types=True)` (#1165)
* 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>
2023-07-29 21:31:36 +02:00
Hynek Schlawack b6cd5a0847
Semantic linefeeds 2023-07-07 14:36:55 +02:00
Sergei Lebedev 36d4762498
Switched from __dataclass_transform__() to typing.dataclass_transform() (#1158) 2023-07-06 18:41:45 +02:00
Hynek Schlawack ecdb117407
Fix type annotation for resolve_types() (#1141) 2023-06-04 11:27:09 +02:00
Hynek Schlawack 747b9ded2f
Simplify CI and use .python-version (#1140)
* Simplify CI and use .python-version

* Test CI on pypy 3.9 too

* We don't need .gitignore to keep the dir anymore
2023-06-04 07:33:48 +02:00
Hynek Schlawack 1e2f6f9cac
Prepare 23.1.0 2023-04-16 12:22:15 +02:00
Hynek Schlawack a7e1a56da6
Fix missing backtick 2023-04-16 12:17:35 +02:00
Lanqing Huang c4c6fdd658
Add literal string support to include and exclude filters (#1068)
* 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>
2023-04-14 08:54:31 +00:00
Hynek Schlawack c8b342d34f
Deprecate zope-interface support (#1120) 2023-04-10 15:34:38 +02:00
Hynek Schlawack 7d55876ed3
Accept tuples in attrs.validators.optional (#1122)
* Accept tuples in attrs.validators.optional

Fixes #937

* Add news fragment
2023-04-10 09:49:51 +02:00
Hynek Schlawack a14e1859b8
Raise a deprecation warning when evolve receives insta as a kw arg (#1117)
* 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
2023-04-05 08:33:02 +02:00
chrysle f04fa4ed8e
CONTRIBUTING: Added instructions for PR workflow (#1105)
* CONTRIBUTING: Added instructions for PR workflow

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Added changelog entry

* Use inline code for changelog filename

Co-authored-by: Sviatoslav Sydorenko <wk.cvs.github@sydorenko.org.ua>

* Add console prompt to command

Co-authored-by: Sviatoslav Sydorenko <wk.cvs.github@sydorenko.org.ua>

* Made some more improvements

* Apply suggestions from code review

* Update .github/CONTRIBUTING.md

* Update changelog.d/1105.change.md

* Update .github/CONTRIBUTING.md

Co-authored-by: Sviatoslav Sydorenko <wk.cvs.github@sydorenko.org.ua>

* Update .github/CONTRIBUTING.md

* Update .github/CONTRIBUTING.md

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sviatoslav Sydorenko <wk.cvs.github@sydorenko.org.ua>
Co-authored-by: Hynek Schlawack <hs@ox.cx>
2023-04-03 16:34:43 +00:00
chrysle 65ee286d6a
Added `type` parameter to `attrs.field()` function (#1107)
* 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>
2023-04-03 14:40:45 +00:00
Tin Tvrtković c7308a66ef
Flesh out resolve_types (#1099)
* Flesh out resolve_types

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add changelog entry

* Fix flake?

* Update 1099.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>
2023-03-18 19:31:15 +01:00
T b950cb8d83
compat: move check for closure cellvar update wrapper function (#1092)
* 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>
2023-02-27 07:07:34 +07:00
Hynek Schlawack 99925dbc2e
Add news fragment for #993 2023-02-06 09:40:43 +01:00
Hynek Schlawack 415147c176
Add news fragment for #1079 2023-02-06 09:36:10 +01:00
Nick Pope c9150d278c
Fix backward compatibility with pickles before v22.2.0 (#1085)
* 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>
2023-01-25 06:58:31 +00:00
Vyacheslav Tverskoy 882805e173
Add AttrsInstance protocol to forgotten attrs.asdict and attrs.astuple (#1090)
* 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>
2023-01-23 09:21:20 +00:00
Bryan Forbes 46053d703d
Fix @frozen typing to work on PEP-681 checkers (#1084) 2023-01-12 07:22:13 +01:00
Julian Berman a71fbbad89
Fix @frozen exceptions to allow __traceback__ to be set. (#1081) 2023-01-08 18:34:28 +01:00
Hynek Schlawack a9960de98d
Prepare 22.2.0 2022-12-21 10:07:30 +01:00
Hynek Schlawack afe211143d
Switch changelog to markdown (#1067)
* 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
2022-12-07 09:15:46 +01:00
Hynek Schlawack 0f6a9b4753
Add unsafe_hash alias for class-wide hash (#1065)
* 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
2022-12-02 07:45:13 +01:00
Hynek Schlawack 67dc8cc261
Improve news fragments 2022-12-01 17:00:20 +01:00
Alex Ford ee3ecb112f
Add Attribute.alias (#950)
* 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>
2022-11-30 14:39:57 +00:00
Hynek Schlawack c20a9dc7b9
Add defaults to cmp_using stubs (#1027)
Fixes #1026
2022-09-19 09:20:55 +02:00
Hynek Schlawack c3078e33d0
Unpin towncrier 2022-09-12 15:18:41 +02:00
Nick Timkovich f870beb84a
Add a not_ validator (#1010) 2022-09-04 13:55:02 +02:00
layday f7fa5a810c
Fix type errors in Pyright (#999) 2022-08-30 15:06:10 +02:00
Kevin M Granger c860e9dd25
Use enum for typing NOTHING (#983)
* 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>
2022-08-27 17:41:56 +02:00
Hynek Schlawack fab5a9289d
Deprecate Python 3.6 (#1017)
* Rename CI workflow to a more meaningful name

* News fragment
2022-08-22 16:43:28 +02:00