Skip to content

Commit 2cc378a

Browse files
committed
wrap GeneratorModel methods into BoundMethod; remove redundant test
The LookupTest.test_generator_attributes contains outdated Python 2 code (doesn't run on Python 3). The test is superceded by GeneratorModelTest.test_model.
1 parent eb88dfe commit 2cc378a

File tree

2 files changed

+10
-28
lines changed

2 files changed

+10
-28
lines changed

astroid/interpreter/objectmodel.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -695,19 +695,19 @@ def attr___self__(self):
695695

696696

697697
class GeneratorModel(FunctionModel, ContextManagerModel):
698-
def __new__(cls, *args, **kwargs):
698+
def __init__(self):
699699
# Append the values from the GeneratorType unto this object.
700-
ret = super().__new__(cls, *args, **kwargs)
700+
super().__init__()
701701
generator = AstroidManager().builtins_module["generator"]
702702
for name, values in generator.locals.items():
703703
method = values[0]
704+
if isinstance(method, nodes.FunctionDef):
705+
method = bases.BoundMethod(method, _get_bound_node(self))
704706

705707
def patched(cls, meth=method):
706708
return meth
707709

708-
setattr(type(ret), IMPL_PREFIX + name, property(patched))
709-
710-
return ret
710+
setattr(type(self), IMPL_PREFIX + name, property(patched))
711711

712712
@property
713713
def attr___name__(self):
@@ -724,9 +724,9 @@ def attr___doc__(self):
724724

725725

726726
class AsyncGeneratorModel(GeneratorModel):
727-
def __new__(cls, *args, **kwargs):
727+
def __init__(self):
728728
# Append the values from the AGeneratorType unto this object.
729-
ret = super().__new__(cls, *args, **kwargs)
729+
super().__init__()
730730
astroid_builtins = AstroidManager().builtins_module
731731
generator = astroid_builtins.get("async_generator")
732732
if generator is None:
@@ -735,13 +735,13 @@ def __new__(cls, *args, **kwargs):
735735

736736
for name, values in generator.locals.items():
737737
method = values[0]
738+
if isinstance(method, nodes.FunctionDef):
739+
method = bases.BoundMethod(method, _get_bound_node(self))
738740

739741
def patched(cls, meth=method):
740742
return meth
741743

742-
setattr(type(ret), IMPL_PREFIX + name, property(patched))
743-
744-
return ret
744+
setattr(type(self), IMPL_PREFIX + name, property(patched))
745745

746746

747747
class InstanceModel(ObjectModel):

tests/test_lookup.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -322,24 +322,6 @@ class _Inner:
322322
self.assertEqual(len(name.lookup("x")[1]), 1, repr(name))
323323
self.assertEqual(name.lookup("x")[1][0].lineno, 3, repr(name))
324324

325-
def test_generator_attributes(self) -> None:
326-
tree = builder.parse(
327-
"""
328-
def count():
329-
"test"
330-
yield 0
331-
332-
iterer = count()
333-
num = iterer.next()
334-
"""
335-
)
336-
next_node = tree.body[2].value.func
337-
gener = next_node.expr.inferred()[0]
338-
self.assertIsInstance(gener.getattr("__next__")[0], nodes.FunctionDef)
339-
self.assertIsInstance(gener.getattr("send")[0], nodes.FunctionDef)
340-
self.assertIsInstance(gener.getattr("throw")[0], nodes.FunctionDef)
341-
self.assertIsInstance(gener.getattr("close")[0], nodes.FunctionDef)
342-
343325
def test_explicit___name__(self) -> None:
344326
code = """
345327
class Pouet:

0 commit comments

Comments
 (0)