Skip to content

stubgen: never generate variable initializer #10623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1070,9 +1070,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 @@ -720,7 +720,7 @@ class A:
[out]
class A:
class B:
x: int = ...
x: int
def f(self) -> None: ...
def g(self) -> None: ...

Expand Down Expand Up @@ -775,7 +775,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 @@ -911,7 +911,7 @@ class Foo:
from typing import Any

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

[case testAliasExceptions]
noalias1 = None
Expand Down Expand Up @@ -1561,14 +1561,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 @@ -2174,7 +2174,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