Skip to content

Type-checking difference between re and regex modules #423

Open
@mrabarnett

Description

@mrabarnett

Original report by Anonymous.


The following code fails under both mypy and pyright:

import re
a: int = 1
m = re.match('foo', 'foobar')
if m:
  a = m[1]

As "str" is incompatible with "int". But the same does not happen for the regex module.

Is the solution something like

from typing import Match
m: Match[str] | None = regex.match(...)

or is there a better way to enable type checking here?

Thanks,

Rich

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions