You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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 Nones might be returned should be documented.
getdoc
,getmodule
,getsourcefile
ininspect
returnsNone
#30575Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: