I’m diving into type hints and type hinting some of my code as I think it is a good practice. I have a function that takes both sqlalchemy and cx_Oracle connection/session objects. I was able to figure out the hinting for sqlalchemy here. Poked around some docs trying to figure it out with cx_Oracle to no avail. My code In
Tag: type-hinting
What’s a good pattern for typehinting with `Literal` and then validating at runtime?
Let’s say I have a class: I would like to maintain the list of viable values, i.e. ‘floor’, ‘ceil’, ‘square’, in only one place. What’s a good recipe for this? The MyPy docs show some ideas with this suggestion of assert_never but that’s just for MyPy to give an error before runtime, not as a way to maintain a single
How do I use type hint for multiple classes in a dictionary but they inherit from same parent class?
I want to use type hint with string as a key and different classes as a value in a dictionary like below. The classes Dev, Product, Test inherit from a base class “Parent”. What is the best way to do the type hint for this? Do I need to set just “Any” as their type? Answer Depends on how accurate
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
python multiprocessing.Array typing
When creating an array, I want to put it inside a dataclass, but I cannot find the type of the returned object. arr = multiprocessing.RawArray(“i”, 2) If I do: but multiprocessing.sharedctypes.c_long_Array_2 does not exists. How can I use type hints, e.g arr: the_type with multiprocessing Array? UPDATE Pycharm example when using typing ctypes.c_long * 2, there’s still a value attribute
Python type hint for Iterable[str] that isn’t str
In Python, is there a way to distinguish between strings and other iterables of strings? A str is valid as an Iterable[str] type, but that may not be the correct input for a function. For example, in this trivial example that is intended to operate on sequences of filenames: Passing in a single filename would produce the wrong result but
Python: use Type Hints together with Annotations
In Python, we all know Type hints, which became available from 2015: and we also know Function Annotations, in particular here I am referring to textual annotations like: But is it possible to use Type Hints together with a textual function annotation? For example: This last one throws an error. I cannot seem to find any source in PEP or
type hints in a Python Google Cloud Function?
In a Python Google Cloud Function with a lot of sub-functions in the “main.py”, I added type hints (= return value annotation as part of function annotation) in pep8 style like this: Union is taken from here, it is needed if there is more than one type hint. The function cannot get deployed, there is no log about what is
Use a different fixed type for interfaces defined in the base class for subclasses
I have a BaseList container that takes BaseItem’s as items. I then derive a new list CustomList and I want it to hold CustomItem’s. How to I type methods in BaseList many methods to accept BaseItem. And also tell it to use CustomItem the derived list CustomList? Here is the code I have so far: Question How can I define
Type hint Pandas DataFrameGroupBy
How should I type hint in Python a pandas DataFrameGroupBy object? Should I just use pd.DataFrame as for normal pandas dataframes? I didn’t find any other solution atm Answer DataFrameGroupBy is a proper type in of itself. So if you’re writing a function which must specifically take a DataFrameGroupBy instance: If you’re looking for a more general polymorphic type, there