Skip to content

gh-61448: Fix inspect.formatargvalues for deleted arguments - #144854

Closed
RoryGlenn wants to merge 1 commit into
python:mainfrom
RoryGlenn:gh-61448-inspect-getargvalues
Closed

RoryGlenn wants to merge 1 commit into
python:mainfrom
RoryGlenn:gh-61448-inspect-getargvalues

Conversation

@RoryGlenn

Copy link
Copy Markdown
Contributor

Fix inspect.formatargvalues() to handle arguments that are no longer bound in the frame locals (e.g. after del) by showing them as <deleted> instead of raising KeyError.

Changes

  • Added a _DELETED sentinel object with repr of <deleted> in Lib/inspect.py
  • Changed formatargvalues() to use locals.get(name, _DELETED) instead of locals[name] for regular arguments, *args, and **kwargs
  • Added two tests: one for deleted regular arguments, one for deleted *args/**kwargs

Reproducer (before fix)

import inspect

def fun(x):
    del x
    return inspect.currentframe()

inspect.formatargvalues(*inspect.getargvalues(fun(10)))
# KeyError: 'x'

After fix

inspect.formatargvalues(*inspect.getargvalues(fun(10)))
# '(x=<deleted>)'

Note: This supersedes the stale PR #101318 which has been awaiting changes since August 2023 with merge conflicts.

Fix inspect.formatargvalues() to handle arguments that are no longer
bound in the frame locals (e.g. after 'del') by showing them as
<deleted> instead of raising KeyError. This applies to regular
arguments, *args, and **kwargs.
@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 6, 2026
@RoryGlenn RoryGlenn closed this by deleting the head repository Aug 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant