Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[stubgen] Improve self annotations#18420

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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
10 changes: 5 additions & 5 deletionsmypy/stubgen.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -648,11 +648,11 @@ def visit_func_def(self, o: FuncDef) -> None:
self.add("\n")
if not self.is_top_level():
self_inits = find_self_initializers(o)
for init, value in self_inits:
for init, value, annotation in self_inits:
if init in self.method_names:
# Can't have both an attribute and a method/property with the same name.
continue
init_code = self.get_init(init, value)
init_code = self.get_init(init, value, annotation)
if init_code:
self.add(init_code)

Expand DownExpand Up@@ -1414,7 +1414,7 @@ def find_method_names(defs: list[Statement]) -> set[str]:

class SelfTraverser(mypy.traverser.TraverserVisitor):
def __init__(self) -> None:
self.results: list[tuple[str, Expression]] = []
self.results: list[tuple[str, Expression, Type | None]] = []

def visit_assignment_stmt(self, o: AssignmentStmt) -> None:
lvalue = o.lvalues[0]
Expand All@@ -1423,10 +1423,10 @@ def visit_assignment_stmt(self, o: AssignmentStmt) -> None:
and isinstance(lvalue.expr, NameExpr)
and lvalue.expr.name == "self"
):
self.results.append((lvalue.name, o.rvalue))
self.results.append((lvalue.name, o.rvalue, o.unanalyzed_type))


def find_self_initializers(fdef: FuncBase) -> list[tuple[str, Expression]]:
def find_self_initializers(fdef: FuncBase) -> list[tuple[str, Expression, Type | None]]:
"""Find attribute initializers in a method.

Return a list of pairs (attribute name, r.h.s. expression).
Expand Down
11 changes: 11 additions & 0 deletionstest-data/unit/stubgen.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -238,13 +238,24 @@ class C:
def __init__(self, x: str) -> None: ...

[case testSelfAssignment]
from mod import A
from typing import Any, Dict, Union
class C:
def __init__(self):
self.a: A = A()
self.x = 1
x.y = 2
self.y: Dict[str, Any] = {}
self.z: Union[int, str, bool, None] = None
[out]
from mod import A
from typing import Any

class C:
a: A
x: int
y: dict[str, Any]
z: int | str | bool | None
def __init__(self) -> None: ...

[case testSelfAndClassBodyAssignment]
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp