Replies: 3 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. |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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:
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.
Something like this:
Is there any way to specify this to people subclassing my class?
Assuming this example code does what I want it to do, and represents it in a way I want, how can I specify the typing?
Beta Was this translation helpful? Give feedback.
All reactions