summaryrefslogtreecommitdiff
path: root/astroid/interpreter
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2022-07-03 18:15:51 +0200
committerGitHub <noreply@github.com>2022-07-03 18:15:51 +0200
commita6fdf0b92af0190dfc8334a69548919cc7130204 (patch)
treec33a18d2a6708573c974741d132cfb5804d7e667 /astroid/interpreter
parentdd80a68fc93d507c3a8182ea5b94f6cbe0e348a7 (diff)
downloadastroid-git-a6fdf0b92af0190dfc8334a69548919cc7130204.tar.gz
Add ``__new__`` and ``__call__`` to``ObjectModel`` and ``ClassModel`` (#1606)
Diffstat (limited to 'astroid/interpreter')
-rw-r--r--astroid/interpreter/objectmodel.py19
1 files changed, 18 insertions, 1 deletions
diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py
index 90085d7d..d120895a 100644
--- a/astroid/interpreter/objectmodel.py
+++ b/astroid/interpreter/objectmodel.py
@@ -118,6 +118,19 @@ class ObjectModel:
return getattr(self, IMPL_PREFIX + name)
raise AttributeInferenceError(target=self._instance, attribute=name)
+ @property
+ def attr___new__(self):
+ """Calling cls.__new__(cls) on an object returns an instance of that object.
+
+ Instance is either an instance or a class definition of the instance to be
+ created.
+ """
+ # TODO: Use isinstance instead of try ... except after _instance has typing
+ try:
+ return self._instance._proxied.instantiate_class()
+ except AttributeError:
+ return self._instance.instantiate_class()
+
class ModuleModel(ObjectModel):
def _builtins(self):
@@ -389,7 +402,6 @@ class FunctionModel(ObjectModel):
attr___repr__ = attr___ne__
attr___reduce__ = attr___ne__
attr___reduce_ex__ = attr___ne__
- attr___new__ = attr___ne__
attr___lt__ = attr___ne__
attr___eq__ = attr___ne__
attr___gt__ = attr___ne__
@@ -512,6 +524,11 @@ class ClassModel(ObjectModel):
def attr___dict__(self):
return node_classes.Dict(parent=self._instance)
+ @property
+ def attr___call__(self):
+ """Calling a class A() returns an instance of A."""
+ return self._instance.instantiate_class()
+
class SuperModel(ObjectModel):
@property