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

Bug: Unions broken in python 3.10 #8

Closed
llucid-97 opened this issue Aug 2, 2022 · 3 comments
Closed

Bug: Unions broken in python 3.10 #8

llucid-97 opened this issue Aug 2, 2022 · 3 comments

Comments

@llucid-97
Copy link

Hi there, I ran into another bug while using this:

I'm using python 3.10 which has some syntax sugar for creating unions using the | operator from https://peps.python.org/pep-0604/

Interestingly, this causes the type checking to fail, when using the old way of creating a typing.Union object doesn't:

Here's the base case:

import dataclasses
import dataclass_utils
import typing


@dataclasses.dataclass
class Foo:
    bar: typing.Union[str, int] = "bar"


f = Foo()
dataclass_utils.check_type(f)
print(f"Pass! {f=}")

This passes as expected:

/home/agimg/anaconda3/envs/pbrl/bin/python /home/agimg/projects/fastpbrl/scripts/scratchpads/dataclass_utils_bug_2.py
Pass! f=Foo(bar='bar')

Process finished with exit code 0

But using the syntax sugar:

import dataclasses
import dataclass_utils


@dataclasses.dataclass
class FooWithSugar:
    bar: str | int = "bar"


f_sugar = FooWithSugar()
dataclass_utils.check_type(f_sugar)
print(f"Pass! {f_sugar=}")

Fails with:

/home/agimg/anaconda3/envs/pbrl/bin/python /home/agimg/projects/fastpbrl/scripts/scratchpads/dataclass_utils_bug_2.py
Traceback (most recent call last):
File "/home/agimg/projects/fastpbrl/scripts/scratchpads/dataclass_utils_bug_2.py", line 25, in
dataclass_utils.check_type(f_sugar)
File "/home/agimg/anaconda3/envs/pbrl/lib/python3.10/site-packages/dataclass_utils/type_checker.py", line 189, in check_root
raise err
dataclass_utils.error.Error0: Error in field 'bar'. Expected type <class 'types.UnionType'>, got <class 'str'> (value: bar)

Process finished with exit code 1

Do you plan on supporting this new syntax?

@tamuhey
Copy link
Owner

tamuhey commented Aug 3, 2022

Hi, thanks for reporting the bug! I will try to create a patch.

@tamuhey
Copy link
Owner

tamuhey commented Aug 4, 2022

Just published a new version 0.7.22, which should solve this issue.

@tamuhey tamuhey closed this as completed Aug 4, 2022
@llucid-97
Copy link
Author

It works for me now. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants