Skip to content

Commit ea26bcb

Browse files
authored
Merge pull request #33 from shabbirahussain/main
Fix crash caused due to decorators not having id attribute
2 parents 50a6150 + 7195918 commit ea26bcb

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

deadcode/visitor/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def get_decorator_name(decorator: Union[ast.Call, ast.Attribute]) -> str:
5858
while isinstance(decorator, ast.Attribute):
5959
parts.append(decorator.attr)
6060
decorator = decorator.value # type: ignore
61-
parts.append(decorator.id) # type: ignore
61+
parts.append(str(id(decorator))) # type: ignore
6262
return '@' + '.'.join(reversed(parts))
6363

6464

tests/visitor/test_utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import ast
2+
import re
3+
4+
from deadcode.visitor.utils import get_decorator_name
5+
6+
class TestUtils:
7+
def test_get_decorator_name_attribute(self):
8+
# Test with a decorator with an attribute
9+
decorator = ast.Attribute(value=ast.Name(id='module', ctx=ast.Load()), attr='my_decorator1')
10+
assert get_decorator_name(decorator).endswith('.my_decorator1')
11+
12+
# Test with a complex decorator
13+
decorator = ast.Attribute(
14+
value=decorator, # Create nested attributes
15+
attr='my_decorator2',
16+
ctx=ast.Load()
17+
)
18+
assert re.match(r'@\d+\.my_decorator1\.\d+\.my_decorator2', get_decorator_name(decorator))
19+
20+
21+
def test_get_decorator_name_call(self):
22+
decorator = ast.Attribute(value=ast.Name(id='module', ctx=ast.Load()), attr='my_decorator')
23+
assert re.match(r'@\d+\.my_decorator', get_decorator_name(decorator))
24+
25+
# Test with a decorator that is a call
26+
decorator = ast.Call(
27+
func=decorator,
28+
args=[],
29+
keywords=[]
30+
)
31+
assert re.match(r'@\d+\.my_decorator', get_decorator_name(decorator))

0 commit comments

Comments
 (0)