Skip to content

Detect definitions in list and tuple pattern of match statement #5

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 1 commit into from
Jun 28, 2024
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
10 changes: 10 additions & 0 deletions auto_typing_final/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def find_identifiers_in_function_body(node: SgNode) -> Iterable[str]: # noqa: C
case (("identifier", _), ("=", _), ("dotted_name", alias)):
if identifier := last_child_of_type(alias, "identifier"):
yield identifier.text()
case "list_pattern" | "tuple_pattern":
for child in node.children():
if (
child.kind() == "case_pattern"
and (last_child := last_child_of_type(child, "dotted_name"))
and (last_last_child := last_child_of_type(last_child, "identifier"))
):
yield last_last_child.text()
case "splat_pattern":
yield from texts_of_identifier_nodes(node)
case "dict_pattern":
Expand Down Expand Up @@ -93,6 +101,8 @@ def find_identifiers_in_function_parameter(node: SgNode) -> Iterable[str]:
{"kind": "keyword_pattern"},
{"kind": "splat_pattern"},
{"kind": "dict_pattern"},
{"kind": "list_pattern"},
{"kind": "tuple_pattern"},
{"kind": "for_statement"},
]
}
Expand Down
28 changes: 28 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,34 @@ def foo():
case [b, *a]: ...
"""),

("""
def foo():
a: typing.Final = 1

match ...:
case [a]: ...
""", """
def foo():
a = 1

match ...:
case [a]: ...
"""),

("""
def foo():
a: typing.Final = 1

match ...:
case (a,): ...
""", """
def foo():
a = 1

match ...:
case (a,): ...
"""),

("""
def foo():
a: typing.Final = 1
Expand Down