From 074904f90d2cc8bd24bdac0c63d777002c0c7937 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sun, 14 Dec 2025 16:32:48 +0000 Subject: [PATCH] [ty] Fix panic on invalid syntax where we would incorrectly consider overloads in another file as belonging to a function in the file being checked --- .../resources/mdtest/overloads.md | 27 +++++++++++++++++++ .../ty_python_semantic/src/types/function.rs | 9 +++++++ 2 files changed, 36 insertions(+) diff --git a/crates/ty_python_semantic/resources/mdtest/overloads.md b/crates/ty_python_semantic/resources/mdtest/overloads.md index f74cafb80b..1173af6ae5 100644 --- a/crates/ty_python_semantic/resources/mdtest/overloads.md +++ b/crates/ty_python_semantic/resources/mdtest/overloads.md @@ -747,3 +747,30 @@ class Sub2(Base): # error: [invalid-overload] def method(self, x: str) -> str: ... ``` + +### Invalid syntax regression case + +We used to panic on this snippet (see ), because +"iterating over the overloads" for `g` would incorrectly list the overloads for `f`: + + + +`module.pyi`: + +```pyi +from typing import overload + +@overload +def f() -> int: ... # error: [invalid-overload] +``` + +`main.py`: + +```py +import module +g = module.f +@staticmethod +def g # error: [invalid-syntax] +``` + + diff --git a/crates/ty_python_semantic/src/types/function.rs b/crates/ty_python_semantic/src/types/function.rs index e727e6663b..d409cdf6fb 100644 --- a/crates/ty_python_semantic/src/types/function.rs +++ b/crates/ty_python_semantic/src/types/function.rs @@ -386,6 +386,15 @@ impl<'db> OverloadLiteral<'db> { return None; } + // These can both happen in edge cases involving not-yet-complete function definitions + // (invalid syntax). + if previous_overload.name(db) != self.name(db) { + return None; + } + if previous_overload.body_scope(db).file(db) != self.file(db) { + return None; + } + Some(previous_literal) }