-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Generic ClassVar Type failing #5144
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
Maybe it has something to do with:
See PEP 526 (https://www.python.org/dev/peps/pep-0526/#class-and-instance-variable-annotations) |
One comment is now have good stubs and a plugin for |
I agree the PEP does seem unnecessarily limiting. If you want to use a type variable at runtime, the idiom might look like: class ListWithElementType(List[T], Generic[T]):
element_type: ClassVar[Type[T]]
# methods that use the element_type at runtime via type(self).element_type
class IntList(ListWithElementType[int]):
element_type = int
The following case should fail. Maybe this is what they had in mind when writing that rule. class Bad(Generic[T]):
# This would bound T at declaration, so this makes no sense.
thing: ClassVar[T] = 0 But it's even more interesting than that, it's possible bounding T is okay iff the type of the initializing expression is the same type as the lower bound of the TypeVar. NumT = TypeVar("NumT", bound=Number)
class PossiblyBadButMaybeNotOneDay(Generic[NumT]):
# This would not be correct if NumT were inferred to be an `int` subtype,
# but it would be fine if it were inferred to be an `int` supertype like `float`, `complex`, etc.
# So in a future with TypeVar lower bounds,
# it would be okay if the lower bound was the same type as the initializer.
thing: ClassVar[NumT] = 0 |
This is exactly what I was looking to do, does the PEP need to be revised first for this to happen? |
Ditto--I'm not sure why this limitation was made in the first place. Of course it could be violated by an assignment expression, but for an abstract base class it's not such an unusual thing to do. I don't see any obvious reason in this case why
should be different from
It's OK if it doesn't have a value--we'll still be giving it one later. |
Here just to say that I've stumbled into this problem when defining abstract class, so I'd be happy if this restriction is lifted. Do you know if there is any issue/proposal to revise related pep? |
I changed the attrs plugin recently to correctly generate the (The proposed protocol: _T = TypeVar("_T")
class AttrsClass(Protocol[_T]):
__attrs_attrs__: ClassVar[_T] ) |
I believe that the reasoning given by @ktbarrett above makes sense. Anybody knows if there is any movement towards this? Any PEPs, discussions or anything? |
There has been some discussion in the python/typing discussion forum, but there hasn't been any recent movement toward eliminating this limitation. If you're interested in championing such a change, I recommend participating in the python/typing forums. |
@levkivskyi did comment on the matter earlier, and it does make sense. We need a PR to move forward. |
ClassVar annotations are not allowed to contain type variables. See python/mypy#5144. Additional type annotations are added to the put_queue and get_queue instance attributes to compensate.
With mypy
0.600
(and the latest mastereb1bb064d707ce05735ff6795df82126e75ea6ea
), running mypy onfails with
instead of allowing this to construct a
float
.The original use-case here is in the
ctypes
stubs, where mypy fails to derive the type of(ctypes.c_float * 5)._type_
-- see:https://github.com/python/typeshed/blob/9ec6d476c44729ea84e9cafde9c5a77610451b0c/stdlib/2and3/ctypes/__init__.pyi#L248-L250
The text was updated successfully, but these errors were encountered: