Repository navigation
Refactor and clean up the union type implementation #88820
Description
Activity
I started reviewing and rewriting Objects/unionobject.c several weeks ago. Some discovered bugs were reported and fixed in separate issues: bpo-44606, bpo-44632, bpo-44635, bpo-44636, bpo-44646, bpo-44652. Before fixing the remaining bugs (bpo-44633, bpo-44642, bpo-44653) I want to merge some minor changes:
- Rename _Py_UnionType to _PyUnion_Type. It is how PyTypeObject instances are named.
- Add _PyUnion_Check() and _PyGenericAlias_Check(). It simplified the code.
- Do not expose _Py_Union(). It is only used locally.
- Move declarations of _Py_make_parameters and _Py_subs_parameters from genericaliasobject.h to internal/pycore_unionobject.h. They are for internal use only.
- Fix a possible leak in dedup_and_flatten_args().
- Significantly simplify union_richcompare(). The Python implementation can be used for comparing types.Union with typing.Union.
- Perform cheaper tests before more expensive tests in is_unionable().
- Optimize __module__ look up in is_typing_module() and support types without __module__.
- Fix outdated comments.
- Minor style fixes.
- Extract related tests to separate class.
- Make some tests more accurate: they should test for TypeError only specific operators, not constructors.
I am going to backport these changes to 3.10 to make backporting of future fixes easier.
- added3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
on Jul 16, 2021 I am going to backport these changes to 3.10 to make backporting of future fixes easier.
- Move declarations of _Py_make_parameters and _Py_subs_parameters ...
Unfortunately, _Py_make_parameters and _Py_subs_parameters are 3.11 only. They were part of #26980. And we couldn't backport it to 3.10 (please see the PR thread). Maybe we can ask Pablo to reconsider for addition in 3.10rc1 if we can get a core dev to approve that?
- Perform cheaper tests before more expensive tests in is_unionable().
- Optimize __module__ look up in is_typing_module().
Yeah I noticed it re-lookups __module__ unnecessarily multiple times for the same type. I tried fixing this a month ago (and also convert to interned PyId strings) but for some reason microbenchmarks didn't show much gain. Things may be different after you clean things up.
- Extract related tests to separate class.
Yes please :). Maybe even a separate file altogether. GenericAlias has its own test_genericalias.
A big +1 on everything else. They were bugging me for a while but I didn't have the time to fix them. Thanks for taking this up Serhiy :)!
Yeah I noticed it re-lookups __module__ unnecessarily multiple times for the same type.
Actually I thought about this change, but did not implement it. Because it does not look performance critical, and future versions will likely do different tests (for example, testing the existence of special attribute, or check against lazily imported and cached classes from the typing module). Also, the test for TypeVar can be shared between genericaliasobject.c and unionobject.c.
It is mostly simple clean up, a line here, two lines there. genericaliasobject.c and unionobject.c could be merged in a single file, because they share several functions not used anywhere more. We perhaps merge them if more C implementations for typing be added in future.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: