Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/ty_python_semantic/resources/mdtest/metaclass.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,18 @@ reveal_type(D) # revealed: <class 'D'>
reveal_type(D.__class__) # revealed: <class 'SignatureMismatch'>
```

## Diagnostic range

<!-- snapshot-diagnostics -->

```py
def _(n: int):
# error: [invalid-metaclass]
class B(metaclass=n):
x = 1
y = 2
```

## Cyclic

Retrieving the metaclass of a cyclically defined class should not cause an infinite loop.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
source: crates/ty_test/src/lib.rs
expression: snapshot
---

---
mdtest name: metaclass.md - Diagnostic range
mdtest path: crates/ty_python_semantic/resources/mdtest/metaclass.md
---

# Python source files

## mdtest_snippet.py

```
1 | def _(n: int):
2 | # error: [invalid-metaclass]
3 | class B(metaclass=n):
4 | x = 1
5 | y = 2
```

# Diagnostics

```
error[invalid-metaclass]: Metaclass type `int` is not callable
--> src/mdtest_snippet.py:3:13
|
1 | def _(n: int):
2 | # error: [invalid-metaclass]
3 | class B(metaclass=n):
| ^^^^^^^^^^^
4 | x = 1
5 | y = 2
|
info: rule `invalid-metaclass` is enabled by default

```
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,12 @@ pub(crate) fn check_static_class_definitions<'db>(

// Check that the class's metaclass can be determined without error.
if let Err(metaclass_error) = class.try_metaclass(db) {
let invalid_metaclass_range = class_node
.arguments
.as_ref()
.and_then(|arguments| arguments.find_keyword("metaclass"))
.map(Ranged::range)
.unwrap_or_else(|| class.header_range(db));
match metaclass_error.reason() {
MetaclassErrorKind::Cycle => {
if let Some(builder) = context.report_lint(&CYCLIC_CLASS_DEFINITION, class_node) {
Expand All @@ -580,20 +586,26 @@ pub(crate) fn check_static_class_definitions<'db>(
}
}
MetaclassErrorKind::GenericMetaclass => {
if let Some(builder) = context.report_lint(&INVALID_METACLASS, class_node) {
if let Some(builder) =
context.report_lint(&INVALID_METACLASS, invalid_metaclass_range)
{
builder.into_diagnostic("Generic metaclasses are not supported");
}
}
MetaclassErrorKind::NotCallable(ty) => {
if let Some(builder) = context.report_lint(&INVALID_METACLASS, class_node) {
if let Some(builder) =
context.report_lint(&INVALID_METACLASS, invalid_metaclass_range)
{
builder.into_diagnostic(format_args!(
"Metaclass type `{}` is not callable",
ty.display(db)
));
}
}
MetaclassErrorKind::PartlyNotCallable(ty) => {
if let Some(builder) = context.report_lint(&INVALID_METACLASS, class_node) {
if let Some(builder) =
context.report_lint(&INVALID_METACLASS, invalid_metaclass_range)
{
builder.into_diagnostic(format_args!(
"Metaclass type `{}` is partly not callable",
ty.display(db)
Expand Down
Loading