Skip to content

Commit

Permalink
Fix typing
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlatr committed Sep 16, 2024
1 parent 415c7d9 commit 28f01e7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pydoctor/astbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,14 +751,14 @@ def _handleAssignment(self,
else:
raise IgnoreAssignment()

def _handleDocComment(self, node: ast.Assign | ast.AnnAssign, target: ast.expr):
def _handleDocComment(self, node: ast.Assign | ast.AnnAssign, target: ast.expr) -> None:
# Process the doc-comments, this is very similiar to the inline docstrings.
try:
parent, name = self._contextualizeTarget(target)
except ValueError:
return

# fetch the target of the inline docstring
# fetch the target of the doc-comment
if (attr:=parent.contents.get(name)) is None:
return

Expand Down
24 changes: 13 additions & 11 deletions pydoctor/astutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,18 +836,18 @@ def __repr__(self) -> str:
self.value.strip())

class TokenProcessor:
def __init__(self, buffers: List[str]) -> None:
def __init__(self, buffers: Sequence[str]) -> None:
lines = iter(buffers)
self.buffers = buffers
self.tokens = generate_tokens(lambda: next(lines))
self.current = None # type: Token
self.previous = None # type: Token
self.current: Token | None = None
self.previous: Token | None = None

def get_line(self, lineno: int) -> str:
"""Returns specified line."""
return self.buffers[lineno - 1]

def fetch_token(self) -> Token:
def fetch_token(self) -> Token | None:
"""Fetch a next token from source code.
Returns ``None`` if sequence finished.
Expand All @@ -867,6 +867,7 @@ def fetch_until(self, condition: Any) -> List[Token]:
"""
tokens = []
while self.fetch_token():
assert self.current
tokens.append(self.current)
if self.current == condition:
break
Expand All @@ -887,14 +888,15 @@ class AfterCommentParser(TokenProcessor):
and returns the comments for variable if exists.
"""

def __init__(self, lines: List[str]) -> None:
def __init__(self, lines: Sequence[str]) -> None:
super().__init__(lines)
self.comment = None # type: str
self.comment: str | None = None

def fetch_rvalue(self) -> List[Token]:
def fetch_rvalue(self) -> Sequence[Token]:
"""Fetch right-hand value of assignment."""
tokens = []
tokens: list[Token] = []
while self.fetch_token():
assert self.current
tokens.append(self.current)
if self.current == [OP, '(']:
tokens += self.fetch_until([OP, ')'])
Expand All @@ -914,14 +916,15 @@ def fetch_rvalue(self) -> List[Token]:
def parse(self) -> None:
"""Parse the code and obtain comment after assignment."""
# skip lvalue (or whole of AnnAssign)
while not self.fetch_token().match([OP, '='], NEWLINE, COMMENT):
while (current:=self.fetch_token()) and not current.match([OP, '='], NEWLINE, COMMENT):
assert self.current

# skip rvalue (if exists)
if self.current == [OP, '=']:
self.fetch_rvalue()

if self.current == COMMENT:
assert self.current
self.comment = self.current.value

comment_re = re.compile('^\\s*#: ?(.*)\r?\n?$')
Expand All @@ -938,8 +941,7 @@ def extract_doc_comment_after(node: ast.Assign | ast.AnnAssign, lines: Sequence[
"""
# check doc comments after assignment
current_line = lines[node.lineno - 1]
parser = AfterCommentParser([current_line[node.col_offset:]] +
lines[node.lineno:])
parser = AfterCommentParser([current_line[node.col_offset:], *lines[node.lineno:]])
parser.parse()
if parser.comment and comment_re.match(parser.comment):
docstring = comment_re.sub('\\1', parser.comment)
Expand Down

0 comments on commit 28f01e7

Please sign in to comment.