Stubgen: Instance Attributes set to `Incomplete` even though explicit declared
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
I am using Stubgen to type my files.
Here, I have a class with a lot of instance attributes.
They are all declared within the __init__ method.
They are set using a method which returns Any as type, but manually set to the correct type:
When generating the stub file, all are set to Incomplete instead of the highlighted code.
Trying to use # type: str behind it results in a error: Critical error during semantic analysis: foo/bar.py:424242: error: invalid syntax [syntax]
To Reproduce
from typing import Any
class MyClass:
def __init__(self) -> None:
bar = Bar("test")
self.test: str = bar.foo("test")
class Bar:
def __init__(self, anything: str) -> None:
pass
def foo(self, test: str) -> Any:
return test
results in:
from _typeshed import Incomplete
from typing import Any
class MyClass:
test: Incomplete
def __init__(self) -> None: ...
class Bar:
def __init__(self, anything: str) -> None: ...
def foo(self, test: str) -> Any: ...
Gist URL: https://gist.github.com/27dfb95516013fa15ed80c1411ce5a4e
Playground URL: https://mypy-play.net/?mypy=latest&python=3.9&gist=27dfb95516013fa15ed80c1411ce5a4e
Expected Behavior
I want the attribute to have the correct, annotated type.
from typing import Any
class MyClass:
test: str
def __init__(self) -> None: ...
class Bar:
def __init__(self, anything: str) -> None: ...
def foo(self, test: str) -> Any: ...
Actual Behavior
It is exported as incomplete
from _typeshed import Incomplete
from typing import Any
class MyClass:
test: Incomplete
def __init__(self) -> None: ...
class Bar:
def __init__(self, anything: str) -> None: ...
def foo(self, test: str) -> Any: ...
Your Environment
- Mypy version used: mypy 1.0.0 (compiled: yes)
- Mypy command-line flags: stubgen ./foo_bar -o .
- Mypy configuration options from
mypy.ini(and other config files): None used - Python version used: Python 3.10.9
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 reproduire le rapport avec l’exemple Python montré et stubgen ./foo_bar -o ., puis examinez la gestion par stubgen des attributs d’instance explicitement annotés auxquels est affecté le résultat d’une méthode renvoyant Any. Le travail est terminé lorsque le stub généré contient test: str au lieu d’importer et d’utiliser Incomplete, sans produire l’erreur de syntaxe signalée.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- tooling
- Type d'issue
- Bug
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 45/100