- Notifications
You must be signed in to change notification settings - Fork263
-
I don't think this is standard terminology. There may be a better way of describing what I'm talking about. I'm seeing that Python generics are instance-level generics - that is, each instance could have a different type for the type variable: CT=TypeVar("CT")classC(Generic[CT]):x:CTdef__init__(self,x:CT)->None:self.x=xdefmain()->None:a=C[int](1)y=a.x+1print(y)b=C[str]("1")z=b.x+"1"print(z) What I'm looking for is a way to specify that a subclass should specify a type that isn't specified by the base class. PT=TypeVar("PT")classP(Generic[PT]):x:ClassVar[PT]# type error: "ClassVar" type cannot include type variablesclassQ(P[int]):x=3classR(P[str]):x="three"deff(p:Type[P])->None:print(p.x)f(Q)f(R) Is there any way to specify this to people subclassing my class? |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 4 comments 4 replies
-
Something like this maybe? A class variable is like an instance variable of a metaclass. Although different type checkers have different support for this. For pyright it works, for mypy generic metaclasses don't seem to work. |
BetaWas this translation helpful?Give feedback.
All reactions
-
I'm getting |
BetaWas this translation helpful?Give feedback.
All reactions
-
Interesting. I'm unable to reproduce that with pyright/pylance. I tried pyright latest version and code type checks fine for me.pyright playground also type checks fine. |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Whenever you are expecting a subclass to provide something, using https://mypy-play.net/?mypy=latest&python=3.11&gist=617d24caceb1069f05fd252ca2fe97d2 As you can see an abstract property with a setter can be provided by a ClassVar, since it provides the same interface. It maybe obfuscates the intent a bit and is a lot of boilerplate for one attribute, but it does work. You may also wish to use the |
BetaWas this translation helpful?Give feedback.
All reactions
-
Actually it looks like this doesn't enforce the property being defined in subclasses, although I'm not sure why. I'm certain I managed to get abstract properties to work this way in the past. |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Alright, mypy will only emit an error if you try to create an instance of a subclass with abstract properties/methods that have not been implemented. It will automatically consider the class abstract, regardless of whether it's using So technically this will catch the error, just not at the definition site of the subclass, but only once you attempt to use the subclass: |
BetaWas this translation helpful?Give feedback.
All reactions
-
Note that if you type ignore that error, mypy will still mostly do what you want:https://mypy-play.net/?mypy=latest&python=3.11&gist=fbe0cc42fcb250d60663d3e25567fa28 |
BetaWas this translation helpful?Give feedback.
All reactions
-
The type check will be lost. |
BetaWas this translation helpful?Give feedback.