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
Tag: python-typing
TypeScript vs. Python typing default arguments and autocompletion
In TypeScript I used to have the following and it will work as type inference: The above example should give you options and autocompletion when typing, and error when you type options that are not in the list of options. When I was exploring Python I found the typing module. But what I tried didn’t work, and I couldn’t find
How to access `ApplyResult` and `Event` types in the multiprocessing library
I’ve written a working wrapper around the python multiprocessing code so I can easily start, clean up, and catch errors in my processes. I’ve recently decided to go back and add proper type hints to this code, however I can’t figure out how to use the types defined in multiprocessing correctly. I have a function which accepts a list of
How to annotate a type that is a combination of multiple duck-types?
Let’s say I want to annotate the parameter to a function, and it should satisfy both typing.Sized and typing.Hashable (any other 2+ types could apply, I just picked these two for the sake of example). How would I annotate this? Given that one can combine types as an “or” using Sized | Hashable, I would expect something like: Sized &
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
Is there a way to get a type from a function that takes a string for an argument?
I have a function that takes a single string arg. I think in my first post of this issue I was not clear enought. The purpose of this question is to figure how how to get typing (autocomplete) support for such methods without using cast. doc = CreateScritpService(“Calc”) To get the type I use cast. doc = cast(SFDocuments.SF_Calc, CreateScritpService(“Calc”)) bas
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