Skip to content

Commit 27e4445

Browse files
committed
docstring_linter: Fix #151692 and other issues
ghstack-source-id: 71f2de6 Pull Request resolved: #156596
1 parent 1abff80 commit 27e4445

17 files changed

+413
-430
lines changed

tools/linter/adapters/_linter/block.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
from tokenize import TokenInfo
1515

1616

17+
_OVERRIDES = {"@override", "@typing_extensions.override", "@typing.override"}
18+
19+
1720
@total_ordering
1821
@dc.dataclass
1922
class Block:
@@ -68,11 +71,20 @@ class Category(str, Enum):
6871

6972
@property
7073
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]
7276

7377
@property
7478
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
7688

7789
@property
7890
def line_count(self) -> int:
@@ -99,9 +111,7 @@ def decorators(self) -> list[str]:
99111

100112
@cached_property
101113
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))
105115

106116
DATA_FIELDS = (
107117
"category",
@@ -149,9 +159,9 @@ def _get_decorators(tokens: Sequence[TokenInfo], block_start: int) -> list[str]:
149159
def decorators() -> Iterator[str]:
150160
rev = reversed(range(block_start))
151161
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
153164

154-
it = iter(newlines)
155165
end = next(it, -1) # Like itertools.pairwise in Python 3.10
156166
for begin in it:
157167
for i in range(begin + 1, end):

0 commit comments

Comments
 (0)