Skip to content

Commit 6e2a9f7

Browse files
committed
stubgen: add --allow-class-level-aliases flag
1 parent a3bc82c commit 6e2a9f7

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

mypy/stubgen.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ def __init__(
229229
verbose: bool,
230230
quiet: bool,
231231
export_less: bool,
232+
allow_class_level_aliases: bool,
232233
) -> None:
233234
# See parse_options for descriptions of the flags.
234235
self.pyversion = pyversion
@@ -247,6 +248,7 @@ def __init__(
247248
self.verbose = verbose
248249
self.quiet = quiet
249250
self.export_less = export_less
251+
self.allow_class_level_aliases = allow_class_level_aliases
250252

251253

252254
class StubSource:
@@ -594,6 +596,7 @@ def __init__(
594596
include_private: bool = False,
595597
analyzed: bool = False,
596598
export_less: bool = False,
599+
allow_class_level_aliases: bool = False,
597600
) -> None:
598601
# Best known value of __all__.
599602
self._all_ = _all_
@@ -608,6 +611,7 @@ def __init__(
608611
self._state = EMPTY
609612
self._toplevel_names: list[str] = []
610613
self._include_private = include_private
614+
self.allow_class_level_aliases = allow_class_level_aliases
611615
self.import_tracker = ImportTracker()
612616
# Was the tree semantically analysed before?
613617
self.analyzed = analyzed
@@ -995,7 +999,8 @@ def visit_assignment_stmt(self, o: AssignmentStmt) -> None:
995999
self.process_namedtuple(lvalue, o.rvalue)
9961000
continue
9971001
if (
998-
isinstance(lvalue, NameExpr)
1002+
(self.is_top_level() or self.allow_class_level_aliases)
1003+
and isinstance(lvalue, NameExpr)
9991004
and not self.is_private_name(lvalue.name)
10001005
and
10011006
# it is never an alias with explicit annotation
@@ -1623,6 +1628,7 @@ def generate_stub_from_ast(
16231628
parse_only: bool = False,
16241629
include_private: bool = False,
16251630
export_less: bool = False,
1631+
allow_class_level_aliases: bool = False,
16261632
) -> None:
16271633
"""Use analysed (or just parsed) AST to generate type stub for single file.
16281634
@@ -1632,6 +1638,7 @@ def generate_stub_from_ast(
16321638
gen = StubGenerator(
16331639
mod.runtime_all,
16341640
include_private=include_private,
1641+
allow_class_level_aliases=allow_class_level_aliases,
16351642
analyzed=not parse_only,
16361643
export_less=export_less,
16371644
)
@@ -1695,7 +1702,12 @@ def generate_stubs(options: Options) -> None:
16951702
files.append(target)
16961703
with generate_guarded(mod.module, target, options.ignore_errors, options.verbose):
16971704
generate_stub_from_ast(
1698-
mod, target, options.parse_only, options.include_private, options.export_less
1705+
mod,
1706+
target,
1707+
options.parse_only,
1708+
options.include_private,
1709+
options.export_less,
1710+
options.allow_class_level_aliases
16991711
)
17001712

17011713
# Separately analyse C modules using different logic.
@@ -1755,6 +1767,13 @@ def parse_options(args: list[str]) -> Options:
17551767
help="generate stubs for objects and members considered private "
17561768
"(single leading underscore and no trailing underscores)",
17571769
)
1770+
parser.add_argument(
1771+
"--allow-class-level-aliases",
1772+
action="store_true",
1773+
help="by default variables which refer to the values of other variables "
1774+
"are only respected at the module-level. this allows aliases at the "
1775+
"class level as well.",
1776+
)
17581777
parser.add_argument(
17591778
"--export-less",
17601779
action="store_true",
@@ -1840,6 +1859,7 @@ def parse_options(args: list[str]) -> Options:
18401859
verbose=ns.verbose,
18411860
quiet=ns.quiet,
18421861
export_less=ns.export_less,
1862+
allow_class_level_aliases=ns.allow_class_level_aliases,
18431863
)
18441864

18451865

test-data/unit/stubgen.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,7 @@ noalias2: Incomplete
960960
noalias3: bool
961961

962962
[case testComplexAlias]
963+
# flags: --allow-class-level-aliases
963964
# modules: main a
964965

965966
from a import valid

0 commit comments

Comments
 (0)