This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: inspect.getsourcefile documentation doesn't mention it can return None
Type: behavior Stage: patch review
Components: Documentation Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Jim Fasarakis-Hilliard, docs@python, pekka.klarck, sobolevn
Priority: normal Keywords: patch

Created on 2020-06-01 11:41 by pekka.klarck, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 30575 open sobolevn, 2022-01-13 13:05
Messages (2)
msg370547 - (view) Author: Pekka Klärck (pekka.klarck) Date: 2020-06-01 11:41
The docs of inspect.getsourcefile [1] mention the function can raise TypeError, but there's nothing about the function possibly returning None. This caused a bug in our project [2].

If I understand the code [3] correctly, None is returned if getsourcefile cannot determine the original source file of the file returned by getfile. That's understandable but should definitely be documented. Raising TypeError that getfile itself may raise might be even better, but such a backwards incompatible API change is probably not worth the effort.

While looking at the code, I also noticed there's getabsfile [4] that uses getfile if getsourcefile returns None. That looks handy but since the function isn't included in the inspect module documentation [5] using it feels pretty risky.

[1] https://docs.python.org/3/library/inspect.html#inspect.getsourcefile
[2] https://github.com/robotframework/robotframework/issues/3587
[3] https://github.com/python/cpython/blob/3.8/Lib/inspect.py#L692
[4] https://github.com/python/cpython/blob/3.8/Lib/inspect.py#L714
[5] https://bugs.python.org/issue12317
msg370548 - (view) Author: Jim Fasarakis-Hilliard (Jim Fasarakis-Hilliard) * Date: 2020-06-01 12:47
For a more comprehensive list, we currently have for `get*` functions in `inspect`:

`inspect.getdoc`: Returns `None` if the documentation string isn't present, either directly on the object or through it mro. This *isn't* documented.

`inspect.getfile`: Explicitly seems to handle None cases. After peeking a bit in the `PyCode_*` interface, it doesn't seem to be possible to assign `None` to the `co_filename` so the returning the `object.co_filename` in the function appears to not be able to return `None`.

`inspect.getmodule`: Returns None in a number of cases. This *isn't* documented.

`inspect.getsourcefile`: Returns None if the filename indicates an extension module or when none of the ifs are matched. This *isn't* documented.

Some (getmodulename, getcomments) do document this. Agreed that the rest of the cases where `None`s might be returned should be documented.
History
Date User Action Args
2022-04-11 14:59:31adminsetgithub: 85015
2022-01-13 13:05:58sobolevnsetkeywords: + patch
nosy: + sobolevn

pull_requests: + pull_request28775
stage: patch review
2020-09-19 15:36:24iritkatrielsetassignee: docs@python

type: behavior
components: + Documentation
nosy: + docs@python
2020-06-01 12:47:06Jim Fasarakis-Hilliardsetnosy: + Jim Fasarakis-Hilliard
messages: + msg370548
2020-06-01 11:41:44pekka.klarckcreate