Regex and Python typing

Create issue
Issue #289 resolved
Jonathan Gossge created an issue

If I use re with the code below I get the following results:

import re
def f(match: re.Match) ->None:

m = re.match(r' *', ' ')
<class 're.Match'>

If I do the same thing in regex I get the following results:

import regex as re
def f(match: re.Match) ->None:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: module 'regex' has no attribute 'Match'
m = re.match(r' *', ' ')
<class '_regex.Match'>

The reason for this is that the re module contains the following two statements which make the Match and Pattern object public API.

Pattern = type(sre_compile.compile('', 0))
Match = type(sre_compile.compile('', 0).match(''))

If I add the following statements at the end of the regex module it exhibits the same behavior as the re module.

Pattern = type(_compile('', 0))
Match = type(_compile('', 0).match(''))

This is desirable because it makes regex compatible with re in this regard and makes it easier to use the Pattern and Match classes for Python typing as they are now public API instead of internal API.

Comments (3)

  1. Log in to comment