mirror of https://github.com/python/cpython
gh-125633: Add function `ispackage` to stdlib `inspect` (#125634)
--------- Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
This commit is contained in:
parent
c51b56038b
commit
dad3453129
|
@ -374,6 +374,13 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
|
||||||
Return ``True`` if the object is a bound method written in Python.
|
Return ``True`` if the object is a bound method written in Python.
|
||||||
|
|
||||||
|
|
||||||
|
.. function:: ispackage(object)
|
||||||
|
|
||||||
|
Return ``True`` if the object is a :term:`package`.
|
||||||
|
|
||||||
|
.. versionadded:: 3.14
|
||||||
|
|
||||||
|
|
||||||
.. function:: isfunction(object)
|
.. function:: isfunction(object)
|
||||||
|
|
||||||
Return ``True`` if the object is a Python function, which includes functions
|
Return ``True`` if the object is a Python function, which includes functions
|
||||||
|
|
|
@ -326,6 +326,10 @@ inspect
|
||||||
If true, string :term:`annotations <annotation>` are displayed without surrounding quotes.
|
If true, string :term:`annotations <annotation>` are displayed without surrounding quotes.
|
||||||
(Contributed by Jelle Zijlstra in :gh:`101552`.)
|
(Contributed by Jelle Zijlstra in :gh:`101552`.)
|
||||||
|
|
||||||
|
* Add function :func:`inspect.ispackage` to determine whether an object is a
|
||||||
|
:term:`package` or not.
|
||||||
|
(Contributed by Zhikang Yan in :gh:`125634`.)
|
||||||
|
|
||||||
|
|
||||||
json
|
json
|
||||||
----
|
----
|
||||||
|
|
|
@ -6,9 +6,9 @@ It also provides some help for examining source code and class layout.
|
||||||
|
|
||||||
Here are some of the useful functions provided by this module:
|
Here are some of the useful functions provided by this module:
|
||||||
|
|
||||||
ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(),
|
ismodule(), isclass(), ismethod(), ispackage(), isfunction(),
|
||||||
isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(),
|
isgeneratorfunction(), isgenerator(), istraceback(), isframe(),
|
||||||
isroutine() - check object types
|
iscode(), isbuiltin(), isroutine() - check object types
|
||||||
getmembers() - get members of an object that satisfy a given condition
|
getmembers() - get members of an object that satisfy a given condition
|
||||||
|
|
||||||
getfile(), getsourcefile(), getsource() - find an object's source code
|
getfile(), getsourcefile(), getsource() - find an object's source code
|
||||||
|
@ -128,6 +128,7 @@ __all__ = [
|
||||||
"ismethoddescriptor",
|
"ismethoddescriptor",
|
||||||
"ismethodwrapper",
|
"ismethodwrapper",
|
||||||
"ismodule",
|
"ismodule",
|
||||||
|
"ispackage",
|
||||||
"isroutine",
|
"isroutine",
|
||||||
"istraceback",
|
"istraceback",
|
||||||
"markcoroutinefunction",
|
"markcoroutinefunction",
|
||||||
|
@ -186,6 +187,10 @@ def ismethod(object):
|
||||||
"""Return true if the object is an instance method."""
|
"""Return true if the object is an instance method."""
|
||||||
return isinstance(object, types.MethodType)
|
return isinstance(object, types.MethodType)
|
||||||
|
|
||||||
|
def ispackage(object):
|
||||||
|
"""Return true if the object is a package."""
|
||||||
|
return ismodule(object) and hasattr(object, "__path__")
|
||||||
|
|
||||||
def ismethoddescriptor(object):
|
def ismethoddescriptor(object):
|
||||||
"""Return true if the object is a method descriptor.
|
"""Return true if the object is a method descriptor.
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ from test.test_inspect import inspect_deferred_annotations
|
||||||
|
|
||||||
# Functions tested in this suite:
|
# Functions tested in this suite:
|
||||||
# ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
|
# ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
|
||||||
# isbuiltin, isroutine, isgenerator, isgeneratorfunction, getmembers,
|
# isbuiltin, isroutine, isgenerator, ispackage, isgeneratorfunction, getmembers,
|
||||||
# getdoc, getfile, getmodule, getsourcefile, getcomments, getsource,
|
# getdoc, getfile, getmodule, getsourcefile, getcomments, getsource,
|
||||||
# getclasstree, getargvalues, formatargvalues, currentframe,
|
# getclasstree, getargvalues, formatargvalues, currentframe,
|
||||||
# stack, trace, ismethoddescriptor, isdatadescriptor, ismethodwrapper
|
# stack, trace, ismethoddescriptor, isdatadescriptor, ismethodwrapper
|
||||||
|
@ -105,7 +105,7 @@ unsorted_keyword_only_parameters = 'throw out the baby with_ the_ bathwater'.spl
|
||||||
class IsTestBase(unittest.TestCase):
|
class IsTestBase(unittest.TestCase):
|
||||||
predicates = set([inspect.isbuiltin, inspect.isclass, inspect.iscode,
|
predicates = set([inspect.isbuiltin, inspect.isclass, inspect.iscode,
|
||||||
inspect.isframe, inspect.isfunction, inspect.ismethod,
|
inspect.isframe, inspect.isfunction, inspect.ismethod,
|
||||||
inspect.ismodule, inspect.istraceback,
|
inspect.ismodule, inspect.istraceback, inspect.ispackage,
|
||||||
inspect.isgenerator, inspect.isgeneratorfunction,
|
inspect.isgenerator, inspect.isgeneratorfunction,
|
||||||
inspect.iscoroutine, inspect.iscoroutinefunction,
|
inspect.iscoroutine, inspect.iscoroutinefunction,
|
||||||
inspect.isasyncgen, inspect.isasyncgenfunction,
|
inspect.isasyncgen, inspect.isasyncgenfunction,
|
||||||
|
@ -121,7 +121,10 @@ class IsTestBase(unittest.TestCase):
|
||||||
predicate == inspect.iscoroutinefunction) and \
|
predicate == inspect.iscoroutinefunction) and \
|
||||||
other == inspect.isfunction:
|
other == inspect.isfunction:
|
||||||
continue
|
continue
|
||||||
self.assertFalse(other(obj), 'not %s(%s)' % (other.__name__, exp))
|
if predicate == inspect.ispackage and other == inspect.ismodule:
|
||||||
|
self.assertTrue(predicate(obj), '%s(%s)' % (predicate.__name__, exp))
|
||||||
|
else:
|
||||||
|
self.assertFalse(other(obj), 'not %s(%s)' % (other.__name__, exp))
|
||||||
|
|
||||||
def test__all__(self):
|
def test__all__(self):
|
||||||
support.check__all__(self, inspect, not_exported=("modulesbyfile",), extra=("get_annotations",))
|
support.check__all__(self, inspect, not_exported=("modulesbyfile",), extra=("get_annotations",))
|
||||||
|
@ -201,7 +204,17 @@ class TestPredicates(IsTestBase):
|
||||||
self.assertFalse(inspect.ismethodwrapper(int))
|
self.assertFalse(inspect.ismethodwrapper(int))
|
||||||
self.assertFalse(inspect.ismethodwrapper(type("AnyClass", (), {})))
|
self.assertFalse(inspect.ismethodwrapper(type("AnyClass", (), {})))
|
||||||
|
|
||||||
|
def test_ispackage(self):
|
||||||
|
self.istest(inspect.ispackage, 'asyncio')
|
||||||
|
self.istest(inspect.ispackage, 'importlib')
|
||||||
|
self.assertFalse(inspect.ispackage(inspect))
|
||||||
|
self.assertFalse(inspect.ispackage(mod))
|
||||||
|
self.assertFalse(inspect.ispackage(':)'))
|
||||||
|
|
||||||
|
class FakePackage:
|
||||||
|
__path__ = None
|
||||||
|
|
||||||
|
self.assertFalse(inspect.ispackage(FakePackage()))
|
||||||
|
|
||||||
def test_iscoroutine(self):
|
def test_iscoroutine(self):
|
||||||
async_gen_coro = async_generator_function_example(1)
|
async_gen_coro = async_generator_function_example(1)
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Add function :func:`inspect.ispackage` to determine whether an object is a
|
||||||
|
:term:`package` or not.
|
Loading…
Reference in New Issue