How do I get a regex pattern type for MyPy
Starting from Python 3.9 typing.Pattern
is deprecated.
Deprecated since version 3.9: Classes Pattern and Match from re now support []. See PEP 585 and Generic Alias Type.
You should use the type re.Pattern
instead:
import re
def some_func(compiled_regex: re.Pattern):
...
mypy
is very strict in terms of what it can accept, so you can't just generate the types or use import locations that it doesn't know how to support (otherwise it will just complain about library stubs for the syntax to a standard library import it doesn't understand). Full solution:
import re
from typing import Pattern
def my_func(compiled_regex: Pattern):
return compiled_regex.flags
patt = re.compile('')
print(my_func(patt))
Example run:
$ mypy foo.py
$ python foo.py
32