Skip to content

pyright does not accept Pattern[AnyStr] #4534

@agateau-gg

Description

@agateau-gg

Describe the bug

pyright does not accept Pattern[AnyStr].

To Reproduce

Given this test file:

from typing import AnyStr

from re import Match, Pattern

def check_re_search(pattern: Pattern[AnyStr], string: AnyStr) -> Match[AnyStr]:
    match = pattern.search(string)
    if match is None:
        raise ValueError(f"'{string!r}' does not match {pattern!r}")
    return match

pyright reports the following error:

No configuration file found.
No pyproject.toml file found.
stubPath /home/agateau/tmp/typings is not a valid directory.
Assuming Python platform Linux
Searching for source files
Found 1 source file
pyright 1.1.291
/home/agateau/tmp/crs.py
  /home/agateau/tmp/crs.py:6:21 - error: Could not bind method "search" because "Pattern[AnyStr@check_re_search]" is not assignable to parameter "self"
    "Pattern[AnyStr@check_re_search]" is incompatible with "Pattern[str]"
      TypeVar "AnyStr@Pattern" is invariant
        Type "AnyStr@check_re_search" cannot be assigned to type "str" (reportGeneralTypeIssues)
  /home/agateau/tmp/crs.py:6:21 - error: Could not bind method "search" because "Pattern[AnyStr@check_re_search]" is not assignable to parameter "self"
    "Pattern[AnyStr@check_re_search]" is incompatible with "Pattern[bytes]"
      TypeVar "AnyStr@Pattern" is invariant
        Type "AnyStr@check_re_search" cannot be assigned to type "bytes" (reportGeneralTypeIssues)

The same code works fine if one replaces all AnyStr with either str or bytes.

Expected behavior

pyright should accept the example code.

VS Code extension or command-line

pyright 1.1.291, running from the command-line.

Metadata

Metadata

Assignees

No one assigned

    Labels

    as designedNot a bug, working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions