Commit Graph

230 Commits

Author SHA1 Message Date
Hynek Schlawack 504eefe91f
NG: make frozen classes comfortably subclassable (#687)
* NG: make frozen classes comfortably subclassable

* Add newsfragment

* This ain't markdown

* Address review
2020-09-05 11:14:54 +02:00
Hynek Schlawack 48755905ee
Don't reset custom __setattr__ in slotted classes (#681)
* Don't reset custom __setattr__ in slotted classes

Fixes #680

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

* Simplify

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

* Be defensive about __bases__

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

* _Classes_ always have a __dict__

* Tighten xfail

* Clarify what need to be reset and when

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

* Reset __attrs_own_setattr__ along with __setattr__

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

* Update src/attr/_make.py

Co-authored-by: Paul Ganssle <paul@ganssle.io>

* Differentiate between own (= attrs) and custom (= user) __setattrs__

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

* We've always used __bases__ in our call to type()

So no reason for being defensive.

* Update tests/test_setattr.py

Co-authored-by: Paul Ganssle <paul@ganssle.io>

Co-authored-by: Paul Ganssle <paul@ganssle.io>
2020-09-05 09:46:06 +02:00
Hynek Schlawack bfd7bb49b4
Ignore inherited field when comparing Attributes (#684)
* Ignore inherited field when comparing Attributes

fixes #682

* add newsfragment
2020-09-03 07:16:58 +02:00
Paul Ganssle dfb2ee284d
Make `zope` a semi-optional test dependency (#685) 2020-09-03 06:56:17 +02:00
Hynek Schlawack f680c5b83e pre-commit autoupdate
Latest black required to remove a bunch of commas to prevent line explosion.
2020-09-01 09:56:52 +02:00
David Euresti 4cfd52db51
Fix auto_attrib=None behavior when maybe_cls=None (#675)
* Fix auto_attrib=None behavior when maybe_cls=None

* Update tests/test_next_gen.py

Co-authored-by: David Euresti <github@euresti.com>

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2020-08-23 07:58:04 +02:00
Hynek Schlawack 358c85e74c
Fix auto-detection of eq/ne in NG APIs (#671)
Fixes #670
2020-08-21 05:19:15 +02:00
Hynek Schlawack fda437c91f
Add attr.field (#669)
* Add attr.field

* Add newsfragment

* better wordzzz

* Typo and clarification
2020-08-20 19:01:34 +02:00
Hynek Schlawack b02335f59d
Add attr.define/mutable/frozen (#666)
* Add attr.auto/mutable/frozen

Prepare for import attrs y'all!

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

* Try combining coverage using 3.8, always

* Add tests for overriding of auto_attribs

* Clarifications

* Add newsfragment

* s/auto/define

This is going be the next-gen name anyways, so no need to introduce new names.

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

* Missed two

* Add type stubs

* Beg for patience

* Explain second setup-python action

* fix comment
2020-08-17 16:22:33 +02:00
Hynek Schlawack 106c33c205
Clear __setattr__ if it was inherited && written by us (#663)
Signed-off-by: Hynek Schlawack <hs@ox.cx>
2020-08-13 13:49:44 +02:00
Hynek Schlawack e554373d16 Add unittest for Attribute.inherited behavior 2020-08-04 10:59:46 +02:00
David Euresti c42bf9ebb9
Add attr.resolve_types (#302)
* Add attr.resolve_types

This adds `attr.resolve_types` which can be used to resolve forward declarations in classes created using `__annotations__`

Fixes #265

* Fix flake8 and doctest

* Missed one

* Fix lint

* Address CR comments
Add to stubs
Make it a decorator, because why not?

* Fix test

* Rerun-tests please

* Rerun-tests please

* Fix another

* Fix lint

Co-authored-by: David Euresti <david@zapgram.com>
2020-07-22 11:43:07 +02:00
Hynek Schlawack 05e930c36f
Rename converters.chain to converters.pipe
Consistent with setters.pipe and conveys better that it acts like a UNIX pipe.

Signed-off-by: Hynek Schlawack <hs@ox.cx>
2020-07-21 14:43:09 +02:00
Hynek Schlawack 2f6fd8516c
Add on_setattr hooks to attr.s and attr.ib (#660)
* Add on_setattr hooks to attr.s and attr.ib

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

* Add PR newsfragment

* Fix attr.s doc sig

* Make _DisableType private

* Mark setters.frozen as NoReturn

* Rename setters.DISABLE to setters.NO_OP to clarify its purpose

DISABLE sounds less purposeful and doesn't convey its meaning as well.

* Fix type

* Loosen up type for convert even further

* Tighten type a tiny bit
2020-07-20 12:43:10 +02:00
Hynek Schlawack 77914a4b27 Better name for functional tests 2020-06-10 08:38:52 +02:00
Hynek Schlawack 277a5c1d45
Add control to generate __[sg]etstate__ (#642)
* Add control to generate __[sg]etstate__

Pass *getstate_setstate* to attr.s to control the generation.  Mostly
interesting for disabling it in slotted classes.

Fixes #512,#513

* Update glossary.rst
2020-05-11 07:11:24 +02:00
Hynek Schlawack be71c30792 Add more explicit test 2020-05-02 14:04:40 +02:00
Hynek Schlawack 5a0491c3e1
Fix test name 2020-04-30 17:56:26 +02:00
Hynek Schlawack 33b61316f8
Fix attribute collection (#635)
Co-Authored-By: Sviatoslav Sydorenko <wk.cvs.github@sydorenko.org.ua>
2020-04-06 11:41:52 +02:00
Hynek Schlawack f8f3f598a3 Tune down strategies a bit
It takes too much time, these changes cut the test times in half and shouldn't
affect the coverage negatively.
2020-03-30 11:17:38 +02:00
Hynek Schlawack 196d948613
Add auto-detection of self-implemented methods (#607)
* Implement auto_detect

Fixes #324

* Add test demonstrating total_ordering

* Ensure the order of applying total_ordering does not matter

* Warn if a method is missing

* Revert "Warn if a method is missing"

This reverts commit 590ef43458040e0a1b9f3644ba22b4229f7d6990.

* Add stern warning that nobody will read
2020-03-16 13:03:12 +01:00
Hynek Schlawack 57cb2378b4 Fix hypothesis warning 2020-03-13 09:50:20 +01:00
smarie 07810aec7b
Fix issue 589 (#590)
* from parsyfiles development (https://github.com/smarie/python-simple-file-collection-parsing-framework):
* added new 'chain' validator to allow users to chain their custom validators with, for example, 'instance_of'
* added utility methods to get the declared attribute type (by inspecting its validators) and to check if it is optional

* from parsyfiles development (https://github.com/smarie/python-simple-file-collection-parsing-framework):
* added tests
* fixed bug with optional validator to pass tests :)

* from parsyfiles development (https://github.com/smarie/python-simple-file-collection-parsing-framework):
* completed doc api.rst
* small mod in tests

* Fixed issue with empty cell in closure when slots are used. Fixed #589

* Removed api.rst mods erroneously injected from another branch.

* Removed api.rst mods erroneously injected from another branch.

* noqa so that flake8 is happy

* Fixed lint error

* Added towncrier changelog entry

* Attempt to fix linter error

* Update tests/test_slots.py

* Last lint fix

* Update changelog.d/590.change.rst

Co-Authored-By: Hynek Schlawack <hs@ox.cx>

* Added extensive doc in the new test

* Update tests/test_slots.py

* last lint error

Co-authored-by: Sylvain MARIE <sylvain.marie@se.com>
Co-authored-by: Hynek Schlawack <hs@ox.cx>
2020-03-13 06:06:14 +01:00
Antonio Botelho 4bd6827091
Wrapped __ne__ in a _make_ne function #626 (#632)
* Wrapped __ne__ in a _make_ne function to guarantee that each attrs class gets a new instance of the __ne__ function

* Changed _make_eq to return just __eq__ instead of __eq__ and __ne__

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2020-03-09 14:56:09 +01:00
Simone Robutti 4dea97e633
added first doc stub (#506)
* added first doc stub

* fixed test

* review

* added newsfragment

* improved docs

Co-authored-by: Simone Robutti <simone.robutti@teraki.com>
2020-03-06 13:54:27 +01:00
Nativ Issac 9fcfe3479f Add and_ converter (#618)
* Add and_ converter

* update _make

* Refactored code according to review

* fixed some linting errors

* minor refactor

* fixed CR comments

* fixed CR comment #2

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2020-03-06 10:34:11 +01:00
Paul Ganssle d6a65fb89f
Use a wrapper class for cache hash to prevent cache serialization (#620)
* Use an self-clearing subclass to store hash cache

Rather than attempting to remove the hash cache from the object state on
deserialization or serialization, instead we store the hash cache in an
object that reduces to None, thus clearing itself when pickled or
copied.

This fixes GH #494 and #613.

Co-authored-by: Matt Wozniski <godlygeek@gmail.com>

* Add test for two-argument __reduce__

I couldn't think of any way to make a useful and meaningful class that
has no state and also has no custom __reduce__ method, so I went
minimalist with it.

* Improve test for hash clearing behavior.

Previously, there was some miniscule risk of hash collision, and also it
was relying on the implementation details of `pickle` (the assumption
that `hash()` is never called as part of `pickle.loads`).

* Add improved testing around cache_hash

* Update src/attr/_make.py

Co-Authored-By: Ryan Gabbard <gabbard@isi.edu>

* Update comment in slots_setstate

Since the cached hash value is not actually serialized in __getstate__,
__setstate__ is not actually "clearing" it on deserialization - it's
initializing the value to None.

* Add changelog entry

* Remove changelog for #611

This change was overshadowed by a more fundamental change in #620.

Co-authored-by: Matt Wozniski <godlygeek@gmail.com>
Co-authored-by: Ryan Gabbard <gabbard@isi.edu>
2020-02-10 17:16:07 +01:00
Paul Ganssle 9b5e988ccc Fixed frozen and cache_hash incompatibility (#612)
* Add xfailing test for frozen/cache_hash issue

This is a failing test for GH issue #611, with the xfail decorator to be
removed when the issue is fixed.

* Fix incompatibility between frozen and cache_hash

Fixes GH issue #611: serialization of a class with frozen=True and
cache_hash=True will now succeed rather than raising a
FrozenInstanceError.

Co-authored-by: Hynek Schlawack <hs@ox.cx>
2020-01-13 08:46:11 +01:00
Hynek Schlawack 43177cc99c pre-commit autoupdate 2019-12-30 14:01:39 +01:00
Tim Gates 311816bb80 Fix simple typo: passses -> passes (#601) 2019-11-19 15:13:45 +01:00
Bruno Oliveira e2b02ec39e Fix default values that are not comparable with '==' (#586)
Fix #585
2019-10-15 06:56:12 +02:00
Hynek Schlawack 7b49d375c7 Rename _version.py into _version_info.py
Makes a lot more sense.
2019-10-02 16:12:03 +02:00
Hynek Schlawack 7fcd616aab Complete suffix handling coverage 2019-10-01 16:48:30 +02:00
Hynek Schlawack 955d622493
Add attr.__version_info__ (#580)
* Add attr.__version_info__

This allows users to check for features and avoid deprecation warnings without
breaking backward compatibility.

* Add newsfragment

* Stay ASCII

* Typo

* Add stubs for _version.py

* Address David's feedback

* Handle PY2 better in comparability test

* drop the ing
2019-10-01 16:06:36 +02:00
David Euresti 08bbaabaca Fix some typing issues (#581)
* Typecheck stubs in CI and fix type errors.  Fixes #578

* Add overloads to instance_of definition.

Improves #576
2019-09-26 06:49:15 +02:00
Hynek Schlawack 08fcfe91d4
Split cmp into eq and order (#574)
* Split cmp into eq and order

Fixes #170

* Fix tests on old pypy3 versions

Old as in: currently on AP.

* Fix issue number and clarify newsfragment

* Clarify behavior and interaction between cmp/eq/order

* This sounds better

* Address Julian's review comments

* Missed a cmp

* Test the behavior of Attribute.cmp

* Make test more idiomatic

* Explain assumptions

* Clarify comment

* Grammar

* One more cmp!
2019-09-22 15:07:19 +02:00
Julian Berman 8824dc26c2 Add default_role for Sphinx. (#571)
* Add default_role for Sphinx.

Fix a bunch of broken refs along the way, which become
errors now via -W if you have default_role set.

In theory you can catch those via sphinx-build -n (i.e.
nitpick mode), which IMHO is a decent idea anyhow, but it's
a longer diff to enable that because it'd involve fixing a
bunch of the places that try to reference types that don't
exist (e.g. :type foo: Any value). But obviously can be done.

Also didn't actually use this anywhere yet (the any role),
but will do so in a follow-up if this is acceptable.

* Remove the roles from builtin objects to demo any.

* Style.

* Add z.i to intersphinx.

Enables the link to z.i.Interface.

* Enable nitpick mode.

Fix the remaining broken links or whitelist them via nitpick_ignore.

* Kill all :func:s.

* Kill all :class:es.

* Kill all :doc:s.

* Kill all :ref:s.

Except one, that probably meant :func:, and which
is a duplicate ref.

* Kill :exc: and :data:.

* Kill :mod:s.

* Kill new explicit refs from the merge.
2019-09-09 15:02:16 +02:00
Hynek Schlawack b8671adf98 re_matches: test for error messages 2019-09-09 12:19:01 +02:00
Hynek Schlawack d153daa4ef Split up re_matches tests 2019-09-09 11:56:44 +02:00
Hynek Schlawack 78cb7d4903 Refactor matches_re a bit 2019-09-09 11:38:08 +02:00
Hynek Schlawack 9bc319b7c8 Add example for matches_re typing 2019-09-09 11:11:50 +02:00
Kurt Rose 08b15877c7 Add matches_re validator (#552)
* matches

* added flags pass-through, and multiple function options; defaults to fullmatch

* move logic from validate to constructor

* cleaned up default args / fullmatch emulation

* fixed meta validator

* cleaned up default args / fullmatch emulation

* test cleanup

* versionadded tag

* added changelog entry

* cleaned up type checks, added to api docs and pyi type signature

* black formats

* black formatting

* 100% test coverage

* added example usage of matches_re to docs

* fixing py2 swallow type error and wrong quote in doctest

* explicit tests for all possible func args

* pragma no branch to get coverage 100%
2019-09-09 10:48:52 +02:00
Hynek Schlawack f84f451557
Stop comparing/ordering subclasses (#570)
* Stop comparing/ordering subclasses

This has been deprecated for a year and was raising a warning.

* Kill double consider
2019-09-07 12:19:10 +02:00
wouter bolsterlee 0b913645c5 support custom repr() callable for attributes (#568)
* wip: support custom repr() callable for attributes

see #567

* extend ‘repr=...’ arg type in .pyi stubs

* expand docstring for attr.ib()

* add changelog entry

* add docs with example

* improve my copy/paste skills 🙈

* fix grammar

* fix typo in changelog entry

* fix and improve attrib() docstring

* detect custom repr() once, not per call. be strict about bool.

* use rst syntax, not markdown

* apply hynek's suggestions for changelog entry

* add ‘versionchanged’ note in docstring

* add custom attribute repr= to typing example

* simplify comment
2019-09-06 14:26:08 +02:00
Hynek Schlawack 22b8cb1c4c Add newsfragment for #559, some polish 2019-08-21 10:28:31 +02:00
Juan Pedro Fisanotti 94ee269438 Ability to have `kw_only` attributes defined anywhere, as an optional feature (#559)
* Ability to disable the keyword-only order checks that forbid normal attrs after kw_only attrs

* Updated typing info

* Updated tests, and added a new test for the new ordering feature

* Pep8

* Updated docstring

* Rename to a name that reflects the usage, not the implementation

* Update tests after the rename

* Huge simplification: never check kw_only and non-init attributes order

* No more new parameter, remove from typing info

* Update tests after the simplification, and add new test
2019-08-21 10:15:18 +02:00
Hynek Schlawack bde0801987
Make auto_exc=True classes hashable by ID (#563)
* Make auto_exc=True classes hashable by ID

They now behave as the docs claim.

Fixes #543

* Add PR newsfragment

* Be more specific about NOP
2019-08-19 20:03:44 +02:00
Joshua Oreman cf9de77409 Update closure cell rewriting to use CodeType.replace() on 3.8 (#562)
* Revert "Don't use CodeType.replace() -- it was added in 3.8.0b1, but CI still uses 3.8.0a4"

Our CI now supports CodeType.replace(), and the only 3.8 versions that don't support it are alpha releases so we don't need to maintain support for them. Using replace() is more forward compatible.

This reverts commit 52097ce321.

* Disable forced-failure test on 3.8+ -- there's no longer enough surface area for a monkeypatch to do anything
2019-08-08 07:57:33 +02:00
David Euresti eda9f2de6f Make unique_filename not depend on repr. (#560)
* Make unique_filename not depend on repr.

For debugging purposes, when a method is created it needs to be put
into a "filename".  This filename was generated using `repr(attrs)`
however this meant that the filename could change every time the
program was loaded depending on the whims of repr.  (In particular
free functions and lambdas would include ids)

This solves the problem by changing the name to be <attrs generated
{method} {module}.{qualname}> If it should happen that the name is
taken then -2 will be appended and incremented until a free filename
is found.

* Add tests

* Fix tests and changelog
2019-08-01 12:42:13 +02:00
Hynek Schlawack dc1b5a01e9 Ensure that bare attributes with default None are removed too (#556)
Fixes #523
2019-07-23 13:19:10 +02:00