Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyright does not accept Pattern[AnyStr] #4534

Closed
agateau-gg opened this issue Jan 26, 2023 · 4 comments
Closed

pyright does not accept Pattern[AnyStr] #4534

agateau-gg opened this issue Jan 26, 2023 · 4 comments
Labels
as designed Not a bug, working as intended

Comments

@agateau-gg
Copy link

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.

@erictraut
Copy link
Collaborator

This won't work given the way Pattern is defined in the typeshed stub and the way that pyright implements constrained TypeVars. (See this documentation and this documentation for details.) The search overload contains annotated self parameters, and these do not match Pattern[AnyStr]. The typeshed stubs happen to work with mypy because of how mypy implements constrained TypeVars, but they won't work with pyright.

The fix for this would require an additional overload within the typeshed stubs. Something like this:

    @overload
    def search(self, string: AnyStr | ReadableBuffer, pos: int = ..., endpos: int = ...) -> Match[AnyStr] | None: ...

Please consider reporting this issue in the typeshed issue tracker.

@agateau-gg
Copy link
Author

Thanks, just opened the typeshed issue.

@erictraut
Copy link
Collaborator

erictraut commented Jan 28, 2023

Good news — the change was accepted into typeshed, and I've pulled the latest typeshed stubs into pyright. This issue will therefore be addressed in the next release of pyright.

@agateau-gg
Copy link
Author

Great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended
Projects
None yet
Development

No branches or pull requests

2 participants