I would like to use autocompletion for a pre-compiled and stored list of regular expressions, but it doesn't appear that I can import the _sre.SRE_Pattern class, and I can't programmatically feed the obtained type from type() to a comment of the format # type: classname or use it for a return -> classname style hint
Is there a way to explicitly import a class from the _sre.c thing?
If you need to support Python 3.8 or earlier, you should use typing.Pattern and typing.Match, which were specifically added to the typing module to accommodate this use case.
Example:
from typing import Pattern, Match
import re
# Type annotations here added purely for demonstration purposes.
# Most type checkers will deduce the correct type automatically.
my_pattern: Pattern[str] = re.compile("[abc]*")
my_match: Match[str] = re.match(my_pattern, "abbcab")
If you are using newer versions of Python, you can use re.Pattern and re.Match instead, as noted in the answer below.
This means you can adapt the above example by simply changing the imports, like so:
from re import Pattern, Match
import re
my_pattern: Pattern[str] = re.compile("[abc]*")
my_match: Match[str] = re.match(my_pattern, "abbcab")