Title: inspect.getsourcefile documentation doesn't mention it can return None
Type: behavior Stage: patch review
Components: Documentation Versions:
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-01-13 13:05 by sobolevn.

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.

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.
Date User Action Args
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