Type of compiled regex object in python

Python 3.5 introduced the typing module. Included therein is typing.Pattern, a _TypeAlias.

Starting with Python 3.6, you can simply do:

from typing import Pattern

my_re = re.compile('foo')
assert isinstance(my_re, Pattern)

In 3.5, there used to be a bug requiring you to do this:

assert issubclass(type(my_re), Pattern)

Which isn’t guaranteed to work according to the documentation and test suite.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)