This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Can't define a typing.Union containing a typing.re.Pattern
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: Martí Congost Tapias, gvanrossum, python-dev, xiang.zhang
Priority: normal Keywords:

Created on 2015-10-13 09:27 by Martí Congost Tapias, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg252923 - (view) Author: Martí Congost Tapias (Martí Congost Tapias) Date: 2015-10-13 09:27
Defining a union of typing.re.Pattern and anything that isn't AnyStr raises a TypeError exception.

Example:

from typing import Union, re

def foo(pattern: Union[str, re.Pattern]) -> None:
    pass

Exception traceback:

Traceback (most recent call last):
  File "/tmp/testtyping.py", line 7, in <module>
    def foo(pattern: Union[str, re.Pattern]) -> None:
  File "/usr/local/lib/python3.5/typing.py", line 534, in __getitem__
    dict(self.__dict__), parameters, _root=True)
  File "/usr/local/lib/python3.5/typing.py", line 491, in __new__
    for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
  File "/usr/local/lib/python3.5/typing.py", line 491, in <genexpr>
    for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
TypeError: issubclass() arg 1 must be a class
msg252937 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2015-10-13 15:54
Confirmed. The failure is because Pattern is _TypeAlias, which is not a type. But it should be allowed. I'll think of something. (Does mypy accept this?)
msg253198 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-10-19 21:56
New changeset 955d3faf727a by Guido van Rossum in branch '3.5':
Issue #25390: typing: Don't crash on Union[str, Pattern].
https://hg.python.org/cpython/rev/955d3faf727a

New changeset 04314479af0b by Guido van Rossum in branch 'default':
Issue #25390: typing: Don't crash on Union[str, Pattern]. (Merge 3.5->3.6)
https://hg.python.org/cpython/rev/04314479af0b
History
Date User Action Args
2022-04-11 14:58:22adminsetgithub: 69577
2015-10-19 21:57:04gvanrossumsetstatus: open -> closed
resolution: fixed
2015-10-19 21:56:46python-devsetnosy: + python-dev
messages: + msg253198
2015-10-13 15:54:58gvanrossumsetassignee: gvanrossum
2015-10-13 15:54:36gvanrossumsetnosy: + gvanrossum
messages: + msg252937
2015-10-13 13:05:52Martí Congost Tapiassetcomponents: + Library (Lib), - Interpreter Core
2015-10-13 10:13:54xiang.zhangsetnosy: + xiang.zhang
2015-10-13 09:27:47Martí Congost Tapiascreate