Overloading a function with a parameter that can be either SomeType[Any] or Any always results in the return type Any
まだ誰も着手していません。
評価
調査の方向性
issue の overload 例から始め、mypy が SomeType[Any] のような overload と Any の overload のどちらを選択するかを追跡します。Callable/Any のケースを int/Any および Callable/int のケースと比較し、修正後の動作で互換性のない代入が報告される一方、既存の対照的なケースが維持されることを確認します。
索引モデルが issue の本文から書いたものです。
説明
Edit: I initially observed this behaviour for Callable[..., Any] and Any, but it now seems like this happens for any SomeType[Any] and Any, hence we've edited the title. For a more extensive explanation, scroll down to post 8.
I noticed that overloading a function with a Callable / Any parameter doesn't work as I'd expect. For example:
from typing import Any, Callable, overload
@overload
def foo(a: Callable) -> int: ...
@overload
def foo(a: Any) -> Any: ...
def foo(a):
pass
a: str = foo(lambda x: x)
I'd expect the mypy error Incompatible types in assignment (expression has type "int", variable has type "str"), but there's no error here.
Of note, if I replace Callable by something else, it works fine:
from typing import Any, overload
@overload
def foo(a: int) -> int: ...
@overload
def foo(a: Any) -> Any: ...
def foo(a):
pass
a: str = foo(1)
# mypy error: Incompatible types in assignment (expression has type "int", variable has type "str")
Finally, if I instead replace Any by something else, it also works fine.
from typing import Any, Callable, overload
@overload
def foo(a: Callable) -> int: ...
@overload
def foo(a: int) -> Any: ...
def foo(a):
pass
a: str = foo(lambda x: x)
# mypy error: Incompatible types in assignment (expression has type "int", variable has type "str")
So it only doesn't work as expected in the first code block. Is that a bug? Or is it expected behaviour somehow?
- 主要言語
- 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
-
area/auth bug comp/agent P3 platform/discord type/security
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
NousResearch/hermes-agent#117848 ·
-
難易度 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