Skip to content
Advertisement

Tag: python-typing

Python annotate type as regex pattern

I have a dictionary annotation The value of time: will always be formatted like 2022-01-01 00:00:00, or “%Y-%m-%d %H:%M:%S”. I’d like a way to express this in the type annotation Something like WIth the goal of IDE hinting through VSCode Intellisense and Pylance. Are regex-defined type annotations supported? Answer Leaving out philosophical discussions about what should or should not be

Python generics: user defined generic in a callable

I have the following setup: This works great, but I’ve tied my Callable to str and int, and I would want something even more generic like being able to define func as something like: where A and B can be whatever, so I can define and send a func like (a: Type[str]) -> Type[int]:… or (a: Type[float]) -> Type[str]:… or

Subtype Generator with fixed type variables

Say I have a program with several generator functions that all have the return type Generator[<type>, <type2>, None] and I want to make an alias SimpleGenerator[<type>, <type2>] that is then expanded to the previous. So for example could be written I imagine the code might look something like (obviously incorrect python code) But I’m not finding an easy way of

Python – TypeHint for Descriptor

I would like to have a base class for all my descriptors which is of type descriptor: Is it correct to use the GetSetDescriptorType? Now when I declare A pycharm inspection complains: get() does not match signature. I have looked up the signature which is: Is it an error in inspection or is my declaration wrong? Answer No, using GetSetDescriptor

New union shorthand giving “unsupported operand type(s) for |: ‘str’ and ‘type'”

Before 3.10, I was using Union to create union parameter annotations: Now, when I use the new union shorthand syntax: I get the error: TypeError: unsupported operand type(s) for |: ‘str’ and ‘type’ Is this not supported? Answer The fact that it’s being used as a type hint doesn’t really matter; fundamentally the expression “Vector” | float is a type

Declare type annotation for optional dependency

Lets say I want to declare an input type originating from an optional dependency of my library. I have the option to set it as a string, at least to make Python happy (e.g. not complain about the missing name). But then Mypy will of course not know what to do about this missing type. Is there any solution, without

Advertisement