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 pipe Union type hints not supported #56

Closed
jwfraustro opened this issue Jun 1, 2023 · 0 comments
Closed

Python 3.10 pipe Union type hints not supported #56

jwfraustro opened this issue Jun 1, 2023 · 0 comments

Comments

@jwfraustro
Copy link

Currently, the Model1 | Model2 Python 3.10 syntax of a union is not recognized between models by pydantic-xml.

This can be replicated pretty easily by changing the TestModel class here to use them:

def test_model_union():
class SubModel1(BaseXmlModel, tag='model1'):
attr1: int = attr()
class SubModel2(BaseXmlModel, tag='model2'):
text: float
class TestModel(BaseXmlModel, tag='model'):
field1: Union[SubModel1, SubModel2] = element()

as in:

    class TestModel(BaseXmlModel, tag='model'):
        field1: SubModel1 | SubModel2 = element()

this will fail when .from_xml() is called (and in testing my project, when to_xml() is called also) with

>   ???
E   pydantic.error_wrappers.ValidationError: 1 validation error for TestModel
E   field1
E     field required (type=value_error.missing)

pydantic/main.py:341: ValidationError

The cause for this is this function:

def is_union(type_: Any) -> bool:
return typing.get_origin(type_) is Union

Unions denoted by the | in Python 3.10 are actually types.UnionType rather than typing.Union.

typing.get_origin() actually returns it specifically:



https://github.com/python/cpython/blob/a5d2b546c1b0b73d0695b98838a3ddd497382999/Lib/typing.py#L2412-L2413



The fix is just adding UnionType to the is_union() function.

@jwfraustro jwfraustro changed the title Python 3.10 pipe union type hints not supported Python 3.10 pipe Union type hints not supported Jun 1, 2023
This was referenced Jun 10, 2023
Merged
dapper91 added a commit that referenced this issue Jun 10, 2023
- piped union typehints support added. See #56
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