mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Stubgen: Instance Attributes set to `Incomplete` even though explicit declared

Open NielsKorschinsky opened this issue 2 years ago • 3 comments

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

NielsKorschinsky avatar Feb 10 '23 18:02 NielsKorschinsky

I see the same issue

  • python 3.11.0
  • mypy 1.0.1

prokie avatar Mar 06 '23 16:03 prokie

Same

  • python 3.11.4
  • mypy 1.4.1

nephi-dev avatar Jul 21 '23 15:07 nephi-dev

Has there been any solutions to this? Besides having to write the stubs manually that is.

ACE07-Sev avatar Apr 28 '24 16:04 ACE07-Sev

Same issue here... Using Python 3.12.3 and mypy 1.11.0+dev.16d5.

I've noticed that when using stubgen to generate stub files, instance attributes are sometimes set to _typeshed.Incomplete, even when they are explicitly declared in the class. This seems to occur when the attribute's type cannot be inferred from its usage in the class body.

For example, consider the following class:

from pathlib import Path
from typing import List

class FileProcessor:
    def __init__(self, filename: Path) -> None:
        self.filename: Path = filename

    def read_lines(self) -> List[str]:
        with self.filename.open() as file:
            return file.readlines()

    def write_lines(self, lines: List[str]) -> None:
        with self.filename.open("w") as file:
            file.writelines(lines)

When stubgen is used to generate a stub file for this class, the filename attribute is set to _typeshed.Incomplete in the class body, even though it is explicitly declared as a Path in the __init__ method:

from _typeshed import Incomplete
from pathlib import Path

class FileProcessor:
    filename: Incomplete
    def __init__(self, filename: Path) -> None: ...
    def read_lines(self) -> list[str]: ...
    def write_lines(self, lines: list[str]) -> None: ...

According to the docs - https://mypy.readthedocs.io/en/stable/stubgen.html#automatic-stub-generation-stubgen - it says the following:

Stubgen generates draft stubs. The auto-generated stub files often require some manual updates, and most types will default to Any. The stubs will be much more useful if you add more precise type annotations, at least for the most commonly used functionality.

I understand it says "draft stubs" and "often require some manual updates", but it would be beneficial if stubgen could automatically infer the type of instance attributes from their declarations in the __init__ method, to avoid the need for manual corrections.

CodeByAidan avatar Jun 06 '24 13:06 CodeByAidan