Skip to content

Fix inline method renaming keyword argument names (#757) - #984

Open
mcepl wants to merge 2 commits into
masterfrom
757-inline-keyword-rename
Open

mcepl wants to merge 2 commits into
masterfrom
757-inline-keyword-rename

Conversation

@mcepl

@mcepl mcepl commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Description

When the inlined function's body uses a name that also exists in the call site's scope (e.g. self when the caller is itself a method), rope prefixes every name in the body with a unique prefix (__0__, …) before inlining the parameters. The occurrence finder used for that rename also matched keyword argument names in calls, so

def translate(self, from_lang='auto', to='en'):
    return self.translate_to('string', from_lang=from_lang, to_lang=to)

was inlined as translate_to('string', __0__from_lang='auto', to_lang='es').

Keyword names in a call always refer to the callee's parameters, never to variables of the inlined body, so the rename now uses create_finder(..., keywords=False).

Fixes #757

Checklist (delete if not relevant):

  • I have added tests that prove my fix is effective or that my feature works
  • I have updated CHANGELOG.md
  • I have made corresponding changes to user documentation for new features (n/a)
  • I have made corresponding changes to library documentation for API changes (n/a)

@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.50%. Comparing base (18e7d47) to head (a0fe1e6).

Additional details and impacted files
@@                     Coverage Diff                      @@
##           changelog-news-fragments     #984      +/-   ##
============================================================
+ Coverage                     95.46%   95.50%   +0.04%     
============================================================
  Files                           134      134              
  Lines                         26946    26955       +9     
============================================================
+ Hits                          25723    25744      +21     
+ Misses                         1223     1211      -12     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mcepl
mcepl force-pushed the 757-inline-keyword-rename branch 2 times, most recently from 81a8994 to 8ed3a06 Compare November 30, 2026 08:29
@mcepl
mcepl changed the base branch from master to changelog-news-fragments November 30, 2026 08:30
@mcepl
mcepl force-pushed the 757-inline-keyword-rename branch from 8ed3a06 to 529505a Compare November 30, 2026 08:45
Nearly every pull request conflicted on the top of CHANGELOG.md,
because all of them appended to the same "Upcoming release"
section. Following a much simplified version of CPython's
blurb/NEWS.d workflow, each PR now adds a small Markdown file
changelog.d/<issue>.md with its entry, so parallel PRs never
touch the same file.

The new bin/news script (plain POSIX sh, so it works on Linux,
the BSDs and macOS) creates a fragment in $VISUAL/$EDITOR from a
template encouraging longer, explanatory entries (`bin/news 123`),
previews the pending release section (`bin/news preview`), and
folds the fragments, sorted by number, into a new "# Release
<version>" section of CHANGELOG.md (`bin/news collect`). A new
portable top-level Makefile wraps it as `make news N=123`,
`make changelog-preview`, `make changelog` and `make release`,
the last of which also builds the documentation via
docs/Makefile; that gains an explicit `html` rule, as its
Sphinx-generated catch-all pattern rule is GNU make only. The
news target passes N and TEXT to bin/news through the
environment ("$$N", "$$TEXT"), so Markdown backticks and quotes
in an entry reach the script literally instead of being
re-parsed by the shell.

The pending entry for the #223 regression tests, previously in
the "Upcoming release" section, has been moved to
changelog.d/223.md, and the PR template, contributing and
release-process docs updated.
@mcepl
mcepl force-pushed the changelog-news-fragments branch from d88420d to 18e7d47 Compare November 30, 2026 08:49
@mcepl
mcepl force-pushed the 757-inline-keyword-rename branch 2 times, most recently from 32dfc4f to 48c77dd Compare November 30, 2026 08:55
@mcepl
mcepl changed the base branch from changelog-news-fragments to mypy-typing-coverage November 30, 2026 08:55
@mcepl
mcepl added this pull request to stack #986 November 30, 2026 08:55
@mcepl
mcepl removed this pull request from stack #986 November 30, 2026 08:58
When the body of an inlined function or method uses a name that
is also defined in the scope at the call site (typically `self`
when the caller is itself a method), `_calculate_definition()`
prefixes all names in the body with a unique prefix such as
`__0__`. The occurrence finder used for this renaming also
matched keyword argument names in calls, so a body like
`self.translate_to('string', from_lang=from_lang)` was inlined as
`translate_to('string', __0__from_lang='auto')`, producing an
invalid keyword argument.

Keyword names in a call refer to parameters of the callee, never
to variables of the inlined body, so pass `keywords=False` to
`create_finder()` to leave them untouched. Regression tests cover
both the method scenario from the issue and a plain function.

Fixes: #757
@mcepl
mcepl force-pushed the 757-inline-keyword-rename branch from 48c77dd to a0fe1e6 Compare November 30, 2026 08:58
@mcepl
mcepl changed the base branch from mypy-typing-coverage to changelog-news-fragments November 30, 2026 08:58
lieryan
lieryan previously approved these changes Dec 1, 2026

@lieryan lieryan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Base automatically changed from changelog-news-fragments to master December 1, 2026 22:17
@mcepl
mcepl dismissed lieryan’s stale review December 1, 2026 22:17

The base branch was changed.

This branch has not been deployed

No deployments
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.

Inline method refactoring inserts an unexpected argument

2 participants