Skip to content

Commit a3f5c4a

Browse files
authored
wrap GeneratorModel methods into BoundMethod; remove redundant test (pylint-dev#2584)
The LookupTest.test_generator_attributes contains outdated Python 2 code (doesn't run on Python 3). The test is superceded by GeneratorModelTest.test_model. Fix AsyncGenerator test and model, they just weren't used before
1 parent eb88dfe commit a3f5c4a

File tree

5 files changed

+18
-55
lines changed

5 files changed

+18
-55
lines changed

astroid/bases.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,10 @@ def __str__(self) -> str:
718718
class AsyncGenerator(Generator):
719719
"""Special node representing an async generator."""
720720

721+
def __init__(self, *args, **kwargs):
722+
super().__init__(*args, **kwargs)
723+
AsyncGenerator.special_attributes = objectmodel.AsyncGeneratorModel()
724+
721725
def pytype(self) -> Literal["builtins.async_generator"]:
722726
return "builtins.async_generator"
723727

astroid/interpreter/objectmodel.py

Lines changed: 11 additions & 15 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,24 +724,20 @@ 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
731-
generator = astroid_builtins.get("async_generator")
732-
if generator is None:
733-
# Make it backward compatible.
734-
generator = astroid_builtins.get("generator")
735-
731+
generator = astroid_builtins["async_generator"]
736732
for name, values in generator.locals.items():
737733
method = values[0]
734+
if isinstance(method, nodes.FunctionDef):
735+
method = bases.BoundMethod(method, _get_bound_node(self))
738736

739737
def patched(cls, meth=method):
740738
return meth
741739

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

746742

747743
class InstanceModel(ObjectModel):

astroid/raw_building.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,8 @@ def _astroid_bootstrapping() -> None:
627627
col_offset=0,
628628
end_lineno=0,
629629
end_col_offset=0,
630-
parent=nodes.Unknown(),
630+
parent=astroid_builtin,
631631
)
632-
_GeneratorType.parent = astroid_builtin
633632
generator_doc_node = (
634633
nodes.Const(value=types.GeneratorType.__doc__)
635634
if types.GeneratorType.__doc__
@@ -651,9 +650,8 @@ def _astroid_bootstrapping() -> None:
651650
col_offset=0,
652651
end_lineno=0,
653652
end_col_offset=0,
654-
parent=nodes.Unknown(),
653+
parent=astroid_builtin,
655654
)
656-
_AsyncGeneratorType.parent = astroid_builtin
657655
async_generator_doc_node = (
658656
nodes.Const(value=types.AsyncGeneratorType.__doc__)
659657
if types.AsyncGeneratorType.__doc__

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:

tests/test_nodes.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,7 @@ def test(self):
14541454
assert bool(inferred.is_generator())
14551455

14561456

1457-
class AsyncGeneratorTest:
1457+
class AsyncGeneratorTest(unittest.TestCase):
14581458
def test_async_generator(self):
14591459
node = astroid.extract_node(
14601460
"""
@@ -1472,23 +1472,6 @@ async def a_iter(n):
14721472
assert inferred.pytype() == "builtins.async_generator"
14731473
assert inferred.display_type() == "AsyncGenerator"
14741474

1475-
def test_async_generator_is_generator_on_older_python(self):
1476-
node = astroid.extract_node(
1477-
"""
1478-
async def a_iter(n):
1479-
for i in range(1, n + 1):
1480-
yield i
1481-
await asyncio.sleep(1)
1482-
a_iter(2) #@
1483-
"""
1484-
)
1485-
inferred = next(node.infer())
1486-
assert isinstance(inferred, bases.Generator)
1487-
assert inferred.getattr("__iter__")
1488-
assert inferred.getattr("__next__")
1489-
assert inferred.pytype() == "builtins.generator"
1490-
assert inferred.display_type() == "Generator"
1491-
14921475

14931476
def test_f_string_correct_line_numbering() -> None:
14941477
"""Test that we generate correct line numbers for f-strings."""

0 commit comments

Comments
 (0)