Skip to content

Commit

Permalink
Fix constructors of Super
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed Apr 23, 2023
1 parent de0751c commit 30df5a8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Release date: TBA
- ``nodes.SetComp``
- ``nodes.Slice``
- ``nodes.Starred``
- ``nodes.Super``, we also added the ``call`` parameter to its ``__init__`` method.
- ``nodes.TryExcept``
- ``nodes.Subscript``
- ``nodes.UnaryOp``
Expand Down
11 changes: 9 additions & 2 deletions astroid/brain/brain_builtin_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,9 @@ def infer_dict(node, context: InferenceContext | None = None):
return value


def infer_super(node, context: InferenceContext | None = None):
def infer_super(
node: nodes.Call, context: InferenceContext | None = None
) -> objects.Super:
"""Understand super calls.
There are some restrictions for what can be understood:
Expand All @@ -405,6 +407,7 @@ def infer_super(node, context: InferenceContext | None = None):
raise UseInferenceDefault

cls = scoped_nodes.get_wrapping_class(scope)
assert cls is not None
if not node.args:
mro_pointer = cls
# In we are in a classmethod, the interpreter will fill
Expand All @@ -430,7 +433,11 @@ def infer_super(node, context: InferenceContext | None = None):
raise UseInferenceDefault

super_obj = objects.Super(
mro_pointer=mro_pointer, mro_type=mro_type, self_class=cls, scope=scope
mro_pointer=mro_pointer,
mro_type=mro_type,
self_class=cls,
scope=scope,
call=node,
)
super_obj.parent = node
return super_obj
Expand Down
31 changes: 20 additions & 11 deletions astroid/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@
MroError,
SuperError,
)
from astroid.interpreter import objectmodel
from astroid.manager import AstroidManager
from astroid.nodes import node_classes, scoped_nodes
from astroid.typing import InferenceResult

objectmodel = util.lazy_import("interpreter.objectmodel")
from astroid.typing import InferenceResult, SuccessfulInferenceResult

if sys.version_info >= (3, 8):
from functools import cached_property
Expand Down Expand Up @@ -70,16 +69,28 @@ class Super(node_classes.NodeNG):
*scope* is the function where the super call is.
"""

# pylint: disable=unnecessary-lambda
special_attributes = util.lazy_descriptor(lambda: objectmodel.SuperModel())
special_attributes = objectmodel.SuperModel()

def __init__(self, mro_pointer, mro_type, self_class, scope):
def __init__(
self,
mro_pointer: SuccessfulInferenceResult,
mro_type: SuccessfulInferenceResult,
self_class: scoped_nodes.ClassDef,
scope: scoped_nodes.FunctionDef,
call: node_classes.Call,
) -> None:
self.type = mro_type
self.mro_pointer = mro_pointer
self._class_based = False
self._self_class = self_class
self._scope = scope
super().__init__()
super().__init__(
parent=scope,
lineno=scope.lineno,
col_offset=scope.col_offset,
end_lineno=scope.end_lineno,
end_col_offset=scope.end_col_offset,
)

def _infer(self, context: InferenceContext | None = None, **kwargs: Any):
yield self
Expand Down Expand Up @@ -249,8 +260,7 @@ class DictInstance(bases.Instance):
that methods such as .values or .items can be properly inferred.
"""

# pylint: disable=unnecessary-lambda
special_attributes = util.lazy_descriptor(lambda: objectmodel.DictModel())
special_attributes = objectmodel.DictModel()


# Custom objects tailored for dictionaries, which are used to
Expand Down Expand Up @@ -348,8 +358,7 @@ def __init__(
# Assigned directly to prevent triggering the DeprecationWarning.
self._doc = doc

# pylint: disable=unnecessary-lambda
special_attributes = util.lazy_descriptor(lambda: objectmodel.PropertyModel())
special_attributes = objectmodel.PropertyModel()
type = "property"

def pytype(self) -> Literal["builtins.property"]:
Expand Down

0 comments on commit 30df5a8

Please sign in to comment.