Relative Content

Tag Archive for pythonpython-typing

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’ […]