Commit Graph

280 Commits

Author SHA1 Message Date
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
David Euresti 82ab0c1d11
Fix mypy tests. Looks like the tests are error order sensitive (#856) 2021-10-30 21:05:49 +02:00
Jasper Spaans 44ea327ab7
Make `from attr import *` work again on recent python versions. (#848)
* Make `from attr import *` work again on recent python versions.

* Move import * test into a function and mark some flake8 exceptions.

* Please the linters.

* Update attr_import_star.py

test was renamed, rename it in the comment as well
2021-10-04 16:23:50 +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
David Euresti b8f5cd780b
Fix mypy tests. (#844)
Looks like the ordering changed
2021-09-19 17:09:35 +02:00
Rebecca Turner e84b57ea68
Inline distutils.util.strtobool in tests (closes #813) (#830)
* Inline distutils.util.strtobool in tests (#813)

`distutils` is deprecated in Python 3.10 and slated for removal in
Python 3.12. Fortunately, `attrs` only uses `distutils` once and it's
trivial to remove.

As suggested by @sscherfke, add the `to_bool` converter to
`converters.py`.

Closes #813

Co-authored-by: Stefan Scherfke <stefan@sofa-rockers.org>

* Use :raises: directive in docstring

* Remove f-strings for Py2.7 and 3.5 support

* Add to_bool tests

Co-authored-by: Stefan Scherfke <stefan@sofa-rockers.org>
Co-authored-by: Hynek Schlawack <hs@ox.cx>
2021-08-10 07:45:28 +02:00
Tin Tvrtković 38580632ce
Rework linecache handling (#828)
* Rework linecache handling

* lint

* Add changelog
2021-06-30 08:28:56 +02:00
David Euresti ec249f62b3
Fix the mypy tests by double quoting. (#825) 2021-06-14 19:17:33 +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 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
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 01aa333005
Remove warning if eq is customized & order is not (#800)
Since this warning is raised from the attr.ib/attr.field call, we cannot verify
whether order is on at all for the whole class. Fixing this would require to
check _after_ the class is assembled which is both additional complexity as
well as performance overhead. Unless we get inundated by bug reports, this
doesn't seem worth it.
2021-05-05 10:02:31 +02:00
Antonio Botelho 71b4638b32
New attr.cmp_using helper function (#787)
* New attr.cmp_using helper function

* Updated cmp_using to use new_class instead of direct patching

* Removed stray annotation that make tests fail in PY2

* Skip test if PY2 because it won't raise TypeError when a method returns NotImplemented

* Skip test if PY2 because it won't raise TypeError when a method returns NotImplemented

* Fixed PY2 tests related to total_ordering

* Added tests to complete converage

* Moved class_name argument to definition and made it non-optional

* Fixed typo in variable name

* Revert accidental black formatting of __init__.pyi in commit 2b7970bf

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2021-05-01 14:21:37 +02:00
Diego Argueta 51e6dcfb8e
Narrower typing on Factory called with takes_self (#780)
* Narrower typing on Factory called with takes_self

* Make Literal annotation depend on Python version

* Fix linting error

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2021-03-24 09:04:19 +01: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
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 fc26dcace1
Fix make_class for dynamic generic classes (#770)
* Fix make_class for dynamic generic classes

Fixes #756

* Actually fix

* Optimize for Python 3
2021-02-26 15:48:52 +01:00
Hynek Schlawack 03d3fc7a71
Add Python 3.10 support (#763)
* Add Python 3.10 support

Python 3.10 has string types everywhere and that has unmasked a bunch of
bugs/edge cases in our code.

Especially the hooks code need a resolving helper for string types. I'm leaving
that for a separate PR.

Fixes #716

* More robust quote handling

* Use resolve_types decorator where possible

* Use a helper for __init__ annotations

* Add test for various ClassVars
2021-02-22 12:56:44 +01:00
Hynek Schlawack 44ac461146 Polish up #627 2021-02-22 08:59:09 +01:00
Antonio Botelho aefdb117fa
Allow user to customize how an attribute is compared (#435) (#627)
* Updated implementation of comparison behaviour customization.

* Fixed version of next release, updates newsfragment and documentation.

* Fixed documentation.

* Fixed documentation.

* Fixed comments and changelog.

* Fix doctest error

* Update src/attr/_make.py

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

* Pass eq_key and order_key explicitly in _CountingAttr

* Merged with master and resolved conflics after introduction of _make_method

Co-authored-by: Antonio Botelho <antonio@inhames.com>
Co-authored-by: Hynek Schlawack <hs@ox.cx>
2021-02-22 08:48:34 +01:00
Stefan Scherfke baa8f8c5b2 Add test asserting that evolve() correctly updates dicts
See: #759
2021-02-19 09:15:45 +01:00
Stefan Scherfke fe6eb31200
Recursively evolve nested attrs classes (#759)
* Recursively evolve nested attrs classes

Fixes: #634

* Apply suggestions from code review

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

* Update tests for recursive evolve()

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2021-02-19 07:01:09 +01:00
David Euresti 1499c77e79
Fix issue with get_type_hints(cls.__init__) and refactor (#760)
* Fix issue with get_type_hints(cls.__init__)

* Refactor

* Improve coverage
2021-02-19 06:50:41 +01:00
Hynek Schlawack c2712fd102 Add forgotten Attribute.evolve to stub
Fixes #752
2021-01-29 11:09:02 +01:00
Hynek Schlawack 68b5e048da mypy has caught up! Uncomment NG type tests 2021-01-29 10:29:17 +01:00
Venky Iyer efcbae51cd
__attrs_pre_init__ (#750)
* add pre-init following post-init pattern

* add tests

* add changelog

* some typos
2021-01-25 07:31:37 +01:00
Venky Iyer 654aa92475
__attrs_init__() (#731) 2021-01-23 13:03:04 +01:00
David Euresti 467e28b268
Fix mypy tests (#749) 2021-01-23 10:33:13 +01:00
Hynek Schlawack a025629e36 Minor word smithing 2021-01-11 07:46:30 +01:00
Vitaliy Yelnik 9de675f4b4
Fix: TypeError when using properties, super(), and slots=True (#652) (#747) 2021-01-11 07:44:16 +01:00
Alex Ford 2fdf92997c
Clarify next-gen auto_attribs inference rules (#742)
* Clarify next-gen auto_attribs inference rules

The next-gen auto_attribs api documentation does not clearly describe
cases where (a) only a subset of attributes are annotated and (b)
`field` definitions are provided for a subset of fields. Update
docstring to reflect current, desirable, behavior.

Add tests to clarify `.define` behavior focused on fully-annotated
classes with partially-defined fields, which is commonly used to add
non-default behavior to a subset of a classes fields. For example:

```python

@attr.define
class NewSchool:
    x: int
    y: list = attr.field()

    @y.validator
    def _validate_y(self, attribute, value):
        if value < 0:
            raise ValueError("y must be positive")

```

The previous docstring *could* be read to imply that:

* The new-school API will not infer auto_attribs if there are any
  unannotated attributes.
* The new-school API will not infer auto_attribs if *any* attr.ib are
  defined, even if those attr.ibs are type annotated.

* Update test to match PR example

* Fix lint error

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2020-12-30 12:53:43 +01:00
David Euresti 6b84ad2db2
Move some of the mypy tests over. (#738)
* First pass

* Fix the tests

* trim

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2020-12-28 07:50:29 +01:00
Hynek Schlawack f132c07e55
Make NOTHING falsey (#732)
* Make NOTHING falsey

Fixes #720

* Add newsfragment

* Python 2
2020-12-21 06:39:58 +01:00
David Euresti 3d274d0bfa
WIP: Add mypy tests (#737)
* Add basic mypy tests

* Only run mypy on 3.6+

* No pypy

* Fix things

* Oooh parametrized
2020-12-21 06:21:28 +01:00
Andrei Bodrov 1f627dd3d5
Fixed slots inheritance (#718)
* Fixed slots inheritance

* Added changelog

* Added a separate test

* Restored backwards-compatibility and added a test for it

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2020-12-13 17:22:34 +01:00
Nicholas Coltharp e09b1d6423
Infer type annotations from converters (#710)
* Infer annotations from converters

* Use semantic newlines

* Add 787.change.rst

* Don't let type annotations override converters

* Make pipe() infer type annotations

* Use PY2 instead of sys.versioninfo >= (3, 3)

* Avert crashing with a nullary converter

* Small doc change

* Add type inference for optional()

* Make pipe() annotations actually work

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2020-12-13 16:30:06 +01:00
Hynek Schlawack f2dabeae82
Fix exception chaining on PyPy (#712)
* Fix exception chaining on PyPy

Fixes #703

* Add newsfragment

* Blankly exclude PyPy from coverage reporting

* Manually add default no cover marker
2020-11-04 11:02:15 +01:00
Hynek Schlawack 6b4a1f1ce6 Tighten up mypy configuration
To prevent 9f7d11e in the future.

Add type annotations to typing example to make it pass too.
2020-11-04 08:40:16 +01:00
Hynek Schlawack 56c73081c5 Add missing collect_by_mro to typing stubs
Fixes #711
2020-11-03 10:10:43 +01:00
Zac Hatfield-Dodds f6ffab7be0
Refactor Hypothesis strategy for self-tests (#706)
* Ease Hypothesis shrinking

This is a tiny performance improvement for shrinking.

Unconditionally drawing and conditionally *using* these boolean flags minimises the impact of mutating other parts of an input on the structure here, which in turn means more attempts will be valid.

* Refactor Hypothesis strategy

Using the composite decorator allows for a much clearer imperative description of how the class is constructed.
2020-10-21 07:04:12 +02:00
Andrei Bodrov bc527b9f29
[RFC] kw_only python 2 backport (#700)
* Added kw_only support for py2

* Docs update

* Added changelog

* Better exception message, moved code to function

* Moved py2-only functions under if PY2:

* Tested fancy error message for unexpected kw-only argument

* Tested fancy error message for unexpected kw-only argument

* Fixed line length

* Added versionchanged

* Updated docs

* Moved functions back under if PY2 - seems codecov doesn't like them in module scope

* Blacked

* Fixed changelog.d

* Removed redundant brackets in test

* Added assertion to the _unpack_kw_only_lines_py2 - hope it will increase code coverage

* List comprehension -> for loop

* lines.extend? I do not like for-loops

* Fix code

* Fixed style/added comment

* Fixed docs (removed python2 mention)

* Fix lint

* Better docstring

* Rewritten docstring and added example code

* Added quotes

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2020-10-19 11:00:00 +02:00
Hynek Schlawack 0e6c74ac17
Treat frozensets like sets in asdict/astuple (#704)
* Treat frozensets like sets in asdict/astuple

Fixes #657

* Add newsfragment
2020-10-19 10:17:25 +02:00
Hynek Schlawack ae51f34681 Attribute.assoc → evolve 2020-10-17 07:27:36 +02:00
Hynek Schlawack 83fef48b2d Ensure objects live long enough in hash test 2020-10-16 08:35:42 +02:00
Stefan Scherfke 0eae613ce1
Add hooks for field transformation and for asdict serialization (#653) 2020-10-15 09:33:59 +02:00