xsdata icon indicating copy to clipboard operation
xsdata copied to clipboard

Support subcriptable types and new Union Type

Open tefra opened this issue 3 years ago • 1 comments

The default generator has to remain compatible with all python versions, but someone could use https://github.com/asottile/pyupgrade to upgrade to 3.10 syntax.

Foo | Bar instead of typing.Union[Foo, Bar]
FooBar | None instead of typing.Optional[FooBar]
dict[str, FooBar] instead of typing.Dict[str, FooBar]
list[FooBar] instead of typing.List[FooBar]
tuple[FooBar, ...] instead of typing.Tuple[FooBar, ...]

xsdata should be able to resolve the typing annotations but there are a couple of issues

  • The new UnionType is not supported at all by xsdata
  • The subscriptable types don't evaluate forward references correctly, the issue was fixed in 3.11 but it wasn't backported to 3.10

The first issue is easy to correct but the second I am not so sure.

tefra avatar May 08 '22 06:05 tefra

Returning to this problem, why we can not simply define forward references with real types and add from __future__ import annotations? I looked at bpo-41370 and to get correct types we can change from this:

children: list['Node']

to this:

from __future__ import annotations

children: list[Node]

Then get_type_hints resolves forward references correctly. Just checked with python 3.10

nmrtv avatar Jul 21 '22 08:07 nmrtv

For us pyupgrade is doing exactly what you described. And now the generated dataclasses are not working anymore with the error:

  File "/home/user/venv/lib/python3.10/site-packages/xsdata/formats/dataclass/models/builders.py", line 540, in is_typing_supported
    and not issubclass(tp, Enum)
TypeError: issubclass() arg 1 must be a clas

sshishov avatar Mar 27 '23 12:03 sshishov

  • The subscriptable types don't evaluate forward references correctly, the issue was fixed in 3.11 but it wasn't backported to 3.10

That's because of this https://github.com/python/cpython/pull/30900 , the issue was fixed in 3.11 but it wasn't backported to 3.10

tefra avatar May 28 '23 16:05 tefra

Hi @tefra can we expect these new types to work in Python3.11+ or not? How we are going to tackle the problem?

sshishov avatar May 29 '23 06:05 sshishov

Hey @sshishov This feature is not merged on master, please give it a try.

No need to run pyupgrade, there are a few new options

  --subscriptable-types / --no-subscriptable-types
                                  Use PEP-585 generics for standard
                                  collections, default: false, python>=3.9
                                  Only
  --union-type / --no-union-type  Use PEP-604 union type, default: false,
                                  python>=3.10 Only

https://xsdata.readthedocs.io/en/latest/codegen.html

tefra avatar Jun 11 '23 16:06 tefra