diff --git a/mypy/stubgen.py b/mypy/stubgen.py index 8c7e24504270..7ea9fa2b3b8b 100755 --- a/mypy/stubgen.py +++ b/mypy/stubgen.py @@ -995,8 +995,7 @@ def visit_assignment_stmt(self, o: AssignmentStmt) -> None: self.process_namedtuple(lvalue, o.rvalue) continue if ( - self.is_top_level() - and isinstance(lvalue, NameExpr) + isinstance(lvalue, NameExpr) and not self.is_private_name(lvalue.name) and # it is never an alias with explicit annotation @@ -1114,7 +1113,7 @@ def is_alias_expression(self, expr: Expression, top_level: bool = True) -> bool: def process_typealias(self, lvalue: NameExpr, rvalue: Expression) -> None: p = AliasPrinter(self) - self.add(f"{lvalue.name} = {rvalue.accept(p)}\n") + self.add(f"{self._indent}{lvalue.name} = {rvalue.accept(p)}\n") self.record_name(lvalue.name) self._vars[-1].append(lvalue.name) diff --git a/test-data/unit/stubgen.test b/test-data/unit/stubgen.test index 8467271e5593..3fcceb809fc4 100644 --- a/test-data/unit/stubgen.test +++ b/test-data/unit/stubgen.test @@ -947,16 +947,6 @@ from typing import Any alias = Container[Any] -[case testAliasOnlyToplevel] -class Foo: - alias = str - -[out] -from _typeshed import Incomplete - -class Foo: - alias: Incomplete - [case testAliasExceptions] noalias1 = None noalias2 = ... @@ -969,6 +959,56 @@ noalias1: Incomplete noalias2: Incomplete noalias3: bool +[case testComplexAlias] +# modules: main a + +from a import valid + +def func() -> int: + return 2 + +aliased_func = func +int_value = 1 + +class A: + cls_var = valid + + def __init__(self, arg: str) -> None: + self.self_var = arg + + def meth(self) -> None: + func_value = int_value + + alias_meth = meth + alias_func = func + alias_alias_func = aliased_func + int_value = int_value + +[file a.py] +valid : list[int] = [1, 2, 3] + + +[out] +# main.pyi +from _typeshed import Incomplete +from a import valid + +def func() -> int: ... +aliased_func = func +int_value: int + +class A: + cls_var = valid + self_var: Incomplete + def __init__(self, arg: str) -> None: ... + def meth(self) -> None: ... + alias_meth = meth + alias_func = func + alias_alias_func = aliased_func + int_value = int_value +# a.pyi +valid: list[int] + -- More features/fixes: -- do not export deleted names