14
14
from tokenize import TokenInfo
15
15
16
16
17
+ _OVERRIDES = {"@override" , "@typing_extensions.override" , "@typing.override" }
18
+
19
+
17
20
@total_ordering
18
21
@dc .dataclass
19
22
class Block :
@@ -68,11 +71,20 @@ class Category(str, Enum):
68
71
69
72
@property
70
73
def start_line (self ) -> int :
71
- return self .tokens [max (self .indent , self .index )].start [0 ]
74
+ """The line number for the def or class statement"""
75
+ return self .tokens [self .begin ].start [0 ]
72
76
73
77
@property
74
78
def end_line (self ) -> int :
75
- return self .tokens [max (self .dedent , self .index )].start [0 ]
79
+ if 0 <= self .dedent < len (self .tokens ):
80
+ return self .tokens [self .dedent ].start [0 ] - 1
81
+ else :
82
+ return self .tokens [- 1 ].start [0 ]
83
+ # Only happens in one case so far: a file whose last line was
84
+ #
85
+ # def function(): ...
86
+ #
87
+ # and the dedent correctly pointed to one past the end of self.tokens
76
88
77
89
@property
78
90
def line_count (self ) -> int :
@@ -99,9 +111,7 @@ def decorators(self) -> list[str]:
99
111
100
112
@cached_property
101
113
def is_override (self ) -> bool :
102
- return not self .is_class and any (
103
- d .rpartition ("." )[2 ] == "override" for d in self .decorators
104
- )
114
+ return not self .is_class and bool (_OVERRIDES .intersection (self .decorators ))
105
115
106
116
DATA_FIELDS = (
107
117
"category" ,
@@ -149,9 +159,9 @@ def _get_decorators(tokens: Sequence[TokenInfo], block_start: int) -> list[str]:
149
159
def decorators () -> Iterator [str ]:
150
160
rev = reversed (range (block_start ))
151
161
newlines = (i for i in rev if tokens [i ].type == token .NEWLINE )
152
- newlines = itertools .chain (newlines , [- 1 ]) # To account for the first line
162
+ it = iter (itertools .chain (newlines , [- 1 ]))
163
+ # The -1 accounts for the very first line in the file
153
164
154
- it = iter (newlines )
155
165
end = next (it , - 1 ) # Like itertools.pairwise in Python 3.10
156
166
for begin in it :
157
167
for i in range (begin + 1 , end ):
0 commit comments