Assignment to Any doesn't make it more precise
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
- Are you reporting a bug, or opening a feature request?
Bug report.
- Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
from typing import Any, Optional
def foo(baz: Any) -> int:
return 1
def bar1(baz: Any = 0) -> int:
reveal_type(baz)
baz = foo(baz)
reveal_type(baz)
return baz
def bar2(baz: Optional[Any] = 0) -> int:
reveal_type(baz)
baz = foo(baz)
reveal_type(baz)
return baz
- What is the actual behavior/output?
main.py:9: note: Revealed type is 'Any'
main.py:11: note: Revealed type is 'Any'
main.py:12: error: Returning Any from function declared to return "int"
main.py:16: note: Revealed type is 'Union[Any, None]'
main.py:18: note: Revealed type is 'builtins.int'
Found 1 error in 1 file (checked 1 source file)
- What is the behavior/output you expect?
After baz = foo(baz) the type of baz should be int. Redefinition should not be a problem here since this assignment makes the type of baz more precise.
By trial-and-error I found that annotating bar as a Union of Any and anything else (here Optional[Any] == Union[Any, None]) fixes this behavior.
- What are the versions of mypy and Python you are using?
Do you see the same issue after installing mypy from Git master?
mypy: 0.780 / 0.790+dev.95eff27adb1fae7b1ec4a7315524ebfdcebf6e2b
python: 3.6.9
- What are the mypy flags you are using? (For example --strict-optional)
--warn-return-any
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 présenté sous le nom de main.py et exécutez mypy avec --warn-return-any ; comparez la sortie de reveal_type avant et après baz = foo(baz). Le travail est terminé lorsque l’affectation est traitée comme un rétrécissement de baz vers int et que l’erreur return-any n’est plus signalée, tout en conservant la compréhension du comportement existant de Optional[Any].
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
- 35/100