Skip to content
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

Support 3.10 types.UnionType #80

Open
ROpdebee opened this issue Oct 27, 2022 · 1 comment
Open

Support 3.10 types.UnionType #80

ROpdebee opened this issue Oct 27, 2022 · 1 comment
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@ROpdebee
Copy link

Describe the bug
3.10 introduced PEP-604 which allows writing unions as X | Y over Union[X, Y]. However, internally the type of such annotation is not typing.Union, it's types.UnionType. attrs-strict doesn't recognise it, leading to validation errors.

To Reproduce

from attrs import define, field
from attrs_strict import type_validator

@define
class X:
  a: int | None = field(validator=type_validator())

X(a=10)

Expected behavior
The above example should work.

Actual behavior
attrs_strict._error.AttributeTypeError: a must be int | None (got 10 that is a <class 'int'>)

Environment (please complete the following information):

  • Operating System and Version: macOS 11.5.2
  • Python version: 3.10.6

Additional context

>>> X.__annotations__
{'a': int | None}
>>> X.__annotations__['a']
int | None
>>> type(X.__annotations__['a'])
<class 'types.UnionType'>
>>> X.__annotations__['a'].__args__
(<class 'int'>, <class 'NoneType'>)

Here's a similar issue in pydantic related to union types as a reference: pydantic/pydantic#3300

@gaborbernat
Copy link
Contributor

PR welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants