Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `re.Pattern` generic?

import re

x = re.compile(r"hello")

In the above code, x is determined to have type re.Pattern[str]. But why is re.Pattern generic, and then specialized to string? What does a re.Pattern[int] represent?

like image 998
bzm3r Avatar asked Oct 29 '25 02:10

bzm3r


1 Answers

re.Pattern was made generic because you can also compile a bytes pattern that will operate only on bytes objects:

p = re.compile(b'fo+ba?r')

p.search(b'foobar')  # fine
p.search('foobar')   # TypeError: cannot use a bytes pattern on a string-like object

At type-checking time, it is defined as generic over AnyStr:

class Pattern(Generic[AnyStr]):
    ...

...where AnyStr is a TypeVar with two constraints, str and bytes:

AnyStr = TypeVar("AnyStr", str, bytes)

re.Pattern[int] is therefore meaningless and would cause a type-checking error.

like image 162
2 revsInSync Avatar answered Oct 30 '25 16:10

2 revsInSync