Skip to content

Stubgen: Instance Attributes set to Incomplete even though explicit declared #14673

Open
@NielsKorschinsky

Description

@NielsKorschinsky

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions