Skip to content

Commit 85fad91

Browse files
author
tkucar
committed
tests reorg, docstring
1 parent 9de4012 commit 85fad91

25 files changed

+43
-11
lines changed

src/codegen/sdk/core/expressions/name.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,35 +51,61 @@ def rename_if_matching(self, old: str, new: str):
5151
self.edit(new)
5252

5353

54-
def _resolve_conditionals(self,conditional_parent:ConditionalBlock,name:str,original_resolved):
54+
@noapidoc
55+
def _resolve_conditionals(self, conditional_parent: ConditionalBlock, name: str, original_resolved):
56+
"""Resolves name references within conditional blocks by traversing the conditional chain.
57+
58+
This method handles name resolution within conditional blocks (like if/elif/else statements) by:
59+
1. Finding the appropriate search boundary based on the conditional block's position
60+
2. Handling "fake" conditionals by traversing up the conditional chain
61+
3. Yielding resolved names while respecting conditional block boundaries
62+
63+
Args:
64+
conditional_parent (ConditionalBlock): The parent conditional block containing the name reference
65+
name (str): The name being resolved
66+
original_resolved: The originally resolved symbol that triggered this resolution
67+
68+
Yields:
69+
Symbol | Import | WildcardImport: Resolved symbols found within the conditional blocks
70+
71+
Notes:
72+
- A "fake" conditional is one where is_true_conditional() returns False
73+
- The search_limit ensures we don't resolve names that appear after our target
74+
- The method stops when it either:
75+
a) Reaches the top of the conditional chain
76+
b) Returns to the original conditional block
77+
c) Can't find any more resolutions
78+
"""
5579
search_limit = conditional_parent.start_byte_for_condition_block
56-
if search_limit>=original_resolved.start_byte:
57-
search_limit=original_resolved.start_byte-1
80+
if search_limit >= original_resolved.start_byte:
81+
search_limit = original_resolved.start_byte-1
5882
if not conditional_parent.is_true_conditional(original_resolved):
5983
#If it's a fake conditional we must skip any potential enveloping conditionals
6084
def get_top_of_fake_chain(conditional,resolved,search_limit=0):
61-
if skip_fake:= conditional.parent_of_type(ConditionalBlock):
85+
if skip_fake := conditional.parent_of_type(ConditionalBlock):
6286
if skip_fake.is_true_conditional(resolved):
6387
return skip_fake.start_byte_for_condition_block
6488
search_limit=skip_fake.start_byte_for_condition_block
6589
return get_top_of_fake_chain(skip_fake,conditional,search_limit)
6690
return search_limit
67-
if search_limit:=get_top_of_fake_chain(conditional_parent,original_resolved):
68-
search_limit=search_limit
91+
if search_limit := get_top_of_fake_chain(conditional_parent,original_resolved):
92+
search_limit = search_limit
6993
else:
7094
return
7195

7296
original_conditional = conditional_parent
73-
while next_resolved:= next(conditional_parent.resolve_name(name,start_byte=search_limit,strict=False),None):
97+
while next_resolved := next(conditional_parent.resolve_name(name,start_byte=search_limit,strict=False),None):
7498
yield next_resolved
7599
next_conditional = next_resolved.parent_of_type(ConditionalBlock)
76100
if not next_conditional or next_conditional == original_conditional:
77101
return
78102
search_limit = next_conditional.start_byte_for_condition_block
79103
if next_conditional and not next_conditional.is_true_conditional(original_resolved):
80104
pass
81-
if search_limit>=next_resolved.start_byte:
82-
search_limit=next_resolved.start_byte-1
105+
if search_limit >= next_resolved.start_byte:
106+
search_limit = next_resolved.start_byte - 1
107+
108+
83109
@noapidoc
84110
@reader
85111
def resolve_name(self, name: str, start_byte: int | None = None, strict: bool = True) -> Generator["Symbol | Import | WildcardImport"]:

src/codegen/sdk/core/interfaces/conditional_block.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77

88
class ConditionalBlock(Statement, ABC):
9-
"""An interface for any code block that might not be executed in the code, e.g if block/else block/try block/catch block ect."""
9+
"""An interface for any code block that might not be executed in the code,
10+
e.g if block/else block, try block/catch block ect.
11+
"""
1012

1113
@property
1214
@abstractmethod
@@ -25,5 +27,8 @@ def start_byte_for_condition_block(self) -> int:
2527

2628
@noapidoc
2729
def is_true_conditional(self,descendant) -> bool:
28-
"""Returns if this conditional is truly conditional, this is necessary as an override for things like finally statements that share a parent with try blocks"""
30+
"""Returns if this conditional is truly conditional,
31+
this is necessary as an override for things like finally
32+
statements that share a parent with try blocks
33+
"""
2934
return True

src/codegen/sdk/core/interfaces/editable.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,7 @@ def parent_of_types(self, types: set[type[T]]) -> T | None:
11071107
return None
11081108

11091109
def is_child_of(self, instance: Editable) -> bool:
1110+
"""Checks if this node is a descendant of the given editable instance in the AST."""
11101111
if not self.parent:
11111112
return False
11121113
if self.parent is instance:

tests/unit/codegen/sdk/python/statements/assignment_statement/__init__.py

Whitespace-only changes.

tests/unit/codegen/sdk/python/statements/attribute/__init__.py

Whitespace-only changes.

tests/unit/codegen/sdk/python/statements/if_block_statement/__init__.py

Whitespace-only changes.

tests/unit/codegen/sdk/python/statements/import_statement/__init__.py

Whitespace-only changes.

tests/unit/codegen/sdk/python/statements/with_statement/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)