Relative Content

Tag Archive for pythonmypypython-typing

Coupling the Optionality of two types

Example Code I = TypeVar(“I”, bound=Optional[Iterable]) O = TypeVar(“O”, bound=Optional[Mappable]) class Worker(Generic[I, O]): @abstractmethod def do_work(self, input: I) -> O: pass worker = Worker[list, dict]() worker_with_optional = Worker[Optional[list], Optional[dict]]() worker_bad_types = Worker[Optional[list], dict]() The actual code in the example may seem a bit contrived, but it was the best way I could think to abstractly […]

Coupling the Optionality of two Generic types

Example Code I = TypeVar(“I”, bound=Optional[Iterable]) O = TypeVar(“O”, bound=Optional[Mappable]) class Worker(Generic[I, O]): @abstractmethod def do_work(self, input: I) -> O: pass worker = Worker[list, dict]() worker_with_optional = Worker[Optional[list], Optional[dict]]() worker_bad_types = Worker[Optional[list], dict]() The actual code in the example may seem a bit contrived, but it was the best way I could think to abstractly […]

eMypy(union-attr): mypy complains about missing attribute of object of type Optional even after checking that the argument is not None

I have a data class having as member a list of objects of another class having as member an object of type Optional[CustomType], and I need to call a method of CustomType class on that member, only when the member is not None.
Even if such check is explicitly performed, mypy still complains about the missing attribute of the item None of the CustomType | None union.