Skip to content

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

Closed
@efagerberg

Description

@efagerberg

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions