Skip to content

Optional[FrameType] has no attribute "f_code" #1217

Answered by Gobot1234
dineshbvadhia asked this question in Q&A
Discussion options

You must be logged in to vote

inspect.currentframe() can depending on the python implementation return None as stated in the documentation:

CPython implementation detail: This function relies on Python stack frame support in the interpreter, which isn’t guaranteed to exist in all implementations of Python. If running in an implementation without Python stack frame support this function returns None.

mypy is just letting you know that's the case. To handle this you can just check if the return of currentframe() is None

from types import FrameType
frame = inspect.currentframe()
if frame is None:
    raise RuntimeError("unsupported python implementation")  # or give a default name
fn_name: str = frame.f_code.co_name

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Akuli
Comment options

Answer selected by dineshbvadhia
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants