Fix kwarg detection for chained receivers (issue #327)#344
Open
apoorvdarshan wants to merge 1 commit into
Open
Fix kwarg detection for chained receivers (issue #327)#344apoorvdarshan wants to merge 1 commit into
apoorvdarshan wants to merge 1 commit into
Conversation
A chained receiver such as `Path.cwd().exists(follow_symlinks=True)` was detected under the name `pathlib.Path.cwd.exists`, because the intermediate `cwd()` call segment is included in the resolved function name. That name does not match the kwarg rule key `pathlib.Path.exists`, so `follow_symlinks` was silently dropped and the code was reported as requiring 3.4 instead of 3.12. When the full resolved name does not match a known kwarg rule, retry with the intermediate call segment(s) collapsed away, keeping the receiver plus the final method. Every receiver-length prefix is tried joined with the final method, and a collapsed candidate is only accepted when it corresponds to an actual kwarg rule, so unrelated chains cannot produce false matches. Also fixes the same issue for the other pathlib methods that gained a `follow_symlinks` kwarg in 3.13 (e.g. `is_file`, `is_dir`).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #327
Root cause
For a chained receiver such as
Path.cwd().exists(follow_symlinks=True), the resolvedfunction name includes the intermediate
cwd()call segment, so the kwarg is looked upunder
('pathlib.Path.cwd.exists', 'follow_symlinks'). That tuple does not match the rulekey
('pathlib.Path.exists', 'follow_symlinks')inKWARGS_REQS, so thefollow_symlinkskwarg is silently dropped and the minimum version is under-reported.
Reproduction
Before (wrong):
After (correct):
The direct form
Path('.').exists(follow_symlinks=True)was already reported correctly as3.12; only the chained-receiver form was affected. The same problem applied to the other
pathlib methods that gained
follow_symlinksin 3.13 (e.g.is_file,is_dir), as notedin the issue thread.
Fix
In
SourceVisitor.visit_keyword, when the fully resolved function name does not match aknown kwarg rule and it contains more than two segments (i.e. a chained call), retry with
the intermediate call segment(s) collapsed away — keeping the receiver plus the final
method (
pathlib.Path.exists). Because an already-resolved receiver can span severalsegments (e.g.
pathlib.Path.cwd.exists), each receiver-length prefix is tried joined withthe final method. A collapsed candidate is only accepted when
(name, kwarg)is an actualentry in
kwargs_reqs_rules, so unrelated chains (e.g.foo.bar().exists(...)) cannotproduce false matches. The change is localized to the existing kwarg-resolution loop and
does not alter any of the primary lookup paths.
Tests
Added focused regression tests in
tests/kwargs.pyfor the chained-receiver form ofPath.exists,Path.is_file, andPath.is_dir. They fail on the unmodified code and passwith the fix. The full existing suite (
python3 runtests.py, 4382 tests across 26 suites)passes, and
flake8is clean on the changed files.Disclosure: prepared with AI assistance; reviewed and verified locally.