Skip to content

inspect: Fix isgenerator logic. #998

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

Merged
merged 3 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions python-stdlib/inspect/inspect.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sys

_g = lambda: (yield)


def getmembers(obj, pred=None):
res = []
Expand All @@ -16,11 +18,16 @@ def isfunction(obj):


def isgeneratorfunction(obj):
return isinstance(obj, type(lambda: (yield)))
return isinstance(obj, type(_g))


def isgenerator(obj):
return isinstance(obj, type(lambda: (yield)()))
return isinstance(obj, type((_g)()))


# In MicroPython there's currently no way to distinguish between generators and coroutines.
iscoroutinefunction = isgeneratorfunction
iscoroutine = isgenerator


class _Class:
Expand Down
2 changes: 1 addition & 1 deletion python-stdlib/inspect/manifest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
metadata(version="0.1.2")
metadata(version="0.1.3")

module("inspect.py")
60 changes: 60 additions & 0 deletions python-stdlib/inspect/test_inspect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import inspect
import unittest


def fun():
return 1


def gen():
yield 1


class Class:
def meth(self):
pass


entities = (
fun,
gen,
gen(),
Class,
Class.meth,
Class().meth,
inspect,
)


class TestInspect(unittest.TestCase):
def _test_is_helper(self, f, *entities_true):
for entity in entities:
result = f(entity)
if entity in entities_true:
self.assertTrue(result)
else:
self.assertFalse(result)

def test_isfunction(self):
self._test_is_helper(inspect.isfunction, entities[0], entities[4])

def test_isgeneratorfunction(self):
self._test_is_helper(inspect.isgeneratorfunction, entities[1])

def test_isgenerator(self):
self._test_is_helper(inspect.isgenerator, entities[2])

def test_iscoroutinefunction(self):
self._test_is_helper(inspect.iscoroutinefunction, entities[1])

def test_iscoroutine(self):
self._test_is_helper(inspect.iscoroutine, entities[2])

def test_ismethod(self):
self._test_is_helper(inspect.ismethod, entities[5])

def test_isclass(self):
self._test_is_helper(inspect.isclass, entities[3])

def test_ismodule(self):
self._test_is_helper(inspect.ismodule, entities[6])
1 change: 1 addition & 0 deletions tools/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function ci_package_tests_run {
python-stdlib/datetime \
python-stdlib/fnmatch \
python-stdlib/hashlib \
python-stdlib/inspect \
python-stdlib/pathlib \
python-stdlib/quopri \
python-stdlib/shutil \
Expand Down
Loading