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

Aliased staticmethod leads to incorrect attribute assignment #9820

Closed
sharkdp opened this issue Feb 5, 2025 · 1 comment
Closed

Aliased staticmethod leads to incorrect attribute assignment #9820

sharkdp opened this issue Feb 5, 2025 · 1 comment
Labels
as designed Not a bug, working as intended bug Something isn't working

Comments

@sharkdp
Copy link

sharkdp commented Feb 5, 2025

Describe the bug

In the following example, the aliased staticmethod decorator is not recognized properly and leads Pyright to believe that C instances have an attribute x.

While searching for existing issues, I found this ticket where it is explained that the "@staticmethod and @classmethod decorators […] require significant special casing in Python static type checkers". I understand that this might be difficult to implement. I do not rely on this behavior and just found it while testing edge-cases. Feel free to close this ticket without comment if this is out of scope for Pyright.

Code or Screenshots

my_staticmethod = staticmethod

class Unrelated:
    x: int | None

class C:
    @my_staticmethod
    def f(unrelated: Unrelated):
        unrelated.x = 1

reveal_type(C().x)  # Pyright reports `int` here, but this attribute does not exist

(Open in Playground)

VS Code extension or command-line

Pyright 1.1.393

@erictraut
Copy link
Collaborator

This is expected behavior. Static type checkers like pyright and mypy do not generally handle aliases to staticmethod or classmethod. These decorators need to be processed early in the analysis — during the "binder" phase — when symbol tables are being constructed. This phase occurs prior to any type evaluation, which depends on the symbol tables.

If you want your code to work with static analysis tools, you'll need to avoid aliasing staticmethod and classmethod.

@erictraut erictraut closed this as not planned Won't fix, can't repro, duplicate, stale Feb 5, 2025
@erictraut erictraut added the as designed Not a bug, working as intended label Feb 5, 2025
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