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

Python 3.10's new Union syntax not working #162

Open
meshy opened this issue Apr 26, 2022 · 2 comments
Open

Python 3.10's new Union syntax not working #162

meshy opened this issue Apr 26, 2022 · 2 comments
Labels
feature A new feature that awaits implementation

Comments

@meshy
Copy link

meshy commented Apr 26, 2022

Hello! I've discovered some problems when using the new python union syntax (PEP-604):

Observed behaviour (example extrapolated from existing tests):

>>> import jsons
>>> class C:
...     def __init__(self, x: str | None):
...         self.x = x
>>> jsons.dump(C("42"))
# Note: warning printed here reproduced below
{}

Expected behaviour:

>>> jsons.dump(C("42"))
{"x": 42}

Python version: 3.10.4
jsons version: 1.6.1

Note: this warning was in the observed output. I extracted it to reduce noise.
/.../jsons/_common_impl.py:43: UserWarning: Failed to dump attribute "42" of object of type "C". Reason: 'NoneType' object is not callable. Ignoring the attribute. Use suppress_warning(attribute-not-serialized) or suppress_warnings(True) to turn off this message. warnings.warn(msg_, *args, **kwargs)

Please let me know if I can provide more information.

@meshy
Copy link
Author

meshy commented May 13, 2022

As a work-around, I've been doing this to stop pyupgrade from changing my code to the new syntax in this case:

from typing import Optional
import jsons

str_or_none = Optional[int]

class C:
    def __init__(self, x: str_or_none):
        self.x = x

@ramonhagenaars ramonhagenaars added the feature A new feature that awaits implementation label Jun 9, 2022
@ramonhagenaars
Copy link
Owner

Hi @meshy! Ah yes, the new pipe-syntax! I was hoping it would work out of the box, but apparently not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A new feature that awaits implementation
Projects
None yet
Development

No branches or pull requests

2 participants