`overload`s not recognised when `self` is typed as a `Protocol`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
When subclassing a base class with overloads, and self is typed as a Protocol, only the first overload is recognised.
To Reproduce
from datetime import timedelta
from typing import Generic, Protocol, TypeVar, overload, reveal_type
T = TypeVar("T")
T_CT = TypeVar("T_CT", contravariant=True)
class Mixin(Generic[T]):
@overload
def d(self: Mixin[timedelta], other: float) -> Mixin[timedelta]: ...
@overload
def d(self: Mixin[timedelta], other: timedelta) -> Mixin[float]: ...
reveal_type(Mixin[timedelta]().d(5.0)) # Mixin[timedelta], fine
reveal_type(Mixin[timedelta]().d(timedelta(days=1))) # Mixin[float], fine
class SupportsD(Protocol[T_CT, T]):
def d(self, other: T_CT, /) -> Mixin[T]: ...
class S(Mixin[T]):
def div(self: SupportsD[T_CT, T], other: T_CT) -> S[T]: ...
reveal_type(S[timedelta]().div(5.0)) # S[timedelta], fine
reveal_type(S[timedelta]().div(timedelta(days=1))) # mypy arg-type
Expected Behavior
S[timedelta]().div(timedelta(days=1)) is revealed as S[float]
Actual Behavior
ttest.pyi:24: error: Argument 1 to "div" of "S" has incompatible type "timedelta"; expected "float" [arg-type]
Your Environment
Mypy version used
1.18.2
Mypy command-line flags
none
Mypy configuration options from mypy.ini (and other config files):
[tool.mypy]
# Import discovery
namespace_packages = false
explicit_package_bases = false
ignore_missing_imports = true
follow_imports = "normal"
follow_imports_for_stubs = false
no_site_packages = false
no_silence_site_packages = false
# Disallow dynamic typing
disallow_any_unimported = false # TODO
disallow_any_expr = false # TODO
disallow_any_decorated = false # TODO
disallow_any_explicit = false # TODO
disallow_any_generics = false # TODO
disallow_subclassing_any = false # TODO
# Untyped definitions and calls
disallow_untyped_calls = false # TODO
disallow_untyped_defs = false # TODO
disallow_incomplete_defs = false # TODO
check_untyped_defs = true
disallow_untyped_decorators = true
# None and Optional handling
no_implicit_optional = true
strict_optional = true
# Configuring warnings
warn_redundant_casts = true
warn_unused_ignores = true
warn_no_return = true
warn_return_any = false # TODO
warn_unreachable = false # GH#27396
# Suppressing errors
ignore_errors = false
enable_error_code = "ignore-without-code" # same as in pandas
# Miscellaneous strictness flags
allow_untyped_globals = false
allow_redefinition = false
local_partial_types = false
implicit_reexport = false # pyright behaves the same
strict_equality = true
# Configuring error messages
show_error_context = false
show_column_numbers = false
show_error_codes = true
Python version used:
3.12.10
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die bereitgestellte Reproduktion mit Python 3.12.10 und mypy 1.18.2 auszuführen, wobei der Schwerpunkt auf der Overload-Auflösung für Mixin.d liegt, wenn self durch SupportsD betrachtet wird. Verfolge die Verarbeitung der beiden Overloads und der Protocol-Typisierung; abgeschlossen ist die Aufgabe, wenn der timedelta-Aufruf akzeptiert und als S[float] aufgelöst wird, während der bestehende float-Fall S[timedelta] bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100