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

@classmethod complaining about the first cls non-optional argument #7662

Closed
sam-goodwin opened this issue Apr 10, 2024 · 10 comments
Closed

@classmethod complaining about the first cls non-optional argument #7662

sam-goodwin opened this issue Apr 10, 2024 · 10 comments
Labels
as designed Not a bug, working as intended bug Something isn't working

Comments

@sam-goodwin
Copy link

Describe the bug
I can't seem to define a class method that accepts the cls parameter. PyRight flags it as a type error.

Code or Screenshots
If possible, provide a minimal, self-contained code sample (surrounded by triple back ticks) to demonstrate the issue. The code should define or import all referenced symbols.

class Foo:
    @classmethod
    def foo(cls):
        ...

I get this error:

Argument of type "(cls: type[Self@Foo]) -> None" cannot be assigned to parameter "f" of type "() -> _R_co@classmethod" in function "__init__"
  Type "(cls: type[Self@Foo]) -> None" cannot be assigned to type "() -> _R_co@classmethod"Pylance[reportArgumentType](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportArgumentType)

VS Code extension or command-line
Both CLI and VS Code have the same problem.

  • pyright 1.1.358
  • ms-python.vscode-pylance v2024.4.1
  • Python 3.10.13
@sam-goodwin
Copy link
Author

sam-goodwin commented Apr 10, 2024

Error goes away if i accept no arguments (as suggested in the type error)

image

image

@sam-goodwin
Copy link
Author

sam-goodwin commented Apr 10, 2024

Definitions of @classmethod in my environment.

class classmethod(Generic[_T, _P, _R_co]):
    @property
    def __func__(self) -> Callable[Concatenate[type[_T], _P], _R_co]: ...
    @property
    def __isabstractmethod__(self) -> bool: ...
    def __init__(self, f: Callable[Concatenate[type[_T], _P], _R_co], /) -> None: ...
    @overload
    def __get__(self, instance: _T, owner: type[_T] | None = None, /) -> Callable[_P, _R_co]: ...
    @overload
    def __get__(self, instance: None, owner: type[_T], /) -> Callable[_P, _R_co]: ...
    if sys.version_info >= (3, 10):
        __name__: str
        __qualname__: str
        @property
        def __wrapped__(self) -> Callable[Concatenate[type[_T], _P], _R_co]: ...

@erictraut
Copy link
Collaborator

I'm not able to repro this issue.

https://pyright-play.net/?pythonVersion=3.10&code=MYGwhgzhAEBiD28BcBYAUNT0ACpIQFsBTAFwAt4ATdLaSogM2gcQApQIBKVDWrAOkHogA

My best guess is that you have a really old, stale version of typing_extensions installed in your environment. Please try to repro with a new, clean virtual environment.

I'm pretty confident this isn't a bug in pyright, so I'm going to close the issue.

@erictraut erictraut added the as designed Not a bug, working as intended label Apr 10, 2024
@sam-goodwin
Copy link
Author

My typing_extensions is latest:

typing-extensions==4.11.0

@sam-goodwin
Copy link
Author

sam-goodwin commented Apr 10, 2024

There is something wrong with the Concatenate type in my setup.

If i monkey patch the classmethod's __init__.py function:

Replace this:

def __init__(self, f: Callable[Concatenate[type[_T], _P], _R_co], /) -> None: ...

With:

def __init__(self, f: Callable[..., _R_co], /) -> None: ...

Then the error goes away.

It looks like Concatenate is always resolving to [] on my machine and therefore always expects zero arguments.

My typesheds in VS Code come from:

.vscode/extensions/ms-python.vscode-pylance-2024.4.1/dist/typeshed-fallback/stdlib/builtins.pyi

But the same errors also happen in CLI which is installed by

pyright==1.1.358

@sam-goodwin
Copy link
Author

sam-goodwin commented Apr 10, 2024

PyLance is complaining about Concatenate[type[_T], _P]

Expected type expression but received "_ExtensionsSpecialForm"

@sam-goodwin
Copy link
Author

sam-goodwin commented Apr 10, 2024

Another monkey patch that makes the error go away is to import Concatenate from typing instead of typing_extensions in builtins.pyi.

Got the idea from: python/typing_extensions#48

@erictraut
Copy link
Collaborator

It sounds like something in your current environment is corrupt. Have you tried creating a clean virtual environment as I suggested above?

Do you have a typing-extensions stub library installed? Those would override the stubs that ship with pyright or pylance.

@sam-goodwin
Copy link
Author

Perhaps I misunderstood what you meant by clean environment. I cleaned it but re-produced it from my requirements.txt.

See here:
https://gist.github.com/sam-goodwin/6834dd69cb7a18d642f37b11a39d6918

If I dial this back to just pyright and no other dependencies, I do indeed see the error go away. Thanks for the lead, I will clean up my dependencies and try and zero in on the culprit.

@sam-goodwin
Copy link
Author

Tracked it down to include = ["py.typed"] in a transitive dependency. I am depending on this packyak package (which I own). Removing the py.typed from the wheel fixes the problem.

https://github.com/sam-goodwin/packyak/blob/fd882ec3252aba6894c4f61d74c12e1f0bbe1a03/pyproject.toml#L7

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 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants