`inspect.getfile` doesn't properly handle PEP 420
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 36k
- PR merge metrics
- PR metrics pending
Description
Bug report
inspect.getfile handles all __file__ is None modules as built-in. Cf. https://github.com/python/cpython/blob/f298ba1f2712ad10530a30bb225548a6889820b5/Lib/inspect.py#L898-L901
This leads to PEP 420 packages being treated as built-in, as implicit namespaces have file set to None, but __path__ set to the actual path.
Creating PEP 420 directory:
~ ❯❯❯ mkdir python_test
~ ❯❯❯ vim python_test/hello.py
Python interpreter
Python 3.9.7 (default, Oct 10 2021, 14:08:54)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import python_test
>>> import inspect
>>> inspect.getfile(python_test)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/python-3.9.7/lib/python3.9/inspect.py", line 660, in getfile
raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module 'python_test' (namespace)> is a built-in module
>>> print(python_test.__file__)
None
>>> print(python_test.__path__)
_NamespacePath(['/home/thetechrobo/python_test'])
Your environment
- CPython versions tested on: 3.9.7
- Operating system and architecture: Elive Linux 3.8.27 64-bit (Debian Bullseye); custom Python installation
Apologies if this is a duplicate. I've searched for any related issues, but I may have missed one.
Linked PRs
- gh-92635
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in Lib/inspect.py around the getfile logic referenced in the report, then reproduce the behavior with an implicit namespace package and inspect its path. Check linked PR gh-92635 for existing work; done means the namespace package is no longer reported as a built-in module and the regression is covered by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100