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

gh-125633: Add function ispackage to stdlib inspect #125634

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
13 changes: 10 additions & 3 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

Here are some of the useful functions provided by this module:

ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(),
isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(),
isroutine() - check object types
ismodule(), isclass(), ismethod(), ispackage(), isfunction(),
isgeneratorfunction(), isgenerator(), istraceback(), isframe(),
iscode(), isbuiltin(), isroutine() - check object types
getmembers() - get members of an object that satisfy a given condition

getfile(), getsourcefile(), getsource() - find an object's source code
Expand Down Expand Up @@ -128,6 +128,7 @@
"ismethoddescriptor",
"ismethodwrapper",
"ismodule",
"ispackage",
"isroutine",
"istraceback",
"markcoroutinefunction",
Expand Down Expand Up @@ -186,6 +187,12 @@ def ismethod(object):
"""Return true if the object is an instance method."""
return isinstance(object, types.MethodType)

def ispackage(object):
"""Return true if the object is a package."""
if ismodule(object) and object.__spec__ is not None:
return object.__spec__.parent == object.__spec__.name
return False
Xiaokang2022 marked this conversation as resolved.
Show resolved Hide resolved

def ismethoddescriptor(object):
"""Return true if the object is a method descriptor.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adding function `ispackage` to determine whether an object is a package or not.
Xiaokang2022 marked this conversation as resolved.
Show resolved Hide resolved
Loading