Skip to content

Remove (not ignore) typing.Final inside loops #6

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 2 commits 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: 9 additions & 1 deletion auto_typing_final/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ def is_in_loop(node: SgNode) -> bool:

def make_operation_from_assignments_to_one_name(nodes: list[SgNode]) -> Operation:
value_assignments: list[Definition] = []
has_node_in_loop = False

for node in nodes:
children = node.children()

if node.kind() == "assignment" and not is_in_loop(node):
if is_in_loop(node):
has_node_in_loop = True

if node.kind() == "assignment":
match tuple(child.kind() for child in children):
case ("identifier", "=", _):
value_assignments.append(
Expand All @@ -74,6 +78,10 @@ def make_operation_from_assignments_to_one_name(nodes: list[SgNode]) -> Operatio
value_assignments.append(OtherDefinition(node))
else:
value_assignments.append(OtherDefinition(node))

if has_node_in_loop:
return RemoveFinal(value_assignments)

match value_assignments:
case [assignment]:
return AddFinal(assignment)
Expand Down
38 changes: 38 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,52 @@ def foo():
a = 1
"""),

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

("""
def foo():
for _ in ...:
a: typing.Final = 1
""", """
def foo():
for _ in ...:
a = 1
"""),

("""
def foo():
for _ in ...:
def foo():
a: typing.Final = 1
""", """
def foo():
for _ in ...:
def foo():
a = 1
"""),

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

for _ in ...:
a: typing.Final = 1
""", """
def foo():
a = 1
b: typing.Final = 2

for _ in ...:
a = 1
"""),

("""
Expand Down