Skip to content
Advertisement

Tag: type-hinting

cx_Oracle connection type hinting in python

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

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 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

Advertisement