A type can be consistent subtype of distinct materializations of a generic protocol
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 38/100
Piste de recherche
Exécutez la reproduction fournie avec mypy --strict et examinez comment le narrowing de TypeIs gère les deux matérialisations génériques de Protocol. Ajoutez un test de régression couvrant check1 et check2, puis vérifiez que les types de retour inférés et le comportement des assertions correspondent aux résultats attendus.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Bug Report
When using TypeIs to narrow a type down to two different fully static materializations of the same generic Protocol, MyPy will infer the result to be Never, thus producing false assertions.
To Reproduce
from typing import Any, Literal, Protocol, TypeIs
# ==== Basic definitions ====
class MockProtocol[T](Protocol): # T is inferred to be contravariant
def __call__(self, t: T) -> None:
...
class MockClass():
def __call__(self, t: int | str) -> None: # Implements MockProtocol[int | str]
pass
# ==== Basic static type checking ====
def only_accept_mock_int(obj: MockProtocol[int]) -> None:
pass
def only_accept_mock_str(obj: MockProtocol[str]) -> None:
pass
only_accept_mock_int(MockClass()) # MockClass is subtype of MockProtocol[int]
only_accept_mock_str(MockClass()) # MockClass is subtype of MockProtocol[str]
# ==== Runtime type checking involving TypeIs ====
def is_mock_int(obj: Any) -> TypeIs[MockProtocol[int]]:
return isinstance(obj, MockClass) # Mock implementation
def is_mock_str(obj: Any) -> TypeIs[MockProtocol[str]]:
return isinstance(obj, MockClass) # Mock implementation
def check1(obj: MockClass) -> Literal[True]: # Won't complain because `return False` is unreachable
if is_mock_str(obj) and is_mock_int(obj):
return True
return False # Inferred to be unreachable
def check2(obj: MockProtocol[str]) -> Literal[False]: # Won't complain because `return True` is unreachable, which is incorrect!
if is_mock_str(obj) and is_mock_int(obj):
return True # Inferred to be unreachable
return False
result1 = check1(MockClass()) # Inferred to be Literal[True]
result2 = check2(MockClass()) # Inferred to be Literal[False], but the value is True!!!
assert result1 == result2 # Inferred to fail consistently, but it should succeed
Expected Behavior
check2 should be inferred as Callable[..., bool] because a fully static type can be subtype of both MockProtocol[str] and MockProtocol[int].
Actual Behavior
Literal[False] is recognized as a compatible return type of check2 due to false reachability assumption, causing the type of result2 to be wrong.
Your Environment
- Mypy version used: 1.18.1
- Mypy command-line flags:
mypy --strict - Mypy configuration options from
mypy.ini(and other config files): (default) - Python version used: Python 3.13.8
- Langage dominant
- Python
- Étoiles
- 20.6k
- Forks
- 3.3k
- Merge moyen
- 1 j 18 h
- PR mergées (30 j)
- 54
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de python/mypy
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
-
documentation
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
-
bug topic-configuration topic-error-reporting
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
Toutes les issues de python/mypy
Issues similaires
-
area/auth bug comp/agent P3 platform/discord type/security
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
NousResearch/hermes-agent#117848 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
bancolombia/sentinel#23 ·
-
test md OuverteCI
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100