How to type hint dictionaries that may have different custom keys and/or values?
In the previous versions of our app, people would just pass some arguments with plain strings to certain functions, as we did not have specific type hinting or data types for some of them. Something like:
How to constrain type to be a union of subclasses in python?
I have a class Foobar
, with several subclasses, say FoobarAlpha
and FoobarBeta
Typing list[T] that allows T and derived classes Python MyPy
There is a problem that I have come across in python that the List type is invariant – meaning that it can only hold objects of a specific type or you will get a type error (for example when running mypy) – but sometimes you need to use a collection in a more generic function which can accept all types of a base class (lets call it A) as well as all derived classes. A clear example is this
Python generic protocol with method taking instance of the protocol (e.g. Functor)
With the current typing implementation, how far can we make this work as expected?
Is there any innate problem in this approach?
Typing with protocol with generic typed variable
I’m trying to create a Mixin that assumes the child class has a certain variable that is of a generic type.
I use a Protocol to define that constraint on the mixin but mypy doesn’t seem to be able to resolve it.
Here is the most minimal version of my could I could get that reproduces the issue: