-
-
Notifications
You must be signed in to change notification settings - Fork 386
Basic type support #239
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
Basic type support #239
Changes from 1 commit
69a7765
d10e1af
143e89f
1c2f766
0168ab8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Added ``type`` argument to ``attr.attr()`` and corresponding ``type`` attribute to ``attr.Attribute``. | ||
This value can be inspected by third-party tools for type checking and serialization. | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
In Python 3.6 or higher, the value of ``attr.Attribute.type`` can also be set using variable type annotations (see `PEP 526 <https://www.python.org/dev/peps/pep-0526/>`_). |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,7 +125,7 @@ def attr(default=NOTHING, validator=None, | |
value is converted before being passed to the validator, if any. | ||
:param metadata: An arbitrary mapping, to be used by third-party | ||
components. See :ref:`extending_metadata`. | ||
:param type: The type of the attribute. In python 3.6 or greater, the | ||
:param type: The type of the attribute. In Python 3.6 or greater, the | ||
preferred method to specify the type is using a variable annotation | ||
(see PEP-526 ). This argument is provided for backward compatibility. | ||
This comment was marked as spam.
10000
Sign in to view
Sorry, something went wrong. |
||
Regardless of the approach used, the type will be stored on | ||
|
@@ -198,7 +198,7 @@ def _transform_attrs(cls, these): | |
for name, ca | ||
in iteritems(these)] | ||
|
||
ann = getattr(cls, '__annotations__', {}) | ||
ann = getattr(cls, "__annotations__", {}) | ||
|
||
non_super_attrs = [ | ||
Attribute.from_counting_attr(name=attr_name, ca=ca, | ||
|
@@ -892,10 +892,10 @@ def from_counting_attr(cls, name, ca, type=None): | |
# type holds the annotated value. deal with conflicts: | ||
if type is None: | ||
type = ca.type | ||
elif ca.type is not None and type is not ca.type: | ||
elif ca.type is not None: | ||
raise ValueError( | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
"Type conflict: annotated type and given type differ: {ann} " | ||
"is not {given}.".format(given=ca.type, ann=type) | ||
"Type annotation and type argument are both present: " | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
"{ann}, {given}.".format(given=ca.type, ann=type) | ||
) | ||
inst_dict = { | ||
k: getattr(ca, k) | ||
|
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.