Issue when `self` is typed as a `Protocol`
Offen
Dieses Issue hat noch niemand übernommen.
bug
topic-protocols
topic-self-types
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
mypy does not recognise typing in self properly when it comes to a Protocol in the following example.
To Reproduce
ttest.pyi:
from typing import Generic, Protocol, TypeVar, reveal_type
T = TypeVar("T")
T_CT = TypeVar("T_CT", contravariant=True)
T_CO = TypeVar("T_CO", covariant=True)
class OpsMixin(Generic[T]):
def _add(self: OpsMixin[int], other: bool) -> OpsMixin[int]: ...
class Supports_Add(Protocol[T_CT, T_CO]):
def _add(self, other: T_CT, /) -> T_CO: ...
class S(OpsMixin[T]):
def __add__(self: Supports_Add[T_CT, T], other: T_CT) -> S[T]: ...
reveal_type(S[bool]() + True) # mypy: S[bool], pyright: Error / Unknown
Expected Behavior
S[bool]() + True should be undefined.
Actual Behavior
ttest.pyi:16: note: Revealed type is "ttest1.S[builtins.bool]"
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
Beginnen Sie mit dem ttest.pyi-Reproduzierer und führen Sie ihn mit mypy 1.18.2 unter Python 3.12.10 und der im Issue gezeigten Konfiguration aus. Verfolgen Sie, wie der Typechecker self-Annotationen behandelt, die Protocol, Generic und add betreffen; abgeschlossen, wenn Sbool + True wie erwartet abgelehnt wird, anstatt S[bool] zu enthüllen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100