You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The re.fullmatch() method either matches or not. The tuple and dict returned by .groups() and .groupdict() respectively are thus of fixed length equal to the number of groups defined in the regex and their elements corresponding to mandatory groups are always at the same type as input, elements corresponding to optional groups can also be None if not matched.
Hence, type checking should see this example script as correct, because:
the regex is known at type-checking time
the return statement of the parse_url() function is only reached when re.fullmatch() succeeds
However, it does not, because it treats fullmatch.groups() and fullmatch.groupdict() as variable length of str|None like those of other Match objects, where we indeed can't tell the length of sequences and whether the members are None or not:
$ mypy mypy_fullmatch_groups.py
mypy_fullmatch_groups.py:16: error: Incompatible return value type (got "Tuple[Tuple[Union[str, Any], ...], Dict[str, Union[str, Any]]]", expected "Tuple[Tuple[str, str, Optional[str]], ParsedDict]")
Found 1 error in 1 file (checked 1 source file)
Everything you say is correct, but there's no way to express this in typeshed's stubs. More precise inference along the lines of what you're describing would have to be provided via special-cased logic in the type checker.
python/mypy#7803, a long-open PR, proposes to add such support to mypy, but work on that PR appears to have stalled.
The
re.fullmatch()
method either matches or not. The tuple and dict returned by.groups()
and.groupdict()
respectively are thus of fixed length equal to the number of groups defined in the regex and their elements corresponding to mandatory groups are always at the same type as input, elements corresponding to optional groups can also beNone
if not matched.Hence, type checking should see this example script as correct, because:
return
statement of theparse_url()
function is only reached whenre.fullmatch()
succeedsHowever, it does not, because it treats
fullmatch.groups()
andfullmatch.groupdict()
as variable length ofstr|None
like those of otherMatch
objects, where we indeed can't tell the length of sequences and whether the members areNone
or not:Environment: Fedora 37 with:
The text was updated successfully, but these errors were encountered: