pythonmypypython-typingpython-re

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?


Solution

  • 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.