annotate a NamedTuple for storing a function and its args and kwargs
I have a system where i want to store a function along with its arguments for later invocation. I have tried:
Python type hinting not generating error for wrong type when running the code
I was recently checking about type hinting and after reading some theory I tried a simple example:
How to define a nested generic type in Python?
I have a function that (should) flatten arbitrarily many times nested list
How to annotate different iterable types with same items’ type?
For example:
How to use python typing TypeVar as a Callable list of parameters?
Lets say I want a function that execute a function with its parameters (yes, this is useless, but also the simplest case I could think of) :
Is typing.assert_never() removed by command line option -O, similar to assert statement?
In Python, assert
statement produces no code if command line optimization options -O
or -OO
are passed. Does it happen for typing.assert_never()
? Is it safe to declare runtime assertions that will not be optimized out?
Creating a default value recursively given a type (types.GenericAlias)
Given a type t
(originally comes from function annotations), I need to create a default value of that type. Normally, t()
will do just that and work for many types, including basic types such as bool
or int
. However, tuple[bool, int]()
returns an empty tuple, which is not a correct default value. It can get slightly trickier with more complex types such as tuple[bool, list[int]]
.
How annotate type function?
@classmethod def get_serializer_class( cls, skip_hidden: bool = False ) -> type[serializers.Serializer]: fields = {} for filter_name, filter_field in cls.filters.items(): if skip_hidden and filter_field.hidden: continue fields[filter_name] = filter_field.serializer_field serializer_class = type( f'{cls.__name__}Serializer’, (cls._meta.serializer_class,), fields) return serializer_class What type should I specify for the serializer_class variable? Now the linter shows an error: Expected type ‘Type[Serializer]’, got ‘type’ […]
How to specify intersection types in python?
Let’s imagine that we have 2 protocols: HasSize
and HasLength
. For domain reasons these may mean different things, but in some cases you want to adapt one to the other:
Annotate a constructor that delegates to another function
I have a Python library that encapsulates an external c++ library that exposes its objects via handles. I have a root class