Skip to content

Commit cdb7659

Browse files
tomcodgentkfoss
tomcodgen
andauthored
fix: [CG-10935] tests patch (#818)
# Motivation <!-- Why is this change necessary? --> # Content <!-- Please include a summary of the change --> # Testing <!-- How was the change tested? --> # Please check the following before marking your PR as ready for review - [ ] I have added tests for my changes - [ ] I have updated the documentation or added new documentation as needed --------- Co-authored-by: tomcodgen <[email protected]>
1 parent 4164602 commit cdb7659

File tree

13 files changed

+62
-8
lines changed

13 files changed

+62
-8
lines changed

src/codegen/sdk/core/function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def resolve_name(self, name: str, start_byte: int | None = None, strict: bool =
148148
if symbol.name == name and (start_byte is None or (symbol.start_byte if isinstance(symbol, Class | Function) else symbol.end_byte) <= start_byte):
149149
yield symbol
150150
return
151-
yield from super().resolve_name(name, start_byte)
151+
yield from super().resolve_name(name, start_byte, strict=strict)
152152

153153
@cached_property
154154
@noapidoc

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22
from collections.abc import Sequence
33

44
from codegen.sdk.core.statements.statement import Statement
5+
from codegen.shared.decorators.docs import noapidoc
56

67

78
class ConditionalBlock(Statement, ABC):
89
"""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."""
910

1011
@property
1112
@abstractmethod
13+
@noapidoc
1214
def other_possible_blocks(self) -> Sequence["ConditionalBlock"]:
1315
"""Should return all other "branches" that might be executed instead."""
1416

1517
@property
18+
@noapidoc
1619
def end_byte_for_condition_block(self) -> int:
20+
"""Returns the end byte for the specific condition block"""
1721
return self.end_byte

src/codegen/sdk/core/statements/for_loop_statement.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ def resolve_name(self, name: str, start_byte: int | None = None, strict: bool =
5757
return
5858
yield frame.top.node
5959
return
60-
yield from super().resolve_name(name, start_byte)
60+
yield from super().resolve_name(name, start_byte, strict=strict)

src/codegen/sdk/core/statements/if_block_statement.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,10 @@ def reduce_condition(self, bool_condition: bool, node: Editable | None = None) -
276276
self.remove()
277277

278278
@property
279+
@noapidoc
279280
def other_possible_blocks(self) -> Sequence[ConditionalBlock]:
280281
if self.is_if_statement:
281-
return self._main_if_block.alternative_blocks
282+
return self.alternative_blocks
282283
elif self.is_elif_statement:
283284
main = self._main_if_block
284285
statements = [main]
@@ -293,6 +294,7 @@ def other_possible_blocks(self) -> Sequence[ConditionalBlock]:
293294
return [main, *main.elif_statements]
294295

295296
@property
297+
@noapidoc
296298
def end_byte_for_condition_block(self) -> int:
297299
if self.is_if_statement:
298300
return self.consequence_block.end_byte

src/codegen/sdk/core/statements/switch_case.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ def _compute_dependencies(self, usage_type: UsageKind | None = None, dest: HasNa
3737
super()._compute_dependencies(usage_type, dest)
3838

3939
@property
40+
@noapidoc
4041
def other_possible_blocks(self) -> list[ConditionalBlock]:
42+
"""Returns the end byte for the specific condition block"""
4143
return [case for case in self.parent.cases if case != self]

src/codegen/sdk/python/function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def resolve_name(self, name: str, start_byte: int | None = None, strict: bool =
131131
if name == "super()":
132132
yield self.parent_class
133133
return
134-
yield from super().resolve_name(name, start_byte)
134+
yield from super().resolve_name(name, start_byte, strict=strict)
135135

136136
@noapidoc
137137
@commiter

src/codegen/sdk/python/statements/catch_statement.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from codegen.sdk.core.statements.catch_statement import CatchStatement
66
from codegen.sdk.python.detached_symbols.code_block import PyCodeBlock
77
from codegen.sdk.python.statements.block_statement import PyBlockStatement
8-
from codegen.shared.decorators.docs import py_apidoc
8+
from codegen.shared.decorators.docs import noapidoc, py_apidoc
99

1010
if TYPE_CHECKING:
1111
from tree_sitter import Node as PyNode
@@ -29,5 +29,6 @@ def __init__(self, ts_node: PyNode, file_node_id: NodeId, ctx: CodebaseContext,
2929
self.condition = self.children[0]
3030

3131
@property
32+
@noapidoc
3233
def other_possible_blocks(self) -> list[ConditionalBlock]:
3334
return [clause for clause in self.parent.except_clauses if clause != self] + [self.parent]

src/codegen/sdk/python/statements/match_case.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from codegen.sdk.core.statements.switch_case import SwitchCase
77
from codegen.sdk.python.detached_symbols.code_block import PyCodeBlock
88
from codegen.sdk.python.statements.block_statement import PyBlockStatement
9-
from codegen.shared.decorators.docs import py_apidoc
9+
from codegen.shared.decorators.docs import noapidoc, py_apidoc
1010

1111
if TYPE_CHECKING:
1212
from codegen.sdk.codebase.codebase_context import CodebaseContext
@@ -23,5 +23,6 @@ def __init__(self, ts_node: PyNode, file_node_id: NodeId, ctx: "CodebaseContext"
2323
self.condition = self.child_by_field_name("alternative")
2424

2525
@property
26+
@noapidoc
2627
def other_possible_blocks(self) -> list["ConditionalBlock"]:
2728
return [case for case in self.parent.cases if case != self]

src/codegen/sdk/python/statements/try_catch_statement.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,12 @@ def nested_code_blocks(self) -> list[PyCodeBlock]:
101101
return nested_blocks
102102

103103
@property
104+
@noapidoc
104105
def other_possible_blocks(self) -> Sequence[ConditionalBlock]:
105106
return self.except_clauses
106107

107108
@property
109+
@noapidoc
108110
def end_byte_for_condition_block(self) -> int:
109111
if self.code_block:
110112
return self.code_block.end_byte

src/codegen/sdk/typescript/function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ def resolve_name(self, name: str, start_byte: int | None = None, strict: bool =
378378
if name == "this":
379379
yield self.parent_class
380380
return
381-
yield from super().resolve_name(name, start_byte)
381+
yield from super().resolve_name(name, start_byte, strict=strict)
382382

383383
@staticmethod
384384
def is_valid_node(node: TSNode) -> bool:

src/codegen/sdk/typescript/statements/catch_statement.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from codegen.sdk.core.statements.catch_statement import CatchStatement
66
from codegen.sdk.typescript.statements.block_statement import TSBlockStatement
7-
from codegen.shared.decorators.docs import apidoc
7+
from codegen.shared.decorators.docs import apidoc, noapidoc
88

99
if TYPE_CHECKING:
1010
from tree_sitter import Node as TSNode
@@ -31,5 +31,6 @@ def __init__(self, ts_node: TSNode, file_node_id: NodeId, ctx: CodebaseContext,
3131
self.condition = self.child_by_field_name("parameter")
3232

3333
@property
34+
@noapidoc
3435
def other_possible_blocks(self) -> list[ConditionalBlock]:
3536
return [self.parent]

src/codegen/sdk/typescript/statements/try_catch_statement.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,15 @@ def nested_code_blocks(self) -> list[TSCodeBlock]:
9696
return nested_blocks
9797

9898
@property
99+
@noapidoc
99100
def other_possible_blocks(self) -> Sequence[ConditionalBlock]:
100101
if self.catch:
101102
return [self.catch]
102103
else:
103104
return []
104105

105106
@property
107+
@noapidoc
106108
def end_byte_for_condition_block(self) -> int:
107109
if self.code_block:
108110
return self.code_block.end_byte

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,24 @@ def test_if_else_reassigment_handling_partial_if(tmpdir) -> None:
223223
assert usage.match == pyspark_arg
224224

225225

226+
def test_if_else_reassigment_handling_solo_if(tmpdir) -> None:
227+
content = """
228+
PYSPARK = "TEST"
229+
if True:
230+
PYSPARK = True
231+
print(PYSPARK)
232+
"""
233+
234+
with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}) as codebase:
235+
file = codebase.get_file("test.py")
236+
symbo = file.get_symbol("PYSPARK")
237+
funct_call = file.function_calls[0]
238+
pyspark_arg = funct_call.args.children[0]
239+
for symb in file.symbols:
240+
usage = symb.usages[0]
241+
assert usage.match == pyspark_arg
242+
243+
226244
def test_if_else_reassigment_handling_double(tmpdir) -> None:
227245
content = """
228246
if False:
@@ -266,3 +284,24 @@ def test_if_else_reassigment_handling_nested_usage(tmpdir) -> None:
266284
second = file.symbols[1]
267285
assert len(first.usages) == 0
268286
assert second.usages[0].match == pyspark_arg
287+
288+
289+
def test_if_else_reassigment_inside_func_with_external_element(tmpdir) -> None:
290+
content = """
291+
PYSPARK="0"
292+
def foo():
293+
if True:
294+
PYSPARK = True
295+
else:
296+
PYSPARK = False
297+
print(PYSPARK)
298+
299+
"""
300+
301+
with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}) as codebase:
302+
file = codebase.get_file("test.py")
303+
funct_call = file.function_calls[0]
304+
pyspark_arg = funct_call.args.children[0]
305+
func = file.get_function("foo")
306+
for assign in func.valid_symbol_names[:-1]:
307+
assign.usages[0] == pyspark_arg

0 commit comments

Comments
 (0)