-
-
Notifications
You must be signed in to change notification settings - Fork 424
Add attr.define/mutable/frozen #666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a4f2115
Add attr.auto/mutable/frozen
hynek 95a0fe7
Try combining coverage using 3.8, always
hynek d5e8c4f
Add tests for overriding of auto_attribs
hynek 6372905
Clarifications
hynek 24374be
Add newsfragment
hynek 65e1024
s/auto/define
hynek 8a4be45
Missed two
hynek 32cd109
Add type stubs
hynek f94afb6
Beg for patience
hynek 53f4ea5
Explain second setup-python action
hynek ac58b0c
fix comment
hynek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| **Provisional** APIs called ``attr.define()``, ``attr.mutable()``, and ``attr.frozen()`` have been added. | ||
|
|
||
| They are only available on Python 3.6 and later, and call ``attr.s()`` with different default values. | ||
|
|
||
| If nothing comes up, they will become the official way for creating classes in 20.2.0 (see above). | ||
|
|
||
| **Please note** that it may take some time until mypy – and other tools that have dedicated support for ``attrs`` – recognize these new APIs. | ||
| Please **do not** open issues on our bug tracker, there is nothing we can do about it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| """ | ||
| This is a Python 3.6 and later-only, keyword-only, and **provisional** API that | ||
| calls `attr.s` with different default values. | ||
|
|
||
| Provisional APIs that shall become "import attrs" one glorious day. | ||
| """ | ||
|
|
||
| from functools import partial | ||
|
|
||
| from attr.exceptions import UnannotatedAttributeError | ||
|
|
||
| from . import setters | ||
| from ._make import attrs | ||
|
|
||
|
|
||
| def define( | ||
| maybe_cls=None, | ||
| *, | ||
| these=None, | ||
| repr=None, | ||
| hash=None, | ||
| init=None, | ||
| slots=True, | ||
| frozen=False, | ||
| weakref_slot=True, | ||
| str=False, | ||
| auto_attribs=None, | ||
| kw_only=False, | ||
| cache_hash=False, | ||
| auto_exc=True, | ||
| eq=True, | ||
| order=False, | ||
| auto_detect=True, | ||
| getstate_setstate=None, | ||
| on_setattr=setters.validate | ||
| ): | ||
| r""" | ||
| The only behavioral difference is the handling of the *auto_attribs* | ||
| option: | ||
|
|
||
| :param Optional[bool] auto_attribs: If set to `True` or `False`, it behaves | ||
| exactly like `attr.s`. If left `None`, `attr.s` will try to guess: | ||
|
|
||
| 1. If all attributes are annotated and no `attr.ib` is found, it assumes | ||
| *auto_attribs=True*. | ||
| 2. Otherwise it assumes *auto_attribs=False* and tries to collect | ||
| `attr.ib`\ s. | ||
|
|
||
|
|
||
| .. versionadded:: 20.1.0 | ||
| """ | ||
|
|
||
| def do_it(auto_attribs): | ||
| return attrs( | ||
| maybe_cls=maybe_cls, | ||
| these=these, | ||
| repr=repr, | ||
| hash=hash, | ||
| init=init, | ||
| slots=slots, | ||
| frozen=frozen, | ||
| weakref_slot=weakref_slot, | ||
| str=str, | ||
| auto_attribs=auto_attribs, | ||
| kw_only=kw_only, | ||
| cache_hash=cache_hash, | ||
| auto_exc=auto_exc, | ||
| eq=eq, | ||
| order=order, | ||
| auto_detect=auto_detect, | ||
| collect_by_mro=True, | ||
| getstate_setstate=getstate_setstate, | ||
| on_setattr=on_setattr, | ||
| ) | ||
|
|
||
| if auto_attribs is not None: | ||
| return do_it(auto_attribs) | ||
|
|
||
| try: | ||
| return do_it(True) | ||
| except UnannotatedAttributeError: | ||
| return do_it(False) | ||
|
|
||
|
|
||
| mutable = define | ||
| frozen = partial(define, frozen=True, on_setattr=None) | ||
hynek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.