I have looked at a few SO posts and github issues related to using Typing with descriptors but I have not been able to get my issue resolved.
I have wrapper classes and I want to define properties as descriptos that can get and "cast" properties of an internal data structure.
class DataDescriptor(object):
def __init__(self, name: str, type_):
self.name = name
self.type_ = type_
def __get__(self, instance, cls):
if not instance:
raise AttributeError("this descriptor is for instances only")
value = getattr(instance._data, self.name)
return self.type_(value)
class City(object):
zip_code: str = DataDescriptor("zip_code", str)
# mypy: Incompatible types in assignment
population: float = DataDescriptor("population", float)
# mypy: Incompatible types in assignment
def __init__(self, data):
self._data = data
class InternalData:
# Will be consumed through city wrapper
def __init__(self):
self.zip_code = "12345-1234"
self.population = "12345"
self.population = "12345"
data = InternalData()
city = City(data)
assert city.zip_code == "12345-1234"
assert city.population == 12345.0
I thought I might be able to use TypeVar but I haven't been able to wrap my head around it.
This is what I have tried - I thought I could dynamically describe that the descriptor will take a "type", and this type is also the type __get__
will return. Am I on the right track?
from typing import TypeVar, Type
T = TypeVar("T")
class DataDescriptor(object):
def __init__(self, name: str, type_: Type[T]):
self.name = name
self.type_ = type_
def __get__(self, instance, cls) -> T:
if not instance:
raise AttributeError("this descriptor is for instances only")
value = getattr(instance._data, self.name)
return self.type_(value)
# Too many arguments for "object"mypy(error)