Skip to content

Commit 60f104a

Browse files
authored
[ty] Honor @OverRide on overload implementations in stubs (#29470)
In stub files, ty required `@override` on the first overload even when an implementation was present, while overload validation required the decorator on the implementation. Honor `@override` on the implementation whenever one exists, matching Pyright and Pyrefly. Continue using the first overload when there is no implementation. The implementation also becomes the location for `invalid-explicit-override` diagnostics in these stubs. Fixes astral-sh/ty#4742. ## Test plan - Checked the reported `.pyi` example, an ellipsis-only minimized example, and a stub without an implementation using Python 3.13 with all rules enabled; all passed without diagnostics. - Compared decorator placement with mypy 2.4.0, Pyright 1.1.414, basedpyright 1.40.2, and Pyrefly 1.4.0. Pyright, basedpyright, and Pyrefly honor the implementation's decorator in stubs; mypy rejects stub overload implementations. All four accept the decorator on the first overload when no implementation exists. ## Ecosystem report For head `5690fe75333fb6755662fda0cec8d6a192905d2b`, the ecosystem run removes 27 false `missing-override-decorator` errors from pyochain stubs whose overload implementations already have `@override`. All changes were reproduced and classified as expected; CI passed and no ecosystem concern blocks review. [Full ecosystem report](#29470 (comment)).
1 parent 207ec3a commit 60f104a

2 files changed

Lines changed: 37 additions & 13 deletions

File tree

‎crates/ty_python_semantic/resources/mdtest/override.md‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,30 @@ class OverrideOnFirstOverload(StubParent):
10281028
@overload
10291029
def method(self, value: str) -> str: ...
10301030

1031+
# With an overload implementation, `@override` belongs on the implementation, including in stubs.
1032+
class OverrideOnStubImplementation(StubParent):
1033+
@overload
1034+
def method(self, value: int) -> int: ...
1035+
@overload
1036+
def method(self, value: str) -> str: ...
1037+
@override
1038+
def method(self, value: int | str) -> int | str: ... # no diagnostic
1039+
1040+
class MissingOverrideOnStubImplementation(StubParent):
1041+
@overload
1042+
def method(self, value: int) -> int: ...
1043+
@overload
1044+
def method(self, value: str) -> str: ...
1045+
def method(self, value: int | str) -> int | str: ... # error: [missing-override-decorator]
1046+
1047+
class OverrideOnStubOverloadWithImplementation(StubParent):
1048+
@overload
1049+
@override
1050+
def method(self, value: int) -> int: ... # error: [invalid-overload]
1051+
@overload
1052+
def method(self, value: str) -> str: ...
1053+
def method(self, value: int | str) -> int | str: ... # error: [missing-override-decorator]
1054+
10311055
class StubProtocolInterface(Protocol):
10321056
def method(self) -> int: ...
10331057

@@ -1399,8 +1423,9 @@ class Spam:
13991423
return x
14001424
```
14011425

1402-
In a stub file, `@override` should always be applied to the first overload. Even if it isn't, we
1403-
always emit `invalid-explicit-override` diagnostics on the first overload.
1426+
Without an implementation, `@override` should be applied to the first overload. Even if it isn't, we
1427+
emit `invalid-explicit-override` diagnostics on the first overload. When an implementation is
1428+
present, the diagnostic points to it, including in stub files.
14041429

14051430
`module.pyi`:
14061431

@@ -1426,6 +1451,12 @@ class Spam:
14261451
def baz(self, x: str) -> str: ... # error: [invalid-explicit-override]
14271452
@overload
14281453
def baz(self, x: int) -> int: ...
1454+
@overload
1455+
def quux(self, x: str) -> str: ...
1456+
@overload
1457+
def quux(self, x: int) -> int: ...
1458+
@override
1459+
def quux(self, x: str | int) -> str | int: ... # error: [invalid-explicit-override]
14291460
```
14301461

14311462
## Overloads in statically-known branches in stub files

‎crates/ty_python_semantic/src/types/overrides.rs‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,7 +1892,7 @@ struct LocalOverrideDefinition {
18921892
/// Range to use as the primary diagnostic location.
18931893
///
18941894
/// This is usually the function name. For an overloaded function, it points to the
1895-
/// implementation in a source file, or the first overload in a stub.
1895+
/// implementation if present, or the first overload otherwise.
18961896
focus_range: FileRange,
18971897
/// Whether any overload or implementation in this local function has `@override`.
18981898
///
@@ -1917,10 +1917,9 @@ impl LocalOverrideDefinition {
19171917
fn from_function<'db>(
19181918
db: &'db dyn Db,
19191919
function: FunctionType<'db>,
1920-
in_stub: bool,
19211920
module: &ParsedModuleRef,
19221921
) -> Self {
1923-
let focus_definition = overriding_definition(db, function, in_stub);
1922+
let focus_definition = overriding_definition(db, function);
19241923

19251924
Self {
19261925
focus_range: focus_definition.focus_range(db, module),
@@ -2026,23 +2025,17 @@ fn extract_local_override_definitions<'db>(
20262025
.local_functions(context.db(), subclass_scope)
20272026
.into_iter()
20282027
.map(|function| {
2029-
LocalOverrideDefinition::from_function(
2030-
context.db(),
2031-
function,
2032-
context.in_stub(),
2033-
context.module(),
2034-
)
2028+
LocalOverrideDefinition::from_function(context.db(), function, context.module())
20352029
})
20362030
.collect()
20372031
}
20382032

20392033
fn overriding_definition<'db>(
20402034
db: &'db dyn Db,
20412035
function: FunctionType<'db>,
2042-
in_stub: bool,
20432036
) -> OverloadLiteral<'db> {
20442037
let (_, implementation) = function.overloads_and_implementation(db);
2045-
if !in_stub && let Some(implementation) = implementation {
2038+
if let Some(implementation) = implementation {
20462039
implementation
20472040
} else {
20482041
function.first_overload_or_implementation(db)

0 commit comments

Comments
 (0)