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
Tag: type-hinting
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
Why do I get a warning when concatenating lists of mixed types in Pycharm?
In Pycharm, the following code produces a warning: Why? Should I not be concatenating two lists of mixed, hinted types? Answer As requested in the comments, here are some reasons why type checkers don’t allow this. The first reason is somewhat prosaic: the type signature of list.__add__ simply doesn’t allow for anything other then a list containing the same type
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
What’s the correct way to check if an object is a typing.Generic?
I’m trying to write code that validates type hints, and in order to do so I have to find out what kind of object the annotation is. For example, consider this snippet that’s supposed to tell the user what kind of value is expected: This should print “value type should be one of (int, str)”, but instead it throws an
Python type annotation for sequences of strings, but not for strings?
Is there a Python type hint that matches lists, tuples and possibly other sequential types, but does not match strings? The issue is that strings are at the same time sequences of strings of length 1 (e.g. individual characters), so they technically match the Sequence[str], but providing a string to a function expecting a list of strings is an error
Type hints with user defined classes
Couldn’t seem to find a definitive answer. I want to do a type hint for a function and the type being some custom class that I have defined, called it CustomClass(). And then let’s say in some function, call it FuncA(arg), I have one argument named arg. Would the correct way to type hint FuncA be: Or would it be: