Callable with Iterator[T] return type and default function with compatible return type, errors
まだ誰も着手していません。
評価
調査の方向性
まず、提供された Python スニペットを mypy 0.812 で実行し、foo のジェネリックな reader パラメーターと baz のデフォルト値を比較します。mypy がデフォルトの callable をジェネリックな戻り値型に対してどのようにチェックするかを追跡します。baz が非互換エラーなしで csv.reader を受け入れ、foo の既存の動作を維持できれば完了です。
索引モデルが issue の本文から書いたものです。
説明
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
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]
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 version used: 0.812
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files):
[mypy]
# Most copied from https://github.com/python/mypy/blob/675ae1afc8a5b0a9a515292c7e9d6bc2a38887be/mypy_self_check.ini
disallow_untyped_calls = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
check_untyped_defs = True
disallow_subclassing_any = True
warn_no_return = True
strict_optional = True
strict_equality = True
no_implicit_optional = True
disallow_any_generics = True
disallow_any_unimported = True
warn_redundant_casts = True
warn_unused_ignores = True
warn_unused_configs = True
show_traceback = True
show_error_codes = True
# Should be removed if we use typehints everywhere
follow_imports = silent
python_version = 3.9
[mypy-memory_profiler.*]
ignore_missing_imports = True
- Python version used: 3.9.1
- Operating system and version: Ubuntu 20.10
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- 平均マージ
- 1日 18時間
- マージ済み PR(30日)
- 54
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/mypy のほかの issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
bug topic-configuration topic-error-reporting
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
bancolombia/sentinel#23 ·
-
test md オープンCI
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
langchain-ai/deepagents#6450 ·
-
bug client
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100