Skip to content

Commit

Permalink
Cope with Astroid having renamed _BaseContainer
Browse files Browse the repository at this point in the history
Fix per #100 (comment),
I haven't checked the release notes myself.

This leaves three remaining test failures which originate from
astroid v3.
  • Loading branch information
PeterJCLaw committed Oct 24, 2023
1 parent 0af0b06 commit a0b018c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 8 additions & 1 deletion asttokens/astroid_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@
astroid_node_classes = None
NodeNG = None

__all__ = ["astroid_node_classes", "NodeNG"]
BaseContainer = getattr(
astroid_node_classes,
'BaseContainer',
getattr(astroid_node_classes, '_BaseContainer', None),
)


__all__ = ["astroid_node_classes", "NodeNG", "BaseContainer"]
6 changes: 3 additions & 3 deletions asttokens/mark_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from . import util
from .asttokens import ASTTokens
from .util import AstConstant
from .astroid_compat import astroid_node_classes as nc
from .astroid_compat import astroid_node_classes as nc, BaseContainer as AstroidBaseContainer

if TYPE_CHECKING:
from .util import AstNode
Expand Down Expand Up @@ -312,7 +312,7 @@ def handle_bare_tuple(self, node, first_token, last_token):
# In Python3.8 parsed tuples include parentheses when present.
def handle_tuple_nonempty(self, node, first_token, last_token):
# type: (AstNode, util.Token, util.Token) -> Tuple[util.Token, util.Token]
assert isinstance(node, ast.Tuple) or isinstance(node, nc._BaseContainer)
assert isinstance(node, ast.Tuple) or isinstance(node, AstroidBaseContainer)
# It's a bare tuple if the first token belongs to the first child. The first child may
# include extraneous parentheses (which don't create new nodes), so account for those too.
child = node.elts[0]
Expand All @@ -331,7 +331,7 @@ def handle_tuple_nonempty(self, node, first_token, last_token):

def visit_tuple(self, node, first_token, last_token):
# type: (AstNode, util.Token, util.Token) -> Tuple[util.Token, util.Token]
assert isinstance(node, ast.Tuple) or isinstance(node, nc._BaseContainer)
assert isinstance(node, ast.Tuple) or isinstance(node, AstroidBaseContainer)
if not node.elts:
# An empty tuple is just "()", and we need no further info.
return (first_token, last_token)
Expand Down

0 comments on commit a0b018c

Please sign in to comment.