[ty] Make the implicit shadowing message on invalid assignment diagnostic info (#22219)

This commit is contained in:
Matthew Mckee
2025-12-29 09:30:48 +00:00
committed by GitHub
parent 8efa14ae1b
commit fde33baaa5
7 changed files with 17 additions and 21 deletions

View File

@@ -1867,7 +1867,7 @@ def external_getattribute(name) -> int:
class ThisFails:
def __init__(self):
# error: [invalid-assignment] "Implicit shadowing of function `__getattribute__`"
# error: [invalid-assignment]
self.__getattribute__ = external_getattribute
# error: [unresolved-attribute]

View File

@@ -205,7 +205,7 @@ class C:
return str(key)
def f(self):
# error: [invalid-assignment] "Implicit shadowing of function `__getitem__`"
# error: [invalid-assignment]
self.__getitem__ = None
# This is still fine, and simply calls the `__getitem__` method on the class

View File

@@ -5,7 +5,7 @@
```py
class C: ...
C = 1 # error: "Implicit shadowing of class `C`"
C = 1 # error: [invalid-assignment]
```
## Explicit

View File

@@ -15,7 +15,7 @@ def f(x: str):
```py
def f(): ...
f = 1 # error: "Implicit shadowing of function `f`"
f = 1 # error: [invalid-assignment]
```
## Explicit shadowing

View File

@@ -20,7 +20,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/shadowing.md
# Diagnostics
```
error[invalid-assignment]: Implicit shadowing of class `C`
error[invalid-assignment]: Object of type `Literal[1]` is not assignable to `<class 'C'>`
--> src/mdtest_snippet.py:3:1
|
1 | class C: ...
@@ -30,7 +30,7 @@ error[invalid-assignment]: Implicit shadowing of class `C`
| |
| Declared type `<class 'C'>`
|
info: Annotate to make it explicit if this is intentional
info: Implicit shadowing of class `C`, add an annotation to make it explicit if this is intentional
info: rule `invalid-assignment` is enabled by default
```

View File

@@ -20,7 +20,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/shadowing.md
# Diagnostics
```
error[invalid-assignment]: Implicit shadowing of function `f`
error[invalid-assignment]: Object of type `Literal[1]` is not assignable to `def f() -> Unknown`
--> src/mdtest_snippet.py:3:1
|
1 | def f(): ...
@@ -30,7 +30,7 @@ error[invalid-assignment]: Implicit shadowing of function `f`
| |
| Declared type `def f() -> Unknown`
|
info: Annotate to make it explicit if this is intentional
info: Implicit shadowing of function `f`, add an annotation to make it explicit if this is intentional
info: rule `invalid-assignment` is enabled by default
```

View File

@@ -2452,29 +2452,25 @@ fn report_invalid_assignment_with_message<'db, 'ctx: 'db, T: Ranged>(
message: std::fmt::Arguments,
) -> Option<LintDiagnosticGuard<'db, 'ctx>> {
let builder = context.report_lint(&INVALID_ASSIGNMENT, node)?;
let mut diag = builder.into_diagnostic(message);
match target_ty {
Type::ClassLiteral(class) => {
let mut diag = builder.into_diagnostic(format_args!(
"Implicit shadowing of class `{}`",
diag.info(format_args!(
"Implicit shadowing of class `{}`, add an annotation to make it explicit if this is intentional",
class.name(context.db()),
));
diag.info("Annotate to make it explicit if this is intentional");
Some(diag)
}
Type::FunctionLiteral(function) => {
let mut diag = builder.into_diagnostic(format_args!(
"Implicit shadowing of function `{}`",
diag.info(format_args!(
"Implicit shadowing of function `{}`, add an annotation to make it explicit if this is intentional",
function.name(context.db()),
));
diag.info("Annotate to make it explicit if this is intentional");
Some(diag)
}
_ => {
let diag = builder.into_diagnostic(message);
Some(diag)
}
_ => {}
}
Some(diag)
}
pub(super) fn report_invalid_assignment<'db>(