Skip to content

re.Pattern, re.Match etc. should not be callable #14558

@dchevell

Description

@dchevell

The following invalid code type checks in mypy and pyright:

# test.py
import re

obj1 = re.Pattern()
$ mypy test.py
Success: no issues found in 1 source file

$ mypy --version
mypy 1.17.1 (compiled: yes)

$ pyright test.py
0 errors, 0 warnings, 0 informations

$ pyright --version
pyright 1.1.403

At runtime, this would fail with TypeError: cannot create 're.Pattern' instances.

As a test, I modified the typeshed stub for Pattern to inherit from Protocol so the lack of a __call__ definition would be caught. No doubt this is a naive approach (nothing is ever so simple), but it seems to work:

- class Pattern(Generic[AnyStr]):
+ class Pattern(Generic[AnyStr], Protocol):
$ mypy test.py
test.py:3: error: Cannot instantiate protocol class "Pattern"  [misc]
Found 1 error in 1 file (checked 1 source file)

$ pyright test.py
/path/to/test.py
  /path/to/test.py:3:1 - error: Cannot instantiate Protocol class "Pattern" (reportAbstractUsage)
1 error, 0 warnings, 0 informations

Aside from re.Pattern and re.Match I'm not sure if there are other types in the stdlib that are not callable but also not ABC's, so this may be a special case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    reason: inexpressibleClosed, because this can't be expressed within the current type system

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions