Stubgen: Instance Attributes set to `Incomplete` even though explicit declared
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the report with the shown Python example and stubgen ./foo_bar -o ., then inspect stubgen's handling of explicitly annotated instance attributes assigned from a method returning Any. Done means the generated stub contains test: str rather than importing and using Incomplete, without producing the reported syntax error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100