Declaration-order-dependent possibly-undefined false positive with nested ifs/matches, decorated any functions and NoReturn
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 20.6k
- Forks
- 3.3k
- Métriques de merge des PR
- Métriques de PR en attente
Description
Bug Report
Some obtuse code results in a false positive for the possibly-undefined error code.
To Reproduce
Run mypy --enable-error-code possibly-undefined file.py on this file:
decorator = lambda f: f
def unreachable() -> None:
raise NotImplementedError()
def do_it(x: bool, y: None) -> None:
if x:
decorated()
var = 0
else:
match y:
case None:
var = 1
case _:
unreachable()
print(var)
@decorator
def decorated() -> None:
pass
This is as small as I've gotten the reproducer. All these parts seem to be significant:
- the outer
ifand an innermatch - the
decoratedcall being separate to thematch - the
matchbeing amatch(replacing thematchwith the equivalentif y is None: ... else: ...construct works fine) - the
matchhaving exhaustive arms with theunreachablecall being unreachable (as far as mypy can tell) (e.g. deleting thecase None:entirely works fine) - the
unreachablecall existing (inliningcase _: raise NotImplementedError()works fine) decoratorbeing a lambda (or an class with__call__method) (usingdef decorator(f): return fworks fine)decoratedbeing defined afterdo_itin the file (changing the order works fine) (NB. it's justdecorated,decoratorandunreachablecan be defined before or afterdo_itand still reproduce the behaviour)
Expected Behavior
This to pass type checking.
In particular, have same behaviour as any of the very similar ablations mentioned above, e.g. both of these work:
- changing
decorator = lambda f: ftodef decorator(f): return f - moving
decoratedto be beforedo_it
Actual Behavior
file.py:19: error: Name "var" may be undefined [possibly-undefined]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.11.2
- Mypy command-line flags:
--enable-error-code possibly-undefined - Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.10
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.
Piste de recherche
Commencez par le reproducteur minimal file.py et exécutez mypy --enable-error-code possibly-undefined pour confirmer le diagnostic signalé. Comparez les ablations listées, notamment la définition du décorateur, l’ordre des déclarations et les branches match, afin d’isoler l’interaction. C’est terminé lorsque le reproducteur passe la vérification de types sans le faux positif.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- devtools
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 42/100