Skip to content

Commit 41f7f60

Browse files
committed
Simplify code, rename variables and add test for nested partials
1 parent 633c497 commit 41f7f60

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

Lib/inspect.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -168,40 +168,36 @@ def isfunction(object):
168168
__kwdefaults__ dict of keyword only parameters with defaults"""
169169
return isinstance(object, types.FunctionType)
170170

171-
def isgeneratorfunction(object):
171+
def isgeneratorfunction(obj):
172172
"""Return true if the object is a user-defined generator function.
173173
174174
Generator function objects provide the same attributes as functions.
175175
See help(isfunction) for a list of attributes."""
176-
is_generator_function = bool((isfunction(object) or ismethod(object)) and
177-
object.__code__.co_flags & CO_GENERATOR)
178-
is_partial_generator = bool((isinstance(object, functools.partial) and
179-
object.func.__code__.co_flags & CO_GENERATOR))
180-
return is_generator_function or is_partial_generator
176+
while isinstance(obj, functools.partial):
177+
obj = obj.func
178+
return bool((isfunction(obj) or ismethod(obj)) and
179+
obj.__code__.co_flags & CO_GENERATOR)
181180

182-
def iscoroutinefunction(object):
181+
def iscoroutinefunction(obj):
183182
"""Return true if the object is a coroutine function.
184183
185184
Coroutine functions are defined with "async def" syntax.
186185
"""
187-
is_coroutine_function = bool(((isfunction(object) or ismethod(object)) and
188-
object.__code__.co_flags & CO_COROUTINE))
189-
is_partial_coroutine = bool((isinstance(object, functools.partial) and
190-
object.func.__code__.co_flags & CO_COROUTINE))
191-
return is_coroutine_function or is_partial_coroutine
186+
while isinstance(obj, functools.partial):
187+
obj = obj.func
188+
return bool(((isfunction(obj) or ismethod(obj)) and
189+
obj.__code__.co_flags & CO_COROUTINE))
192190

193-
def isasyncgenfunction(object):
191+
def isasyncgenfunction(obj):
194192
"""Return true if the object is an asynchronous generator function.
195193
196194
Asynchronous generator functions are defined with "async def"
197195
syntax and have "yield" expressions in their body.
198196
"""
199-
is_async_gen_function = bool((isfunction(object) or ismethod(object)) and
200-
object.__code__.co_flags & CO_ASYNC_GENERATOR)
201-
is_partial_async_gen = bool((isinstance(object, functools.partial) and
202-
object.func.__code__.co_flags & CO_ASYNC_GENERATOR))
203-
return is_async_gen_function or is_partial_async_gen
204-
197+
while isinstance(obj, functools.partial):
198+
obj = obj.func
199+
return bool((isfunction(obj) or ismethod(obj)) and
200+
obj.__code__.co_flags & CO_ASYNC_GENERATOR)
205201

206202
def isasyncgen(object):
207203
"""Return true if the object is an asynchronous generator."""

Lib/test/test_inspect.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import collections
33
import datetime
44
import functools
5+
from functools import partial
56
import importlib
67
import inspect
78
import io
@@ -174,35 +175,35 @@ def test_iscoroutine(self):
174175
inspect.iscoroutinefunction(gen_coroutine_function_example))
175176
self.assertFalse(
176177
inspect.iscoroutinefunction(
177-
functools.partial(gen_coroutine_function_example)))
178+
partial(partial(gen_coroutine_function_example))))
178179
self.assertFalse(inspect.iscoroutine(gen_coro))
179180

180181
self.assertTrue(
181182
inspect.isgeneratorfunction(gen_coroutine_function_example))
182183
self.assertTrue(
183184
inspect.isgeneratorfunction(
184-
functools.partial(gen_coroutine_function_example)))
185+
partial(partial(gen_coroutine_function_example))))
185186
self.assertTrue(inspect.isgenerator(gen_coro))
186187

187188
self.assertTrue(
188189
inspect.iscoroutinefunction(coroutine_function_example))
189190
self.assertTrue(
190191
inspect.iscoroutinefunction(
191-
functools.partial(coroutine_function_example)))
192+
partial(partial(coroutine_function_example))))
192193
self.assertTrue(inspect.iscoroutine(coro))
193194

194195
self.assertFalse(
195196
inspect.isgeneratorfunction(coroutine_function_example))
196197
self.assertFalse(
197198
inspect.isgeneratorfunction(
198-
functools.partial(coroutine_function_example)))
199+
partial(partial(coroutine_function_example))))
199200
self.assertFalse(inspect.isgenerator(coro))
200201

201202
self.assertTrue(
202203
inspect.isasyncgenfunction(async_generator_function_example))
203204
self.assertTrue(
204205
inspect.isasyncgenfunction(
205-
functools.partial(async_generator_function_example)))
206+
partial(partial(async_generator_function_example))))
206207
self.assertTrue(inspect.isasyncgen(async_gen_coro))
207208

208209
coro.close(); gen_coro.close(); # silence warnings

0 commit comments

Comments
 (0)