How to annotate the type of arguments forwarded to another function?
Let’s say we have a trivial function that calls open()
but with a fixed argument:
Generic type that acts like base type
I want to create a generic type A[T]
that acts exactly like T
, except that I can tell at runtime that the type is in fact A[T]
and not T
.
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 […]
Type a Callable so it can take any number of string arguments, and then keyword arguments
I have a function which accepts any function such that:
Mypy: type `Callable` so it can take any number of `str` arguments, and then keyword arguments
I have a function which accepts any function such that:
How to make override decorator mandatory?
I came across new override decorator for Python 3.12 and it looks like an extremely good practice.
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.
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.
`python`: what determines the order of type variables when narrowing a generic type?
Suppose I have: