-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
mypy thinks properties on a class (not an instance) are callable #18490
New issue
Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “No 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? No Sign in to your account
Comments
hi @injust ! That's because the property decorator returns an object that has a Here is an alternative version of your example:
|
from typing import TYPE_CHECKING, cast
class Foo:
something = 1
@property
def bar(self) -> bool:
return True
reveal_type(Foo.bar) # note: Revealed type is "def (self: test.Foo) -> builtins.bool"
reveal_type(cast(Foo, None).bar) # note: Revealed type is "builtins.bool" This is confusing to me -- why is |
class Foo:
something = 1
@property
def bar(self) -> bool:
return True
print(Foo.bar.__get__(Foo())) # True
print(Foo.bar(Foo())) # TypeError: 'property' object is not callable
|
So, the The way that works is It means if you have a class that takes in parameters, you don't have to worry about what those parameters are or how to make them when you're in an In this case So at runtime, you'd want to say
Whilst a simplification (and without type annotations for brevity), the property decorator effectively means your Foo class looks like this
Essentially So when you have an instance of Foo and you access So when you're doing |
I realized only after I wrote this that |
Also, specifically for When you access |
This makes sense to me, and it explains why But I think mypy should be able to understand this and raise a type checking error, no? |
mypy is giving the correct errors in all of these examples. |
If I take my original example and run it through Pyright, it says that But mypy thinks everything is correct and does not show any errors. Am I missing something obvious here? |
@injust ah, I see, I missed that. You are correct :) |
Bug Report
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.13&gist=9f317c32db027f53db148d0b6899be82
Expected Behavior
The code should not type check.
Actual Behavior
Your Environment
The text was updated successfully, but these errors were encountered: