You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to add some type hints to a module that takes a filename and reads it with a caller supplied reader (most common csv.reader and csv.DictReader). I noticed when I do not supply a default, mypy is fine, however, when I do, it complains about incompatibility.
To Reproduce
importcsvfromtypingimportCallable, Iterable, Iterator, Text, TypeVarT=TypeVar('T')
deffoo(x: str, reader: Callable[[Iterable[Text]], Iterator[T]]) ->Iterator[T]:
returnreader(x)
# Is fine, resolves to Iterator[List[str]]x=foo('bar.txt', csv.reader)
# Complains that _reader is not compatible with Iterator[T]defbaz(x: str, reader: Callable[[Iterable[Text]], Iterator[T]] =csv.reader) ->Iterator[T]:
returnreader(x)
I had originally identified a work-around to a similar case in #10854 (comment). Unfortunately, that no longer works, but I was able to devise a new one using None as the default argument. (See #10854 (comment)et seq.)
Bug Report
I am trying to add some type hints to a module that takes a filename and reads it with a caller supplied reader (most common csv.reader and csv.DictReader). I noticed when I do not supply a default, mypy is fine, however, when I do, it complains about incompatibility.
To Reproduce
When I look at the _reader definition I see
So it is a subclass of
Iterator[List[str]]
which should be compatible withIterator[T]
Expected Behavior
No mypy error is shown
Actual Behavior
mypy complains about using csv.reader as the default value of the reader argument due to it not matching
_reader
.Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: