I am doing some input experiments in Python 3.6. I want to create an entity class that can be created in two ways:
- Using a regular initializer (
p = Person(name='Hannes', age=27) ) - Statically from a state object (
p = Person.from_state(person_state) ).
The Entity class from which Person derives has a state class as a generic parameter. However, when checking the code with pypy, I get an error that Person # from_state is not getting the state type from the class that it inherits:
untitled2.py:47: error: argument 1 "from_state" in "Entity" has an incompatible type "UserState"; expected "StateType"
I thought that inheriting from Entity[UserState] , StateType would be bound to UserState , and method signatures in child classes would be updated accordingly.
This is the complete code. I marked a line where, as I suspect, I am doing something wrong with ????? . Line 47 is almost at the bottom and marked in the code.
from typing import TypeVar, Generic, NamedTuple, List, NewType EntityId = NewType('EntityId', str) StateType = TypeVar('StateType') class Entity(Generic[StateType]): id: EntityId = None state: StateType = None @classmethod def from_state(cls, state: StateType):
source share