Skip to content

Commit

Permalink
stubgen: never generate variable initializer (#10623)
Browse files Browse the repository at this point in the history
Previously, the generation of a variable initialized (= ...) was
inconsistent between top-level and class-level variables. Since the
initializer has no function in stubs, stubgen will now never generate
it.
  • Loading branch information
srittau authored Jun 10, 2021
1 parent 7e1eb3c commit 8664ada
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
8 changes: 3 additions & 5 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
Expression, IntExpr, UnaryExpr, StrExpr, BytesExpr, NameExpr, FloatExpr, MemberExpr,
TupleExpr, ListExpr, ComparisonExpr, CallExpr, IndexExpr, EllipsisExpr,
ClassDef, MypyFile, Decorator, AssignmentStmt, TypeInfo,
IfStmt, ImportAll, ImportFrom, Import, FuncDef, FuncBase, TempNode, Block,
IfStmt, ImportAll, ImportFrom, Import, FuncDef, FuncBase, Block,
Statement, OverloadedFuncDef, ARG_POS, ARG_STAR, ARG_STAR2, ARG_NAMED, ARG_NAMED_OPT
)
from mypy.stubgenc import generate_stub_for_c_module
Expand Down Expand Up @@ -678,7 +678,7 @@ def visit_decorator(self, o: Decorator) -> None:
self.visit_func_def(o.func, is_abstract=is_abstract)

def process_decorator(self, o: Decorator) -> Tuple[bool, bool]:
"""Process a series of decorataors.
"""Process a series of decorators.
Only preserve certain special decorators such as @abstractmethod.
Expand Down Expand Up @@ -1076,9 +1076,7 @@ def get_init(self, lvalue: str, rvalue: Expression,
typename += '[{}]'.format(final_arg)
else:
typename = self.get_str_type_of_node(rvalue)
has_rhs = not (isinstance(rvalue, TempNode) and rvalue.no_rhs)
initializer = " = ..." if has_rhs and not self.is_top_level() else ""
return '%s%s: %s%s\n' % (self._indent, lvalue, typename, initializer)
return '%s%s: %s\n' % (self._indent, lvalue, typename)

def add(self, string: str) -> None:
"""Add text to generated stub."""
Expand Down
22 changes: 11 additions & 11 deletions test-data/unit/stubgen.test
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class C:
x = 1
[out]
class C:
x: int = ...
x: int

[case testInitTypeAnnotationPreserved]
class C:
Expand All @@ -178,7 +178,7 @@ class C:
x.y = 2
[out]
class C:
x: int = ...
x: int
def __init__(self) -> None: ...

[case testSelfAndClassBodyAssignment]
Expand All @@ -192,7 +192,7 @@ class C:
x: int

class C:
x: int = ...
x: int
def __init__(self) -> None: ...

[case testEmptyClass]
Expand Down Expand Up @@ -244,7 +244,7 @@ class A:
_x: int

class A:
_y: int = ...
_y: int

[case testSpecialInternalVar]
__all__ = []
Expand Down Expand Up @@ -438,7 +438,7 @@ class A:
def f(self): ...
[out]
class A:
x: int = ...
x: int
def f(self) -> None: ...

[case testSkipMultiplePrivateDefs]
Expand Down Expand Up @@ -742,7 +742,7 @@ class A:
[out]
class A:
class B:
x: int = ...
x: int
def f(self) -> None: ...
def g(self) -> None: ...

Expand Down Expand Up @@ -797,7 +797,7 @@ class A:
from typing import Any

class A:
x: Any = ...
x: Any
def __init__(self, a: Any | None = ...) -> None: ...
def method(self, a: Any | None = ...) -> None: ...

Expand Down Expand Up @@ -933,7 +933,7 @@ class Foo:
from typing import Any

class Foo:
alias: Any = ...
alias: Any

[case testAliasExceptions]
noalias1 = None
Expand Down Expand Up @@ -1583,14 +1583,14 @@ class B:
from typing import Any

class A:
y: str = ...
y: str
@property
def x(self): ...

class B:
@property
def x(self): ...
y: str = ...
y: str
@x.setter
def x(self, value: Any) -> None: ...

Expand Down Expand Up @@ -2196,7 +2196,7 @@ class C:
from typing import Any

class C:
x: Any = ...
x: Any
def __init__(self, x: Any) -> None: ...
def __lt__(self, other: Any) -> Any: ...
def __le__(self, other: Any) -> Any: ...
Expand Down

0 comments on commit 8664ada

Please sign in to comment.