Skip to content

gh-149010: Improve reliability of inspect CLI#149357

Merged
ncoghlan merged 4 commits intopython:mainfrom
ncoghlan:gh-149010-improve-inspect-cli-non-source-handling
May 4, 2026
Merged

gh-149010: Improve reliability of inspect CLI#149357
ncoghlan merged 4 commits intopython:mainfrom
ncoghlan:gh-149010-improve-inspect-cli-non-source-handling

Conversation

@ncoghlan
Copy link
Copy Markdown
Contributor

@ncoghlan ncoghlan commented May 4, 2026

  • Handle non-source modules more gracefully (and consistently)
  • Improve handling of frozen modules (which may or may not have source)
  • Avoid reporting misleading info when looking up objects via aliases
  • Refactor CLI implementation to improve testability
  • Add several more test cases

Closes #149010

* Handle non-source modules more gracefully (and consistently)
* Improve handling of frozen modules (which may or may not have source)
* Avoid reporting misleading info when looking up objects via aliases
* Refactor CLI implementation to improve testability
* Add several more test cases

Closes python#149010
@ncoghlan
Copy link
Copy Markdown
Contributor Author

ncoghlan commented May 4, 2026

Some before-and-after examples.

Before:

acoghlan@TerminalMist:~/devel/cpython$ python3 -m inspect sys --details
Can't get info for builtin modules.

After:

$ ./python -m inspect sys --details
Target: sys
Origin: built-in
Source: None
Cached: None
Loader: <class '_frozen_importlib.BuiltinImporter'>

Before:

$ python3 -m inspect _pickle --details
Target: _pickle
Origin: None
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/usr/lib64/python3.13/inspect.py", line 3460, in <module>
    _main()
    ~~~~~^^
  File "/usr/lib64/python3.13/inspect.py", line 3441, in _main
    print('Cached: {}'.format(module.__cached__))
                              ^^^^^^^^^^^^^^^^^
AttributeError: module '_pickle' has no attribute '__cached__'

After:

$ ./python -m inspect _pickle --details
Target: _pickle
Origin: /home/acoghlan/devel/cpython/build/lib.linux-x86_64-3.15/_pickle.cpython-315-x86_64-linux-gnu.so
Source: None
Cached: None
Loader: <_frozen_importlib_external.ExtensionFileLoader object at 0x7e9d38f2a2c0>

Before:

$ python3 -m inspect os:path.join --details
Target: os:path.join
Origin: /usr/lib64/python3.13/os.py
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/usr/lib64/python3.13/inspect.py", line 3460, in <module>
    _main()
    ~~~~~^^
  File "/usr/lib64/python3.13/inspect.py", line 3441, in _main
    print('Cached: {}'.format(module.__cached__))
                              ^^^^^^^^^^^^^^^^^
AttributeError: module 'os' has no attribute '__cached__'

After:

$ ./python -m inspect os:path.join --details
Target: posixpath:join (looked up as "os:path.join")
Origin: frozen
Source: /home/acoghlan/devel/cpython/Lib/posixpath.py
Cached: None
Line: 72

@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented May 4, 2026

Documentation build overview

📚 cpython-previews | 🛠️ Build #32526303 | 📁 Comparing d94403b against main (1504bd6)

  🔍 Preview build  

2 files changed
± library/inspect.html
± whatsnew/changelog.html

Comment thread Lib/test/test_inspect/test_inspect.py Outdated

def _clean_object_ids(text):
# Helper to handle "<obj at 0x...>" details in CLI output checks
import re
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's okay to put this at the top for tests. Can even lazy import re if you want.

There's also one in test_re_module_has_signatures we could move.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given how long the CI takes to run, I'm going to put that on the "try to remember to clean that up next time I touch this module" list :)

@ncoghlan ncoghlan merged commit 5235467 into python:main May 4, 2026
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve inspect module CLI handling of non-source modules

2 participants