From a3bc82cce73a67075a9440543570a8325d4e6034 Mon Sep 17 00:00:00 2001 From: Chad Dombrova Date: Tue, 3 Jan 2023 16:34:56 -0800 Subject: [PATCH 1/2] stubgen: Allow aliases below the top level --- mypy/stubgen.py | 5 ++-- test-data/unit/stubgen.test | 60 ++++++++++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 13 deletions(-) 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..2b1a77a1cfdd 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: + pass + + 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 From 274cbc1e532eb24f5fce35ec83e2aed0bf0380ae Mon Sep 17 00:00:00 2001 From: Chad Dombrova Date: Mon, 30 Jan 2023 11:56:08 -0800 Subject: [PATCH 2/2] add a function-level alias to test --- test-data/unit/stubgen.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/stubgen.test b/test-data/unit/stubgen.test index 2b1a77a1cfdd..3fcceb809fc4 100644 --- a/test-data/unit/stubgen.test +++ b/test-data/unit/stubgen.test @@ -977,7 +977,7 @@ class A: self.self_var = arg def meth(self) -> None: - pass + func_value = int_value alias_meth = meth alias_func = func