Skip to content

Commit

Permalink
Add __init__ to the ObjectModel
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed Jul 6, 2022
1 parent 1e8013d commit b4e4a78
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
7 changes: 5 additions & 2 deletions astroid/interpreter/objectmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from typing import TYPE_CHECKING

import astroid
from astroid import util
from astroid import nodes, util
from astroid.context import InferenceContext, copy_context
from astroid.exceptions import AttributeInferenceError, InferenceError, NoDefault
from astroid.manager import AstroidManager
Expand Down Expand Up @@ -131,6 +131,10 @@ def attr___new__(self):
except AttributeError:
return self._instance.instantiate_class()

@property
def attr___init__(self) -> nodes.Const:
return nodes.Const(None)


class ModuleModel(ObjectModel):
def _builtins(self):
Expand Down Expand Up @@ -409,7 +413,6 @@ def attr___ne__(self):
attr___delattr___ = attr___ne__
attr___getattribute__ = attr___ne__
attr___hash__ = attr___ne__
attr___init__ = attr___ne__
attr___dir__ = attr___ne__
attr___call__ = attr___ne__
attr___class__ = attr___ne__
Expand Down
74 changes: 74 additions & 0 deletions tests/unittest_object_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,27 @@ def test_module_model(self) -> None:
xml.__cached__ #@
xml.__package__ #@
xml.__dict__ #@
xml.__init__ #@
xml.__new__ #@
xml.__subclasshook__ #@
xml.__str__ #@
xml.__sizeof__ #@
xml.__repr__ #@
xml.__reduce__ #@
xml.__setattr__ #@
xml.__reduce_ex__ #@
xml.__lt__ #@
xml.__eq__ #@
xml.__gt__ #@
xml.__format__ #@
xml.__delattr___ #@
xml.__getattribute__ #@
xml.__hash__ #@
xml.__dir__ #@
xml.__call__ #@
xml.__closure__ #@
"""
)
assert isinstance(ast_nodes, list)
Expand Down Expand Up @@ -284,6 +305,20 @@ def test_module_model(self) -> None:
dict_ = next(ast_nodes[8].infer())
self.assertIsInstance(dict_, astroid.Dict)

init_ = next(ast_nodes[9].infer())
assert isinstance(init_, nodes.Const)
assert init_.value is None

new_ = next(ast_nodes[10].infer())
assert isinstance(new_, nodes.Module)
assert new_.name == "xml"

# The following nodes are just here for theoretical completeness,
# and they either return Uninferable or raise InferenceError.
for ast_node in ast_nodes[11:28]:
with pytest.raises(InferenceError):
next(ast_node.infer())


class FunctionModelTest(unittest.TestCase):
def test_partial_descriptor_support(self) -> None:
Expand Down Expand Up @@ -394,6 +429,27 @@ def func(a=1, b=2):
func.__globals__ #@
func.__code__ #@
func.__closure__ #@
func.__init__ #@
func.__new__ #@
func.__subclasshook__ #@
func.__str__ #@
func.__sizeof__ #@
func.__repr__ #@
func.__reduce__ #@
func.__reduce_ex__ #@
func.__lt__ #@
func.__eq__ #@
func.__gt__ #@
func.__format__ #@
func.__delattr___ #@
func.__getattribute__ #@
func.__hash__ #@
func.__dir__ #@
func.__class__ #@
func.__setattr__ #@
''',
module_name="fake_module",
)
Expand Down Expand Up @@ -427,6 +483,24 @@ def func(a=1, b=2):
for ast_node in ast_nodes[7:9]:
self.assertIs(next(ast_node.infer()), astroid.Uninferable)

init_ = next(ast_nodes[9].infer())
assert isinstance(init_, nodes.Const)
assert init_.value is None

new_ = next(ast_nodes[10].infer())
assert isinstance(new_, nodes.FunctionDef)
assert new_.name == "func"

# The following nodes are just here for theoretical completeness,
# and they either return Uninferable or raise InferenceError.
for ast_node in ast_nodes[11:26]:
inferred = next(ast_node.infer())
assert inferred is util.Uninferable

for ast_node in ast_nodes[26:27]:
with pytest.raises(InferenceError):
inferred = next(ast_node.infer())

def test_empty_return_annotation(self) -> None:
ast_node = builder.extract_node(
"""
Expand Down

0 comments on commit b4e4a78

Please sign in to comment.