Skip to content

Commit

Permalink
fixup! wrap GeneratorModel methods into BoundMethod; remove redundant…
Browse files Browse the repository at this point in the history
… test
  • Loading branch information
temyurchenko committed Sep 25, 2024
1 parent 2cc378a commit 09bd85c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 28 deletions.
4 changes: 4 additions & 0 deletions astroid/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,10 @@ def __str__(self) -> str:
class AsyncGenerator(Generator):
"""Special node representing an async generator."""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
AsyncGenerator.special_attributes = objectmodel.AsyncGeneratorModel()

def pytype(self) -> Literal["builtins.async_generator"]:
return "builtins.async_generator"

Expand Down
6 changes: 1 addition & 5 deletions astroid/interpreter/objectmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,11 +728,7 @@ def __init__(self):
# Append the values from the AGeneratorType unto this object.
super().__init__()
astroid_builtins = AstroidManager().builtins_module
generator = astroid_builtins.get("async_generator")
if generator is None:
# Make it backward compatible.
generator = astroid_builtins.get("generator")

generator = astroid_builtins["async_generator"]
for name, values in generator.locals.items():
method = values[0]
if isinstance(method, nodes.FunctionDef):
Expand Down
6 changes: 2 additions & 4 deletions astroid/raw_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,9 +627,8 @@ def _astroid_bootstrapping() -> None:
col_offset=0,
end_lineno=0,
end_col_offset=0,
parent=nodes.Unknown(),
parent=astroid_builtin,
)
_GeneratorType.parent = astroid_builtin
generator_doc_node = (
nodes.Const(value=types.GeneratorType.__doc__)
if types.GeneratorType.__doc__
Expand All @@ -651,9 +650,8 @@ def _astroid_bootstrapping() -> None:
col_offset=0,
end_lineno=0,
end_col_offset=0,
parent=nodes.Unknown(),
parent=astroid_builtin,
)
_AsyncGeneratorType.parent = astroid_builtin
async_generator_doc_node = (
nodes.Const(value=types.AsyncGeneratorType.__doc__)
if types.AsyncGeneratorType.__doc__
Expand Down
21 changes: 2 additions & 19 deletions tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,7 @@ def test(self):
assert bool(inferred.is_generator())


class AsyncGeneratorTest:
class AsyncGeneratorTest(unittest.TestCase):
def test_async_generator(self):
node = astroid.extract_node(
"""
Expand All @@ -1466,29 +1466,12 @@ async def a_iter(n):
"""
)
inferred = next(node.infer())
assert isinstance(inferred, bases.AsyncGenerator)
assert isinstance(inferred, bases.Generator)
assert inferred.getattr("__aiter__")
assert inferred.getattr("__anext__")
assert inferred.pytype() == "builtins.async_generator"
assert inferred.display_type() == "AsyncGenerator"

def test_async_generator_is_generator_on_older_python(self):
node = astroid.extract_node(
"""
async def a_iter(n):
for i in range(1, n + 1):
yield i
await asyncio.sleep(1)
a_iter(2) #@
"""
)
inferred = next(node.infer())
assert isinstance(inferred, bases.Generator)
assert inferred.getattr("__iter__")
assert inferred.getattr("__next__")
assert inferred.pytype() == "builtins.generator"
assert inferred.display_type() == "Generator"


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

0 comments on commit 09bd85c

Please sign in to comment.