File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -57,7 +57,7 @@ def get_decorator_name(decorator: ast.Call | ast.Attribute) -> str:
5757 while isinstance (decorator , ast .Attribute ):
5858 parts .append (decorator .attr )
5959 decorator = decorator .value # type: ignore
60- parts .append (decorator . id ) # type: ignore
60+ parts .append (str ( id ( decorator )) ) # type: ignore
6161 return '@' + '.' .join (reversed (parts ))
6262
6363
Original file line number Diff line number Diff line change 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 ))
You can’t perform that action at this time.
0 commit comments