Commit Graph

195 Commits

Author SHA1 Message Date
NI1993 7804a68ee3
Added attrs.validators.min_len() (#916)
* Added attrs.validators.min_len()

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

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

* Rename foo.change.rst to 916.change.rst

* Update src/attr/validators.py

Co-authored-by: nativ <nativi@spotnix.io>
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-02-02 09:10:33 +00:00
Ray Zhang 947bfb5421
Fix hashing for custom `eq` objects (#909)
* Add hashing for eq'd objects

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

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

* Add coverage to test

* oops

* Add changelog message

* Revert "Add changelog message"

This reverts commit 0bd3f5cba6.

* Address comments

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-01-31 05:57:33 +01:00
davfsa 3df7bbe089
Speedup uses of `object.__setattr__` (#898)
* Speedup uses of `object.__setattr__`

* Add changelog fragment
2022-01-03 13:39:41 +00:00
Hynek Schlawack 2de9014310 Prepare 21.4.0 2021-12-29 14:09:28 +01:00
Hynek Schlawack 1ff3f1ee91 Minor changelog polish 2021-12-29 14:04:39 +01:00
Hynek Schlawack 0575d51ffd
Make virtual repr file names unique (#896)
* Make virtual repr file names unique

* Add newsfragments
2021-12-29 14:02:59 +01:00
Michał Górny 03dd7136cf
Do not require cloudpickle for PyPy (#892)
* Do not require cloudpickle for PyPy

The cloudpickle package relies on CPython implementation details,
and does not even import on PyPy:

```
ImportError while importing test module '/tmp/attrs/tests/test_3rd_party.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/pypy3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_3rd_party.py:7: in <module>
    import cloudpickle
.tox/pypy3/lib/pypy3.8/site-packages/cloudpickle/__init__.py:4: in <module>
    from cloudpickle.cloudpickle import *  # noqa
.tox/pypy3/lib/pypy3.8/site-packages/cloudpickle/cloudpickle.py:57: in <module>
    from .compat import pickle
.tox/pypy3/lib/pypy3.8/site-packages/cloudpickle/compat.py:13: in <module>
    from _pickle import Pickler  # noqa: F401
E   ModuleNotFoundError: No module named '_pickle'
```

Disable the dependency for PyPy and make the test handle missing
cloudpickle gracefully.

* Enable testing on pypy-3.8

* add a news entry

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2021-12-29 09:43:19 +01:00
Hynek Schlawack dd26edd68e Prepare 21.3.0 2021-12-28 06:59:45 +01:00
Hynek Schlawack fcfb5a692c Last pass over changelogs 2021-12-28 06:39:24 +01:00
Hynek Schlawack e7345584ff
Add attrs namespace (#887) 2021-12-25 15:15:10 +01:00
Hynek Schlawack 7b02220bd3
Serialize keys as tuples in asdict (#888)
* Add tuple_keys to asdict

See #646

* Add typing example

* Add newsfragments

* Add missing test

* Switch it on by default

* Let's not make buggy behavior configurable
2021-12-16 09:01:51 +01:00
Hynek Schlawack 679e4b443d
NG: convert on setattr by default (#886)
* NG: convert on setattr by default

Not doing that from the get-go was an oversight.

Fixes #835

* Add optimization for default on_setattr w/ no work to do

Otherwise we'd end up with an explicit setattr every time.

* Fix optimization for NG default & j/ convert

* NG is actually 3.6+

* Add test for convert optimization for good measure
2021-12-14 15:50:37 +01:00
wouter bolsterlee 1759260aa0
Add support for re.Pattern to validators.matches_re() (#877)
Fixes #875.

This adds support for pre-compiled regular expressions to
attr.validators.matches_re(). This cannot be combined with flags since
the pre-compiled pattern already has those.

Detailed changes:

- attr.validators.matches_re() now accepts re.Pattern in addition to
  strings; update type annotations accordingly.
- Convert percent-formatting into str.format() for an error message
- Simplify (private) _MatchesReValidator helper class a bit: use the
  actual compiled pattern, and drop the unused .flags attribute.
- Simplify control flow a bit; add pointer about fullmatch emulation.
- Add tests
- Tweak existing test to ensure that .fullmatch() actually works
  correctly by matching a pattern that also matches (but not
  ‘full-matches’) a shorter substring.

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2021-11-30 06:32:24 +01:00
Stefan Scherfke c6143d518b
Add "no_run_validators()" context manager (#859)
* Add "no_run_validators()" context manager

* Move functions to validators module

* Update changelog entry

* Add a few docstring improvements and fixes

* Update tests/test_validators.py

* Minor polish

Signed-off-by: Hynek Schlawack <hs@ox.cx>

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2021-11-17 07:05:01 +01:00
Hynek Schlawack d884af5e6d
Rename 847.change.rst to 857.change.rst 2021-11-04 06:57:07 +01:00
Hynek Schlawack 11c66eff32
Update 847.change.rst 2021-11-04 06:53:23 +01:00
paul fisher 554d6f293d
Pull thread-local into _compat module to fix cloudpickling. (#857)
Because cloudpickle tries to pickle a function's globals, when it
pickled an attrs instance, it would try to pickle the `__repr__` method
and its globals, which included a `threading.local`. This broke
cloudpickle for all attrs classes unless they explicitly specified
`repr=False`. Modules, however, are pickled by reference, not by value,
so moving the repr into a different module means we can put `_compat`
into the function's globals and not worry about direct references.
Includes a test to ensure that attrs and cloudpickle remain compatible.

Also adds an explanation of the reason we even *have* that global
thread-local variable.  It wasn't completely obvious to a reader why
the thread-local was needed to track reference cycles in `__repr__`
calls, and the test did not previously contain a cycle that touched
a non-attrs value. This change adds a comment explaining the need
and tests a cycle that contains non-attrs values.

Fixes:
- https://github.com/python-attrs/attrs/issues/458
- https://github.com/cloudpipe/cloudpickle/issues/320
2021-11-04 06:52:10 +01:00
paul fisher 9eccd70571
Fix #824's changelog entry filename. (#860) 2021-11-03 18:44:44 +01:00
Hynek Schlawack e1d156239f
Fix rst 2021-10-28 07:16:37 +02:00
Stefan Scherfke 52fcad29bb
Add additonal validators (#845)
* Add additonal validators

* Python 2 \o/

* Add changelog entry

* Add "versionadded" tags

* More python 2

* Add doctests and rename maxlen to max_len

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2021-09-24 12:47:19 +02:00
Stefan Scherfke f57b6a6759
Convert transformed attrs to AttrsClass (#824)
* Convert transformed attrs to AttrsClass

Fixes: #821

* Add cangelog entry

* Only call AttrsClass once

* Calm mypy by inline the AttrsClass call

* Defer AttrsClass creation as long as possible

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2021-09-22 21:58:32 +02:00
David Euresti f31bb2850a
Fix bug in resolve_types with subclasses (#843)
* Fix bug in resolve_types with subclasses

* lint

* changelog

* lint again, like we did last summer

* Moar coverage

* lint ... why

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2021-09-19 21:04:55 +02:00
Hynek Schlawack fcb7393f53 Add changelog for #830, fix filenames 2021-08-10 07:48:50 +02:00
Tin Tvrtković 38580632ce
Rework linecache handling (#828)
* Rework linecache handling

* lint

* Add changelog
2021-06-30 08:28:56 +02:00
Tin Tvrtković 9709dd82e1
Initial implementation of a faster repr (#819)
* Initial implementation of a faster repr

* Switch to positional args for _make_repr

* Fix tests and changelog

* Remove trailing comma for Py2

* Fix lint

* __qualname__ is always present if f-strings work

* Fix Py2 qualname

* Revert "Fix Py2 qualname"

This reverts commit eb091a31d2.

* Update src/attr/_make.py

Co-authored-by: Hynek Schlawack <hs@ox.cx>

* Update changelog.d/819.changes.rst

Co-authored-by: Hynek Schlawack <hs@ox.cx>

* Update src/attr/_make.py

Co-authored-by: Hynek Schlawack <hs@ox.cx>

* Update src/attr/_make.py

Co-authored-by: Hynek Schlawack <hs@ox.cx>

* Update src/attr/_make.py

Co-authored-by: Hynek Schlawack <hs@ox.cx>

* Update src/attr/_make.py

Co-authored-by: Hynek Schlawack <hs@ox.cx>

* Update src/attr/_make.py

Co-authored-by: Hynek Schlawack <hs@ox.cx>

* Fix syntax

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2021-05-26 21:57:52 +02:00
Hynek Schlawack 8c9a79680f
Minor polish to #815
Signed-off-by: Hynek Schlawack <hs@ox.cx>
2021-05-18 11:29:29 +02:00
Karthikeyan Singaravelan 8613af97bb
Add __match_args__ to support match case destructuring in Python 3.10 (#815)
* Add support to generate __match_args__ for Python 3.10.

* Add versionadded directive.

* Update stubs.

* Update changelog and add a test to typing examples.

* Fix error regarding new-style classes in Python 2.

* Fix lint error regarding line length.

* Fix lint error regarding trailing whitespace.

* Add docstrings for interrogate.

* Use _has_own_attribute instead of cls.__dict__ contains check.

* Update docs as per review comments.

* Revert mistaken changelog update.

* Add Python 3.10 pattern matching syntax test cases.

* Update define signature with match_args.

* Fix conftest formatting.

* Fix isort formatting.

* Bump to Python 3.10 to parse syntax.

* Bump basepython of lint to Python 3.10 for parsing.

* Move lint to py310

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2021-05-18 07:02:06 +02:00
Hynek Schlawack 20c2d4fc0c
Optimize the case of on_setattr=validate & no validators (#817)
* Optimize the case of on_setattr=validate & no validators

This is important because define/mutable have on_setattr=setters.validate on
default.

Fixes #816

Signed-off-by: Hynek Schlawack <hs@ox.cx>

* Grammar
2021-05-17 09:24:46 +02:00
Hynek Schlawack 83d3cd70f9 Prepare 21.2.0 2021-05-07 11:22:31 +02:00
Hynek Schlawack f83dabb15e That comma is not necessary 2021-05-07 11:19:55 +02:00
Hynek Schlawack 744a790756 Clarify yanking of 21.1 2021-05-07 11:13:13 +02:00
Hynek Schlawack 8076287aeb
Create 807.breaking.rst 2021-05-06 18:39:48 +02:00
Hynek Schlawack f10d050864
Revert recursive evolve (#806)
* Revert "Recursively evolve nested attrs classes (#759)"

* Add regression test

* lol legacy python

* Add newsfragment

* Add a test to prevent inst -> dict replacement breaking
2021-05-06 15:26:27 +02:00
Hynek Schlawack b22195ee65 Prepare 21.1.0 2021-05-06 10:20:53 +02:00
Hynek Schlawack c5ae43f7f2 Pin towncrier
Currently the title is either not rendered at all, or rendered twice

cf.

- https://github.com/twisted/towncrier/issues/346
- https://github.com/twisted/towncrier/issues/340
2021-05-06 10:16:57 +02:00
Hynek Schlawack 503164f570 Fix changelog template for towncrier 21.3.0
See https://github.com/twisted/towncrier/issues/340
2021-05-06 10:08:29 +02:00
Hynek Schlawack 16f7a0df07 Make #796 newsfragment more timeless 2021-05-05 15:52:11 +02:00
Hynek Schlawack eb2df7f622 Add announcement about `import attrs` 2021-05-05 12:11:37 +02:00
Alex Ford 7e372c56a2
Implement pyright support via dataclass_transforms (#796)
* Add __dataclass_transform__ decorator.

* Add doc notes for pyright dataclass_transform support.

* Fix docs build error.

* Expand docs on dataclass_transform

* Add changelog

* Fix docs build

* Fix lint

* Add note on __dataclass_transform__ in .pyi

* Add baseline test of pyright support via tox

* Add pyright tests to tox run configuration

* Fix test errors, enable in tox.

* Fixup lint

* Move pyright to py39

* Add test docstring.

* Fixup docs.

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2021-05-05 10:24:53 +02:00
Hynek Schlawack 743f973889
Add narrative docs for #787 (#801) 2021-05-04 17:41:14 +02:00
Hynek Schlawack 936853283e Add newsfragment for #787 2021-05-01 14:32:22 +02:00
Hynek Schlawack da6fb00a7b Remove redundant newsfragment
It used the wrong PR# and because I couldn't find it, I wrote my own in 710.
2021-05-01 14:29:39 +02:00
Hynek Schlawack f47e568c9e
Make NG APIs official (#786) 2021-04-09 20:36:10 +02:00
Hynek Schlawack 066d1ca7df Add narrative docs for #731
Signed-off-by: Hynek Schlawack <hs@ox.cx>
2021-04-06 07:27:11 +02:00
Hynek Schlawack e01ddbe307
Detect ClassVars coming from typing_extensions (#782)
* Detect ClassVars coming from typing_extensions

* Add newsfragment
2021-03-20 18:02:59 +01:00
David Euresti ce8bb4ffa9
Add attribs to resolve_types and fix test_hooks (#774) 2021-03-05 16:01:04 -08:00
Hynek Schlawack af3045b0ca Eat some cmp crow 2021-02-28 13:23:51 +01:00
Antonio Botelho f580185cc4
Undeprecate cmp (#773)
* Document comparison

* Grammar

* Stress independence of eq/order

* Add example for eq

* Be consistent with fields

* Undeprecated cmp

* Update doc to remove cmp deprecation

* Reintroduced deprecation warning on Attribute cmp property

* Added changelog file

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2021-02-28 13:20:40 +01:00
Hynek Schlawack 2903da4b5b Add missing newsfragment 2021-02-22 12:58:30 +01:00
Hynek Schlawack 44ac461146 Polish up #627 2021-02-22 08:59:09 +01:00