Skip to content

fix: set __wrapped__ on instrumented functions to preserve signature#556

Closed
gaoflow wants to merge 1 commit into
agronholm:masterfrom
gaoflow:fix-553-preserve-signature
Closed

fix: set __wrapped__ on instrumented functions to preserve signature#556
gaoflow wants to merge 1 commit into
agronholm:masterfrom
gaoflow:fix-553-preserve-signature

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 18, 2026

Copy link
Copy Markdown

The @typechecked decorator replaces the original function with an
AST-instrumented copy but did not set __wrapped__ on the new
function object. This prevented static analysis tools (e.g. PyCharm,
mypy) from inspecting the original function signature, causing false
"unexpected argument" errors.

Fix: use functools.update_wrapper(new_function, f, updated=()) in
instrument() to set __wrapped__ and copy __dict__ without
overwriting the already-manually-copied attributes.

Fixes: #553

The @TypeChecked decorator replaces the original function with an
AST-instrumented copy but did not set __wrapped__ on the new function
object. This prevented static analysis tools (e.g. PyCharm) from
inspecting the original function signature, causing false 'unexpected
argument' errors.

Use functools.update_wrapper with updated=() to set __wrapped__ (and
other wrapper bookkeeping like __dict__) without overwriting the
manually-copied attributes.

Fixes: agronholm#553
Signed-off-by: Vincent Gao <gaobing1230@gmail.com>
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 94.85% (+0.003%) from 94.847% — gaoflow:fix-553-preserve-signature into agronholm:master

@agronholm

Copy link
Copy Markdown
Owner

More AI slop. Banned.

@agronholm agronholm closed this Jun 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Title: @typechecked decorator does not preserve function signature, causing false "unexpected argument" errors in PyCharm

3 participants