Commit Graph

412 Commits

Author SHA1 Message Date
Filipe Brandenburger e21793e90a
Allow converter.optional to take a Converter such as converter.pipe as its argument (#1372)
* Allow converter.optional to take a converter such as converter.pipe as its argument

* Only turn optional into a Converter if needed

* Move call to Converter constructor to the end of optional()

The constructor consumes __annotations__, so move the constructor call to after those have been set on the optional_converter function

* Update tests/test_converters.py

* Update tests/test_converters.py

---------

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2024-11-17 11:15:26 +00:00
Thomas Grainger 1921da6eac
allow __suppress_context__ and __notes__ to be mutated on frozen exceptions (#1365)
* allow __suppress_context__ to be mutated on frozen exceptions

* add changelog

* and __notes__! lets' not forget that.

* add notes to news

* add tests

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

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

* fix improt

* fix test

* fix tests again

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-10-26 11:20:44 +00:00
Hynek Schlawack 2f1014aa60
Black dropped us 5 years ago
31f4105731
2024-09-05 09:02:00 +02:00
Hynek Schlawack a8e24b0186
Drop Python 3.7 (#1340)
* Drop Python 3.7

* Add news fragment

* update Ruff
2024-08-29 17:35:04 +02:00
Hynek Schlawack ae7340966c
Cull Ruff ignores 2024-08-16 07:23:14 +02:00
Hynek Schlawack 5d6d21aa5e
Make Converter (including pipe) correctly infer the return type (#1331)
When running in adapter mode.
2024-08-06 14:01:28 +00:00
Hynek Schlawack 6fda0a4e08
Make Converter a kind of adapter, fix converters.pipe (#1328)
* Make Converter a kind of adapter, fix converters.pipe

* Fix import cycle on 3.7/8

* stray space

* Create static __call__ on Converter instantiation

* Add tests for adapters doing passing correct args

* Add news fragment
2024-08-06 13:49:43 +02:00
Hynek Schlawack 7373d88f9b
Restore basic functionality on 3.14[sic] (#1329)
* Restore basic functionality on 3.14[sic]

Essentially switch to PEP 649 / 749 for annotations. Some tests need to
be skipped for now, but the rest is working.

Fixes #1326

* Add news fragment

* We have not 3.14 CI yet

* Use imprerative xfails instead of skips
2024-08-06 13:37:35 +02:00
Hynek Schlawack f520d9a89f
Only soft-deprecate hash (#1330)
* Only soft-deprecate hash

Given the amount of warnings in the eco-system, make the deprection
soft. We won't remove it.

* news fragment
2024-08-06 13:30:30 +02:00
Hynek Schlawack 6e51cb7c40
Remove missed hashes in tests 2024-08-03 15:41:27 +02:00
Hynek Schlawack bde3f58cfe
Deprecate hash for unsafe_hash (#1323)
* Deprecate hash for unsafe_hash

It's the standard -- what are we gonna do.

* Add deprecation tests

* Add news fragment
2024-08-03 15:19:21 +02:00
Hynek Schlawack dbb25ce347
Add support for __attrs_init_subclass__ (#1321)
* Add support for __attrs_init_subclass__

* Fix test docstring

* Fix import

* Add versionadded

* Invert logic and add example

* Explain behavior in API docs

* Move to narrative docs

* Link

* once is enough

* why hide

* endash

* better phrasing
2024-08-03 13:38:12 +02:00
Hynek Schlawack fd7538f0e2
validators.in_ now transforms certain unhashable options to tuples (#1320)
* validators.in_ now transforms certain unhashable options to tuples

Fixes #1295

* Add news fragment

* type
2024-08-03 13:35:21 +02:00
Hynek Schlawack 09161fc918
Fix crash when __pre_init__, kw_only, and defaults come together (#1319)
* Fix crash when __pre_init__, kw_only, and defaults come together

Fixes #1284

* Add news fragment

* Improve test
2024-08-03 13:31:53 +02:00
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
Hynek Schlawack 1fd3af0b94
Write specific benchmarks for CodSpeed (#1306)
* Write specific benchmarks for CodSpeed

* Add frozen benchmark

* Remove instantiation noise

* Put into separate dir like cattrs

* Be consistent
2024-07-18 11:00:27 +02: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
Hynek Schlawack 9ebd31c894
update ruff 2024-07-15 11:15:17 +02: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 0b07265e0c
Unpin Mypy (#1301)
* Unpin Mypy

fixes #1279

Looks like 1.10.1 fixed itself?

* Fix type errors

* Update pins

* Don't run Mypy on 3.8 too

* wtf

* Fix order
2024-07-13 12:33:59 +02:00
Hynek Schlawack 6e25a0c4f5
Unpin/fix Pyright (#1302)
* Unpin/fix Pyright

Fixes #1278

* Consistency

* Remove stale comment

* Add explanation why we need experimental features
2024-07-13 12:01:09 +02:00
Hynek Schlawack 6f64cd6d19
Cooperate on making CodSpeed work in tox (#1300)
* Cooperate on making CodSpeed work in tox

* Make CodSpeed an own job

* Install tox

* Limit benchmarks

7 Minutes are a bit long

* Still too many
2024-07-11 13:24:51 +02:00
Hynek Schlawack 3bec9e1825
Add Codspeed (#1299)
* Add Codspeed

* Only run benchmarks on 3.12 (for now?)
2024-07-11 12:17:22 +02:00
Hynek Schlawack 11288d5709
Update Ruff 2024-06-28 09:42:30 +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 1967be0eb6
Move factory tests where they belong 2024-03-17 09:15:36 +01:00
Hynek Schlawack 4b1d869e9a
Simplify slot field detection 2024-03-17 07:27:41 +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
Hynek Schlawack f590f44cfd
docs: style improvements 2024-03-16 16:22:58 +01:00
Hynek Schlawack d129b1f756
pre-commit autoupdate 2024-03-16 15:31:54 +01:00
Lumír 'Frenzy' Balhar f9ff9135b4
Fix test_ne in test_cmp.py for Python 3.13 (#1255)
* Fix test_ne in test_cmp.py for Python 3.13

Compiler in Python 3.13+ strips indents from docstrings
so they need to be compared without it for new Pythons.

Fixes: https://github.com/python-attrs/attrs/issues/1228

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

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

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-03-07 09:23:46 +00:00
Denis Laxalde b9084fab02
Remove pytest.deprecated_call() in TestAssoc::test_unknown (#1249)
assoc() no longer raises a deprecation warning since commit
22ae8473fb but the 'with
pytest.deprecated_call():' in that test was not removed then (in
contrast with other test cases).

Maybe this got unnoticed due to a pytest bug?
In any case, using pytest 8+ (and keeping deprecated_call()) shows that
no warning is raised and the test fails.

Removing the upper bound on pytest in dev dependencies as tests now
pass with pytest 8.0.

Fix #1233.

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2024-03-02 06:40:36 +00:00
Hynek Schlawack 75d54c2197
Black 24 2024-01-26 08:15:53 +01:00
Hynek Schlawack 2bddbdb704
Add codespell 2024-01-08 07:37:20 +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
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
Eugene Toder 079954ef40
Remove work-arounds for setting cell_contents (#1201)
* Remove work-arounds for setting cell_contents

Simple assignment to cell_contents works starting from Python 3.7[1].
This is also the minimum supported version of Python according to
pyproject.toml. It follows that we don't need complex work-arounds that
were needed in the past.

Also tested on pypy 3.8.

[1] https://bugs.python.org/issue30486

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

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

---------

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-11-11 10:54:24 +00:00
Eugene Toder ea1037cc14
Fix mypy and pyright tests in CI (#1202)
Some error messages changed in mypy 1.7.0 and pyright 1.1.335.

Also, test_mypy sometimes fails with xdist (-n auto). A failure I can
reliably reproduce locally is due to changes in type variables numbering.
There may be other issues.
2023-11-11 10:45:04 +00:00
Tin Tvrtković fb97252186
Bump mypy, fix tests (#1190)
* Bump mypy, fix tests

* Skip Mypy on 3.7

* Maybe?

* Maybe again?

* Fix old school tuple

* Fix test
2023-10-17 05:48:35 +02:00
Hynek Schlawack 1046c75480
Monkeypatching is software bankruptcy 2023-09-29 08:57:03 +02: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 a5cbd51fd9
Tighten linting (#1175)
* Tighten linting

* Consistency

* Simplify config

* Simplify
2023-08-20 12:03:53 +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
Sergei Lebedev 36d4762498
Switched from __dataclass_transform__() to typing.dataclass_transform() (#1158) 2023-07-06 18:41:45 +02:00