Skip to content

Fix mypy warnings for astroid/rebuilder #1244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions astroid/nodes/node_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4489,12 +4489,12 @@ class MatchSingleton(Pattern):
def __init__(
self,
*,
value: Literal[True, False, None],
value: Literal[True, False, None], # type: ignore[assignment] # See https://github.com/python/mypy/pull/10389
lineno: Optional[int] = None,
col_offset: Optional[int] = None,
parent: Optional[NodeNG] = None,
) -> None:
self.value: Literal[True, False, None] = value
self.value = value
super().__init__(lineno=lineno, col_offset=col_offset, parent=parent)


Expand Down
15 changes: 12 additions & 3 deletions astroid/rebuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from astroid import nodes
from astroid._ast import ParserModule, get_parser_module, parse_function_type_comment
from astroid.const import PY37_PLUS, PY38_PLUS, PY39_PLUS, Context
from astroid.exceptions import ParentMissingError
from astroid.manager import AstroidManager
from astroid.nodes import NodeNG

Expand Down Expand Up @@ -106,7 +107,7 @@ def __init__(
def _get_doc(self, node: T_Doc) -> Tuple[T_Doc, Optional[str]]:
try:
if PY37_PLUS and hasattr(node, "docstring"):
doc = node.docstring
doc = node.docstring # type: ignore[union-attr, attr-defined] # mypy doesn't recognize hasattr
return node, doc
if node.body and isinstance(node.body[0], self._module.Expr):

Expand Down Expand Up @@ -803,8 +804,10 @@ def _save_assignment(self, node: Union[nodes.AssignName, nodes.DelName]) -> None
"""save assignement situation since node.parent is not available yet"""
if self._global_names and node.name in self._global_names[-1]:
node.root().set_local(node.name, node)
else:
elif node.parent:
node.parent.set_local(node.name, node)
else:
raise ParentMissingError(target=node)

def visit_arg(self, node: "ast.arg", parent: NodeNG) -> nodes.AssignName:
"""visit an arg node by returning a fresh AssName instance"""
Expand Down Expand Up @@ -872,6 +875,8 @@ def visit_arguments(self, node: "ast.arguments", parent: NodeNG) -> nodes.Argume
type_comment_posonlyargs=type_comment_posonlyargs,
)
# save argument names in locals:
if not newnode.parent:
raise ParentMissingError(target=newnode)
if vararg:
newnode.parent.set_local(vararg, newnode)
if kwarg:
Expand Down Expand Up @@ -935,6 +940,9 @@ def check_function_type_comment(
# Invalid type comment, just skip it.
return None

if not type_comment_ast:
return None

returns: Optional[NodeNG] = None
argtypes: List[NodeNG] = [
self.visit(elem, parent) for elem in (type_comment_ast.argtypes or [])
Expand Down Expand Up @@ -1354,7 +1362,8 @@ def visit_attribute(
newnode = nodes.AssignAttr(node.attr, node.lineno, node.col_offset, parent)
# Prohibit a local save if we are in an ExceptHandler.
if not isinstance(parent, nodes.ExceptHandler):
self._delayed_assattr.append(newnode)
# mypy doesn't recognize that newnode has to be AssignAttr
self._delayed_assattr.append(newnode) # type: ignore [arg-type]
else:
newnode = nodes.Attribute(node.attr, node.lineno, node.col_offset, parent)
newnode.postinit(self.visit(node.value, newnode))
Expand Down