Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue#6305 Replace deprecated ast.NameConstant usage and add tests #6306

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion scrapy/utils/misc.py
Expand Up @@ -264,7 +264,7 @@ def is_generator_with_return_value(callable: Callable) -> bool:
def returns_none(return_node: ast.Return) -> bool:
value = return_node.value
return (
value is None or isinstance(value, ast.NameConstant) and value.value is None
value is None or isinstance(value, ast.Constant) and value.value is None
)

if inspect.isgeneratorfunction(callable):
Expand Down
Expand Up @@ -12,6 +12,13 @@
def _indentation_error(*args, **kwargs):
raise IndentationError()

def create_ast_return(value=None):
return (
unittest.mock.MagicMock(name='ast.Return', value=None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm my point is that I expect this test to test real code that produces specific ast nodes when parsed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(hpefully that makes sense)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that makes sense. I will push a new change tomorrow. Thank you for the feedback!

if value is None
else unittest.mock.MagicMock(name='ast.Return',
value=unittest.mock.MagicMock(name='ast.Constant', value=value))
)

def top_level_return_something():
"""
Expand Down Expand Up @@ -70,11 +77,16 @@ def i1():
yield url
return 1

def j1():
yield 1
return create_ast_return(value=42)

assert is_generator_with_return_value(top_level_return_something)
assert is_generator_with_return_value(f1)
assert is_generator_with_return_value(g1)
assert is_generator_with_return_value(h1)
assert is_generator_with_return_value(i1)
assert is_generator_with_return_value(j1)

with warnings.catch_warnings(record=True) as w:
warn_on_generator_with_return_value(None, top_level_return_something)
Expand Down Expand Up @@ -137,6 +149,9 @@ def k2():
def l2():
return

def m2():
return create_ast_return()

assert not is_generator_with_return_value(top_level_return_none)
assert not is_generator_with_return_value(f2)
assert not is_generator_with_return_value(g2)
Expand All @@ -145,6 +160,7 @@ def l2():
assert not is_generator_with_return_value(j2) # not recursive
assert not is_generator_with_return_value(k2) # not recursive
assert not is_generator_with_return_value(l2)
assert not is_generator_with_return_value(m2)

with warnings.catch_warnings(record=True) as w:
warn_on_generator_with_return_value(None, top_level_return_none)
Expand Down