[New-bugs-announce] [issue42233] GenericAlias does not support union type expressions

Ken Jin report at bugs.python.org
Sun Nov 1 10:32:10 EST 2020


New submission from Ken Jin <kenjin4096 at gmail.com>:

Union type expressions added in PEP 604 throw an error when both operands are GenericAlias objects.

Eg the following works::

int | list[str]

The following throws TypeError::

list[int] | dict[float, str]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'types.GenericAlias' and 'types.GenericAlias'

I have submitted a PR to fix this. Coincidentally, it also fixes the fact that union expressions weren't de-duplicating GenericAlias properly.

Eg::
>>> list[int] | int | list[int]
list[int] | int | list[int]

For non-GenericAlias type expressions, the new code shouldn't be much slower. Rich compare is only used for GenericAlias objects. This isn't very scientific, but 

python -m timeit "int | str | float"

# original
1000000 loops, best of 5: 295 nsec per loop

# purely rich compare
1000000 loops, best of 5: 344 nsec per loop

# check  for GenericAlias and rich compare only for that
1000000 loops, best of 5: 297 nsec per loop

----------
components: Interpreter Core
messages: 380145
nosy: gvanrossum, kj, levkivskyi
priority: normal
severity: normal
status: open
title: GenericAlias does not support union type expressions
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42233>
_______________________________________


More information about the New-bugs-announce mailing list