Support subcriptable types and new Union Type
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.
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
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
- 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
Hi @tefra can we expect these new types to work in Python3.11+ or not? How we are going to tackle the problem?
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