-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Class variable gets the type of instance variable #4886
Comments
So where is the contradiction here? The PEP doesn't say instance variables can't be set on the class. This is a relatively common pattern -- instance variable with a default (also mentioned in the PEP IIRC). The opposite however is unsafe, and therefor prohibited: class Test:
a: ClassVar[int]
Test().a = 1 # Error! |
@ilevkivskyi I think that @roganov's point is the reverse -- not that the member shouldn't be inherited by the instance, rather that the member should only be present on the instance. |
Ideally there should be a way to specify different types for class members and instance members. I tried to implement this using descriptors, but didn't succeed. |
The pep is slightly vague about this:
While this does indicate that it shouldn't be available on the class, it doesn't explicitly rule it out. The case from #11832 is very cut and dry though. Where the instance variables are declared within the class Foo:
def __init__(self) -> None:
self.bar: str
self.foo = 1
Foo.foo # no error
Foo.bar # no error |
According to PEP 526, type annotations inside class bodies apply to instances unless wrapped with
ClassVar
. Am I misreading something or is this a bug?The text was updated successfully, but these errors were encountered: