-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Generic class atributes aren't fully instantiated #2878
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
Comments
I don't think that the current semantics actually support that. Consider this:
The value of Mypy could work around this even with the current semantics by rejecting code like Also, attributes that use class type variables should only be accessible through an instance, not the class. So |
I agree this looks strange. One possible solution (first that comes to my mind) is to tweak descriptor
I think I agree. Effectively this means that in the proposed implementation of But what should we do with look-up of instance variables (those not wrapped in |
Prohibiting it seems to be an easy and safe solution. If we give it any normal type, this would make it possible to assign to the default:
This would break However, if we make it a read-only attribute when accessed through the class, we could sometimes use the upper bound type of the type variable, but that wouldn't work for types list I can't think of any use reasonable case where it would be important to access the default value of a instance variable that has a generic type variables in it's type through a type object. If you can come up with an example, it could be easier to see what we should do, other than rejecting it. |
My main concern is that the user who are not aware of instance/class variable distinction will be confused if their perfectly working code is rejected. The problem is finding a good error message.
(Btw the runtime issue in typing is now fixed) |
Implements ClassVar introduced in PEP 526. Resolves #2771, #2879. * Support annotating class attributes with ClassVar[...]. * Prohibit ClassVar annotations outside class bodies (regular variable annotations, function and method signatures). * Prohibit assignments to class variables accessed from instances (including assignments on self). * Add checks for overriding class variables with instance variables and vice versa when subclassing. * Prohibit ClassVars nested inside other types. * Add is_classvar flag to Var. * Add nesting_level attribute to TypeAnalyser and use it inside helper methods anal_type and anal_array. Move analyzing implicit TupleTypes from SemanticAnalyzer to TypeAnalyser. * Analyze ClassVar[T] as T and bare ClassVar as Any. * Prohibit generic ClassVars and accessing generic instance variables via class (see #2878 comments). * Add types.get_type_vars helper, which returns all type variables present in analyzed type, similar to TypeAnalyser.get_type_var_names.
Discovered while reviewing #2873.
The text was updated successfully, but these errors were encountered: