Skip to content

_csv.pyi's _reader does not satisfy Iterator[T] #5497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
efagerberg opened this issue May 18, 2021 · 3 comments
Closed

_csv.pyi's _reader does not satisfy Iterator[T] #5497

efagerberg opened this issue May 18, 2021 · 3 comments

Comments

@efagerberg
Copy link

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.

Python: 3.9.1

import csv
from typing import Callable, Iterable, Iterator, Text, TypeVar

T = TypeVar('T')

def foo(x: str, reader: Callable[[Iterable[Text]], Iterator[T]]) -> Iterator[T]:
    return reader(x)


# Is fine, resolves to Iterator[List[str]]
x = foo('bar.txt', csv.reader)


# Complains that _reader is not compatible with Iterator[T]
def baz(x: str, reader: Callable[[Iterable[Text]], Iterator[T]] = csv.reader) -> Iterator[T]:
    return reader(x)

When I look at the _reader definition I see

class _reader(Iterator[List[str]]):
    dialect: Dialect
    line_num: int
    if sys.version_info >= (3, 0):
        def __next__(self) -> List[str]: ...
    else:
        def next(self) -> List[str]: ...

So it is a subclass of Iterator[List[str]] which should be compatible with Iterator[T]

@JelleZijlstra
Copy link
Member

I'm not sure this is a typeshed issue; I think it's about mypy not doing well with typevars + default values.

@efagerberg
Copy link
Author

@JelleZijlstra perhaps, wanted to make sure I wasn't missing anything in the _reader definition. But I suspect you are right.

@efagerberg
Copy link
Author

Closing in favor of python/mypy#10504

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants