Skip to content

fix: flag overload signatures not immediately followed by implementation - #5310

Closed
Aayushyaash wants to merge 2 commits into
pyrefly-org:mainfrom
Aayushyaash:fix-overload-contiguous-check
Closed

Aayushyaash wants to merge 2 commits into
pyrefly-org:mainfrom
Aayushyaash:fix-overload-contiguous-check

Conversation

@Aayushyaash

Copy link
Copy Markdown
Contributor

Summary

PEP 484 and the Python Typing Specification require @overload signatures to appear consecutively, immediately followed by their implementation. Intervening statements, such as other function definitions, variable assignments, or expressions, break this sequence and should be reported.

Previously, Pyrefly linked overload predecessors by lexical identifier within the scope (flow.get_value(name)). Because intervening statements updated only their own entries in Flow, the previous overload entry remained available, allowing Pyrefly to incorrectly link overloads across those statements without reporting an error.

This PR fixes the issue by:

  1. Tracking last_func_def: Option<(Name, bool)> in Flow and resetting it when processing non-function statements in stmt_impl.
  2. Validating sequential contiguity in create_function_index when linking an overload to its predecessor, recording the result in BindingDecoratedFunction.is_contiguous.
  3. Emitting an InvalidOverload diagnostic in solve_function_binding when function_binding.is_contiguous is false.

Fixes #5212

  • I am a human, and I fully understand the code I am submitting.

Test Plan

Added unit tests in pyrefly/lib/test/overload.rs covering:

  • An intervening function definition before the implementation.
  • An intervening statement between @overload signatures.
  • An intervening field attribute definition between method overloads in a class.
  • Valid contiguous overloads and regular function redefinitions remaining unaffected.

Validation

cargo test --lib -p pyrefly -- test_overload_intervening
cargo test --lib -p pyrefly -- test::overload::
cargo clippy -p pyrefly
cargo fmt -- --check
python3 test.py --no-test --no-tensor-shapes --no-conformance --no-jsonschema --no-extension

@meta-codesync

meta-codesync Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D126658648. (Because this pull request was imported automatically, there will not be any future comments.)

@github-actions

This comment has been minimized.

@github-actions github-actions Bot added size/l and removed size/l labels Oct 22, 2026
@github-actions

This comment has been minimized.

@stroxler

stroxler commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Hmm. This had a merge conflict which I tried to resolve (it was just tests). But now test are failing, I suspect it just needs a rebase(?)

Thanks for working on this!

@Aayushyaash
Aayushyaash force-pushed the fix-overload-contiguous-check branch from eaa1ddd to 121b771 Compare October 22, 2026 20:03
@github-actions github-actions Bot added size/l and removed size/l labels Oct 22, 2026
@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@Aayushyaash

Aayushyaash commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Rebased cleanly onto the latest main branch. All CI checks are green now.

@stroxler stroxler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

@meta-codesync meta-codesync Bot closed this in 6d51669 Oct 23, 2026
@meta-codesync meta-codesync Bot added the Merged label Oct 23, 2026
@meta-codesync

meta-codesync Bot commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in 6d51669.

@Aayushyaash
Aayushyaash deleted the fix-overload-contiguous-check branch October 23, 2026 04:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

new rule - overload signatures not immediately followed by the implementation

2 participants