diff options
Diffstat (limited to 'astroid/node_classes.py')
| -rw-r--r-- | astroid/node_classes.py | 175 |
1 files changed, 132 insertions, 43 deletions
diff --git a/astroid/node_classes.py b/astroid/node_classes.py index b1344723..59b1e31c 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -3,7 +3,7 @@ # Copyright (c) 2010 Daniel Harding <dharding@gmail.com> # Copyright (c) 2012 FELD Boris <lothiraldan@gmail.com> # Copyright (c) 2013-2014 Google, Inc. -# Copyright (c) 2014-2018 Claudiu Popa <pcmanticore@gmail.com> +# Copyright (c) 2014-2020 Claudiu Popa <pcmanticore@gmail.com> # Copyright (c) 2014 Eevee (Alex Munroe) <amunroe@yelp.com> # Copyright (c) 2015-2016 Ceridwen <ceridwenv@gmail.com> # Copyright (c) 2015 Florian Bruhin <me@the-compiler.org> @@ -11,13 +11,17 @@ # Copyright (c) 2016 Jared Garst <jgarst@users.noreply.github.com> # Copyright (c) 2016 Jakub Wilk <jwilk@jwilk.net> # Copyright (c) 2016 Dave Baum <dbaum@google.com> -# Copyright (c) 2017-2018 Ashley Whetter <ashley@awhetter.co.uk> -# Copyright (c) 2017 Łukasz Rogalski <rogalski.91@gmail.com> +# Copyright (c) 2017-2020 Ashley Whetter <ashley@awhetter.co.uk> +# Copyright (c) 2017, 2019 Łukasz Rogalski <rogalski.91@gmail.com> # Copyright (c) 2017 rr- <rr-@sakuya.pl> +# Copyright (c) 2018-2019 hippo91 <guillaume.peillex@gmail.com> +# Copyright (c) 2018 Nick Drozd <nicholasdrozd@gmail.com> +# Copyright (c) 2018 Ville Skyttä <ville.skytta@iki.fi> # Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com> # Copyright (c) 2018 brendanator <brendan.maginnis@gmail.com> -# Copyright (c) 2018 Nick Drozd <nicholasdrozd@gmail.com> # Copyright (c) 2018 HoverHell <hoverhell@gmail.com> +# Copyright (c) 2019 kavins14 <kavin.singh@mail.utoronto.ca> +# Copyright (c) 2019 kavins14 <kavinsingh@hotmail.com> # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER @@ -897,7 +901,7 @@ class NodeNG: _repr_tree(self, result, set()) return "".join(result) - def bool_value(self): + def bool_value(self, context=None): """Determine the boolean value of this node. The boolean value of a node can have three @@ -986,7 +990,7 @@ class _BaseContainer( :type: list(NodeNG) """ - super(_BaseContainer, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) def postinit(self, elts): """Do some setup after initialisation. @@ -1021,7 +1025,7 @@ class _BaseContainer( """ return self.elts - def bool_value(self): + def bool_value(self, context=None): """Determine the boolean value of this node. :returns: The boolean value of this node. @@ -1145,10 +1149,9 @@ class LookupMixIn: _stmts = [] _stmt_parents = [] statements = self._get_filtered_node_statements(stmts) - for node, stmt in statements: # line filtering is on and we have reached our location, break - if stmt.fromlineno > mylineno > 0: + if stmt.fromlineno and stmt.fromlineno > mylineno > 0: break # Ignore decorators with the same name as the # decorated function @@ -1282,7 +1285,7 @@ class AssignName( :type: str or None """ - super(AssignName, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) class DelName( @@ -1322,7 +1325,7 @@ class DelName( :type: str or None """ - super(DelName, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) class Name(mixins.NoChildrenMixin, LookupMixIn, NodeNG): @@ -1363,7 +1366,7 @@ class Name(mixins.NoChildrenMixin, LookupMixIn, NodeNG): :type: str or None """ - super(Name, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) def _get_name_nodes(self): yield self @@ -1395,18 +1398,21 @@ class Arguments(mixins.AssignTypeMixin, NodeNG): # - we expose 'annotation', a list with annotations for # for each normal argument. If an argument doesn't have an # annotation, its value will be None. - + # pylint: disable=too-many-instance-attributes _astroid_fields = ( "args", "defaults", "kwonlyargs", "posonlyargs", + "posonlyargs_annotations", "kw_defaults", "annotations", "varargannotation", "kwargannotation", "kwonlyargs_annotations", "type_comment_args", + "type_comment_kwonlyargs", + "type_comment_posonlyargs", ) varargannotation = None """The type annotation for the variable length arguments. @@ -1432,7 +1438,7 @@ class Arguments(mixins.AssignTypeMixin, NodeNG): :param parent: The parent node in the syntax tree. :type parent: NodeNG or None """ - super(Arguments, self).__init__(parent=parent) + super().__init__(parent=parent) self.vararg = vararg """The name of the variable length arguments. @@ -1502,6 +1508,24 @@ class Arguments(mixins.AssignTypeMixin, NodeNG): :type: list(NodeNG or None) """ + self.type_comment_kwonlyargs = [] + """The type annotation, passed by a type comment, of each keyword only argument. + + If an argument does not have a type comment, + the value for that argument will be None. + + :type: list(NodeNG or None) + """ + + self.type_comment_posonlyargs = [] + """The type annotation, passed by a type comment, of each positional argument. + + If an argument does not have a type comment, + the value for that argument will be None. + + :type: list(NodeNG or None) + """ + # pylint: disable=too-many-arguments def postinit( self, @@ -1516,6 +1540,8 @@ class Arguments(mixins.AssignTypeMixin, NodeNG): varargannotation=None, kwargannotation=None, type_comment_args=None, + type_comment_kwonlyargs=None, + type_comment_posonlyargs=None, ): """Do some setup after initialisation. @@ -1563,6 +1589,14 @@ class Arguments(mixins.AssignTypeMixin, NodeNG): :param type_comment_args: The type annotation, passed by a type comment, of each argument. :type type_comment_args: list(NodeNG or None) + + :param type_comment_args: The type annotation, + passed by a type comment, of each keyword only argument. + :type type_comment_args: list(NodeNG or None) + + :param type_comment_args: The type annotation, + passed by a type comment, of each positional argument. + :type type_comment_args: list(NodeNG or None) """ self.args = args self.defaults = defaults @@ -1575,6 +1609,8 @@ class Arguments(mixins.AssignTypeMixin, NodeNG): self.varargannotation = varargannotation self.kwargannotation = kwargannotation self.type_comment_args = type_comment_args + self.type_comment_kwonlyargs = type_comment_kwonlyargs + self.type_comment_posonlyargs = type_comment_posonlyargs # pylint: disable=too-many-arguments @@ -1589,7 +1625,7 @@ class Arguments(mixins.AssignTypeMixin, NodeNG): :type: int or None """ - lineno = super(Arguments, self).fromlineno + lineno = super().fromlineno return max(lineno, self.parent.fromlineno or 0) @decorators.cachedproperty @@ -1612,7 +1648,13 @@ class Arguments(mixins.AssignTypeMixin, NodeNG): positional_only_defaults = self.defaults[: len(self.defaults) - len(args)] if self.posonlyargs: - result.append(_format_args(self.posonlyargs, positional_only_defaults)) + result.append( + _format_args( + self.posonlyargs, + positional_only_defaults, + self.posonlyargs_annotations, + ) + ) result.append("/") if self.args: result.append( @@ -1695,6 +1737,11 @@ class Arguments(mixins.AssignTypeMixin, NodeNG): def get_children(self): yield from self.posonlyargs or () + + for elt in self.posonlyargs_annotations: + if elt is not None: + yield elt + yield from self.args or () yield from self.defaults @@ -1798,7 +1845,7 @@ class AssignAttr(mixins.ParentAssignTypeMixin, NodeNG): :type: str or None """ - super(AssignAttr, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) def postinit(self, expr=None): """Do some setup after initialisation. @@ -2017,7 +2064,7 @@ class AugAssign(mixins.AssignTypeMixin, Statement): :type: str or None """ - super(AugAssign, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) def postinit(self, target=None, value=None): """Do some setup after initialisation. @@ -2130,7 +2177,7 @@ class BinOp(NodeNG): :type: str or None """ - super(BinOp, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) def postinit(self, left=None, right=None): """Do some setup after initialisation. @@ -2218,7 +2265,7 @@ class BoolOp(NodeNG): :type: str or None """ - super(BoolOp, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) def postinit(self, values=None): """Do some setup after initialisation. @@ -2416,7 +2463,7 @@ class Comprehension(NodeNG): :param parent: The parent node in the syntax tree. :type parent: NodeNG or None """ - super(Comprehension, self).__init__() + super().__init__() self.parent = parent # pylint: disable=redefined-builtin; same name as builtin ast module. @@ -2513,7 +2560,7 @@ class Const(mixins.NoChildrenMixin, NodeNG, bases.Instance): :type: object """ - super(Const, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) def __getattr__(self, name): # This is needed because of Proxy's __getattr__ method. @@ -2592,7 +2639,7 @@ class Const(mixins.NoChildrenMixin, NodeNG, bases.Instance): """ return self._proxied.qname() - def bool_value(self): + def bool_value(self, context=None): """Determine the boolean value of this node. :returns: The boolean value of this node. @@ -2694,7 +2741,7 @@ class DelAttr(mixins.ParentAssignTypeMixin, NodeNG): :type: str or None """ - super(DelAttr, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) def postinit(self, expr=None): """Do some setup after initialisation. @@ -2767,7 +2814,7 @@ class Dict(NodeNG, bases.Instance): :type: list(tuple(NodeNG, NodeNG)) """ - super(Dict, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) def postinit(self, items): """Do some setup after initialisation. @@ -2865,7 +2912,7 @@ class Dict(NodeNG, bases.Instance): raise exceptions.AstroidIndexError(index) - def bool_value(self): + def bool_value(self, context=None): """Determine the boolean value of this node. :returns: The boolean value of this node. @@ -2920,7 +2967,7 @@ class Ellipsis(mixins.NoChildrenMixin, NodeNG): # pylint: disable=redefined-bui <Ellipsis l.1 at 0x7f23b2e35160> """ - def bool_value(self): + def bool_value(self, context=None): """Determine the boolean value of this node. :returns: The boolean value of this node. @@ -3299,7 +3346,7 @@ class ImportFrom(mixins.NoChildrenMixin, mixins.ImportFromMixin, Statement): :type: int """ - super(ImportFrom, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) class Attribute(NodeNG): @@ -3334,7 +3381,7 @@ class Attribute(NodeNG): :type: str or None """ - super(Attribute, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) def postinit(self, expr=None): """Do some setup after initialisation. @@ -3379,7 +3426,7 @@ class Global(mixins.NoChildrenMixin, Statement): :type: list(str) """ - super(Global, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) def _infer_name(self, frame, name): return name @@ -3460,6 +3507,11 @@ class If(mixins.MultiLineBlockMixin, mixins.BlockRangeMixIn, Statement): def has_elif_block(self): return len(self.orelse) == 1 and isinstance(self.orelse[0], If) + def _get_yield_nodes_skip_lambdas(self): + """An If node can contain a Yield node in the test""" + yield from self.test._get_yield_nodes_skip_lambdas() + yield from super()._get_yield_nodes_skip_lambdas() + class IfExp(NodeNG): """Class representing an :class:`ast.IfExp` node. @@ -3547,7 +3599,7 @@ class Import(mixins.NoChildrenMixin, mixins.ImportFromMixin, Statement): :type: list(tuple(str, str or None)) or None """ - super(Import, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) class Index(NodeNG): @@ -3620,7 +3672,7 @@ class Keyword(NodeNG): :type: Name or None """ - super(Keyword, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) def postinit(self, value=None): """Do some setup after initialisation. @@ -3665,7 +3717,7 @@ class List(_BaseContainer): :type: Context or None """ - super(List, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) def pytype(self): """Get the name of the type that this node represents. @@ -3720,7 +3772,7 @@ class Nonlocal(mixins.NoChildrenMixin, Statement): :type: list(str) """ - super(Nonlocal, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) def _infer_name(self, frame, name): return name @@ -3776,7 +3828,7 @@ class Print(Statement): :type: bool or None """ - super(Print, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) def postinit(self, dest=None, values=None): """Do some setup after initialisation. @@ -4029,9 +4081,7 @@ class Starred(mixins.ParentAssignTypeMixin, NodeNG): :type: Context or None """ - super(Starred, self).__init__( - lineno=lineno, col_offset=col_offset, parent=parent - ) + super().__init__(lineno=lineno, col_offset=col_offset, parent=parent) def postinit(self, value=None): """Do some setup after initialisation. @@ -4087,9 +4137,7 @@ class Subscript(NodeNG): :type: Context or None """ - super(Subscript, self).__init__( - lineno=lineno, col_offset=col_offset, parent=parent - ) + super().__init__(lineno=lineno, col_offset=col_offset, parent=parent) # pylint: disable=redefined-builtin; had to use the same name as builtin ast module. def postinit(self, value=None, slice=None): @@ -4282,7 +4330,7 @@ class Tuple(_BaseContainer): :type: Context or None """ - super(Tuple, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) def pytype(self): """Get the name of the type that this node represents. @@ -4338,7 +4386,7 @@ class UnaryOp(NodeNG): :type: str or None """ - super(UnaryOp, self).__init__(lineno, col_offset, parent) + super().__init__(lineno, col_offset, parent) def postinit(self, operand=None): """Do some setup after initialisation. @@ -4452,6 +4500,11 @@ class While(mixins.MultiLineBlockMixin, mixins.BlockRangeMixIn, Statement): yield from self.body yield from self.orelse + def _get_yield_nodes_skip_lambdas(self): + """A While node can contain a Yield node in the test""" + yield from self.test._get_yield_nodes_skip_lambdas() + yield from super()._get_yield_nodes_skip_lambdas() + class With( mixins.MultiLineBlockMixin, @@ -4694,6 +4747,42 @@ class Unknown(mixins.AssignTypeMixin, NodeNG): yield util.Uninferable +class EvaluatedObject(NodeNG): + """Contains an object that has already been inferred + + This class is useful to pre-evaluate a particular node, + with the resulting class acting as the non-evaluated node. + """ + + name = "EvaluatedObject" + _astroid_fields = ("original",) + _other_fields = ("value",) + + original = None + """The original node that has already been evaluated + + :type: NodeNG + """ + + value = None + """The inferred value + + :type: Union[Uninferable, NodeNG] + """ + + def __init__(self, original, value): + self.original = original + self.value = value + super().__init__( + lineno=self.original.lineno, + col_offset=self.original.col_offset, + parent=self.original.parent, + ) + + def infer(self, context=None, **kwargs): + yield self.value + + # constants ############################################################## CONST_CLS = { |
