Skip to content
Advertisement

Tag: type-hinting

mypy declares IO[bytes] incompatible with BinaryIO

Consider the following code: Running mypy v0.782 on the above code under Python 3.6.9 fails with the following error: However, I feel that this code should not be regarded as an error, as ZipFile.open() returns a binary filehandle, which TextIOWrapper accepts. Moreover, IO[bytes] and BinaryIO are (as far as I understand) effectively the same thing; it’s just that BinaryIO is

Python how to type hint a Callable with __wrapped__

When passing around functions, I normally type hint them with typing.Callable. The docs for collections.abc.Callable state that it has four dunder methods: class collections.abc.Callable ABCs for classes that provide respectively the methods __contains__(), __hash__(), __len__(), and __call__(). At one point, I want to check if there is a __wrapped__ attribute on a function. This works fine at runtime via a

Type hints for a pandas DataFrame with mixed dtypes

I’ve been looking for robust type hints for a pandas DataFrame, but cannot seem to find anything useful. This question barely scratches the surface Pythonic type hints with pandas? Normally if I want to hint the type of a function, that has a DataFrame as an input argument I would do: What I cannot seem to find is how do

How to type hint a generic numeric type in Python?

Forgive me if this question has been asked before but I could not find any related answer. Consider a function that takes a numerical type as input parameter: This works with integers, floats and complex numbers. Is there a basic type so that I can do a type hinting (of a real existing type/base class), such as: Furthermore I need

Type hint for instance of subclass

I want to allow type hinting using Python 3 to accept instances which are children of a given class. I’m using the enforce module to check the function typing. E.g.: but it seems like python 3 doesn’t allow for this syntax, returning: Argument ‘x’ was not of type < class ‘A’ >. Actual type was B. Any help? Answer By

How to type-annotate object returned by csv.writer?

I want to apply type annotation to the returned object of csv.writer in order to comply with a larger codebase. Unfortunately I can not figure out the fitting return type. If I try to use this classname: I get the following mypy error: Does someone know which type to use in this case. Of course I could use typing.Any but

Advertisement