Merge pull request #5599 from tonal/patch-1

Partial in is_generator_with_return_value
This commit is contained in:
Andrey Rahmatullin 2022-10-31 20:08:16 +05:00 committed by GitHub
commit a95a338eea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -9,6 +9,7 @@ from collections import deque
from contextlib import contextmanager
from importlib import import_module
from pkgutil import iter_modules
from functools import partial
from w3lib.html import replace_entities
@ -226,7 +227,11 @@ def is_generator_with_return_value(callable):
return value is None or isinstance(value, ast.NameConstant) and value.value is None
if inspect.isgeneratorfunction(callable):
src = inspect.getsource(callable)
func = callable
while isinstance(func, partial):
func = func.func
src = inspect.getsource(func)
pattern = re.compile(r"(^[\t ]+)")
code = pattern.sub("", src)

View File

@ -1,5 +1,6 @@
import unittest
import warnings
from functools import partial
from unittest import mock
from scrapy.utils.misc import is_generator_with_return_value, warn_on_generator_with_return_value
@ -254,3 +255,10 @@ https://example.org
warn_on_generator_with_return_value(None, top_level_return_none)
self.assertEqual(len(w), 1)
self.assertIn('Unable to determine', str(w[0].message))
def test_partial(self):
def cb(arg1, arg2):
yield {}
partial_cb = partial(cb, arg1=42)
assert not is_generator_with_return_value(partial_cb)