Skip to content

Commit 00fa822

Browse files
committed
Add initial implementation of CodeItem.number_of_uses
this feature is still unfinished and under active implementation.
1 parent 0d2dbe3 commit 00fa822

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

deadcode/tests/nested_scope/test_nested_scope.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from unittest import skip
2+
13
from deadcode.cli import main
24
from deadcode.tests.base import BaseTestCase
35

@@ -120,6 +122,7 @@ class Eggs(Spam):
120122

121123
self.assertIsNone(result)
122124

125+
@skip("This feature is being implemented")
123126
def test_type_tracking_for_function_arguments(self):
124127
self.files = {
125128
"foo.py": """
@@ -160,3 +163,18 @@ def spam(eggs):
160163
)
161164

162165
self.assertIsNone(result)
166+
167+
@skip(">> Observed types in scope definition is still being implemented")
168+
def test_scope_update_for_method_call_expression(self):
169+
# >>> TODO: Mark types in scope usage correctly for method invocation
170+
self.files = {
171+
"foo.py": """
172+
Bar().spam()
173+
"""
174+
}
175+
176+
result = main(["foo.py", "--no-color", "--fix"])
177+
178+
self.assertFiles({"foo.py": """"""})
179+
180+
self.assertIsNone(result)

deadcode/utils/nested_scopes.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def add(self, code_item: CodeItem) -> None:
3131
current_scope[scope_part] = {} # Could use None if type cannot have scope
3232
current_scope = current_scope[scope_part]
3333

34-
# > TODO: leef should be replaced. Is it replaced with new code item?
34+
# > TODO: leaf should be replaced. Is it replaced with new code item?
3535
current_scope[code_item] = {}
3636

3737
def get(self, name: str, scope: str) -> Optional[Union[CodeItem, str]]:
@@ -60,3 +60,24 @@ def get(self, name: str, scope: str) -> Optional[Union[CodeItem, str]]:
6060
return current_scope_keys[current_scope_keys.index(name)]
6161

6262
return None
63+
64+
def mark_as_used(self, name: str, scope: str) -> None:
65+
# >TODO: This method does not work, when methods are being invoked.
66+
# The scope is global, name is method name, but the instance and
67+
# class names are not being taken into account.
68+
69+
# Solution: parsing of a method invocation should be handled differently.
70+
# More precicely. The parsing should be in the right order.
71+
72+
# Next step to solve this issue:
73+
# > Investigate how usage statement is being handled and what can be done about it.
74+
# Write tests for Class and instance creations.
75+
76+
# In this place I should get a list of names, which are being used in the invocation.
77+
78+
# if name == "spam":
79+
# breakpoint()
80+
81+
code_item = self.get(name, scope)
82+
if isinstance(code_item, CodeItem):
83+
code_item.number_of_uses += 1

deadcode/visitor/code_item.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class CodeItem: # TODO: This should also be a dataclass, because hash and tuple
5252
"name_column",
5353
"message",
5454
"error_code",
55+
"number_of_uses",
5556
)
5657

5758
def __init__(
@@ -70,12 +71,14 @@ def __init__(
7071
name_line: Optional[int] = None,
7172
name_column: Optional[int] = None,
7273
message: str = "",
74+
number_of_uses: int = 0,
7375
):
7476
self.name = name
7577
self.type_ = type_
7678
self.filename = filename
7779
self.scope = scope
7880
self.inherits_from = inherits_from
81+
self.number_of_uses = number_of_uses
7982

8083
if code_parts is None:
8184
self.code_parts = []

0 commit comments

Comments
 (0)