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
20 changes: 20 additions & 0 deletions crates/ruff_db/src/diagnostic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl Diagnostic {
id,
severity,
message: message.into_diagnostic_message(),
custom_concise_message: None,
annotations: vec![],
subs: vec![],
fix: None,
Expand Down Expand Up @@ -213,6 +214,10 @@ impl Diagnostic {
/// cases, just converting it to a string (or printing it) will do what
/// you want.
pub fn concise_message(&self) -> ConciseMessage<'_> {
if let Some(custom_message) = &self.inner.custom_concise_message {
return ConciseMessage::Custom(custom_message.as_str());
}

let main = self.inner.message.as_str();
let annotation = self
.primary_annotation()
Expand All @@ -226,6 +231,15 @@ impl Diagnostic {
}
}

/// Set a custom message for the concise formatting of this diagnostic.
///
/// This overrides the default behavior of generating a concise message
/// from the main diagnostic message and the primary annotation.
pub fn set_concise_message(&mut self, message: impl IntoDiagnosticMessage) {
Arc::make_mut(&mut self.inner).custom_concise_message =
Some(message.into_diagnostic_message());
}

/// Returns the severity of this diagnostic.
///
/// Note that this may be different than the severity of sub-diagnostics.
Expand Down Expand Up @@ -532,6 +546,7 @@ struct DiagnosticInner {
id: DiagnosticId,
severity: Severity,
message: DiagnosticMessage,
custom_concise_message: Option<DiagnosticMessage>,
annotations: Vec<Annotation>,
subs: Vec<SubDiagnostic>,
fix: Option<Fix>,
Expand Down Expand Up @@ -1520,6 +1535,8 @@ pub enum ConciseMessage<'a> {
/// This indicates that the diagnostic is probably using the old
/// model.
Empty,
/// A custom concise message has been provided.
Custom(&'a str),
}

impl std::fmt::Display for ConciseMessage<'_> {
Expand All @@ -1535,6 +1552,9 @@ impl std::fmt::Display for ConciseMessage<'_> {
write!(f, "{main}: {annotation}")
}
ConciseMessage::Empty => Ok(()),
ConciseMessage::Custom(message) => {
write!(f, "{message}")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,12 @@ b: TD | None = f([{"x": 0}, {"x": 1}])
reveal_type(b) # revealed: TD

# error: [missing-typed-dict-key] "Missing required key 'x' in TypedDict `TD` constructor"
# error: [invalid-key] "Invalid key for TypedDict `TD`: Unknown key "y""
# error: [invalid-key] "Unknown key "y" for TypedDict `TD`"
# error: [invalid-assignment] "Object of type `Unknown | dict[Unknown | str, Unknown | int]` is not assignable to `TD`"
c: TD = f([{"y": 0}, {"x": 1}])

# error: [missing-typed-dict-key] "Missing required key 'x' in TypedDict `TD` constructor"
# error: [invalid-key] "Invalid key for TypedDict `TD`: Unknown key "y""
# error: [invalid-key] "Unknown key "y" for TypedDict `TD`"
# error: [invalid-assignment] "Object of type `Unknown | dict[Unknown | str, Unknown | int]` is not assignable to `TD | None`"
c: TD | None = f([{"y": 0}, {"x": 1}])
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def if_else_isinstance_error(obj: A | B):
elif isinstance(obj, C):
pass
else:
# error: [type-assertion-failure] "Argument does not have asserted type `Never`"
# error: [type-assertion-failure] "Type `B & ~A & ~C` is not equivalent to `Never`"
assert_never(obj)

def if_else_singletons_success(obj: Literal[1, "a"] | None):
Expand All @@ -101,7 +101,7 @@ def if_else_singletons_error(obj: Literal[1, "a"] | None):
elif obj is None:
pass
else:
# error: [type-assertion-failure] "Argument does not have asserted type `Never`"
# error: [type-assertion-failure] "Type `Literal["a"]` is not equivalent to `Never`"
assert_never(obj)

def match_singletons_success(obj: Literal[1, "a"] | None):
Expand All @@ -125,7 +125,9 @@ def match_singletons_error(obj: Literal[1, "a"] | None):
pass
case _ as obj:
# TODO: We should emit an error here, but the message should
# show the type `Literal["a"]` instead of `@Todo(…)`.
# error: [type-assertion-failure] "Argument does not have asserted type `Never`"
# show the type `Literal["a"]` instead of `@Todo(…)`. We only
# assert on the first part of the message because the `@Todo`
# message is not available in release mode builds.
# error: [type-assertion-failure] "Type `@Todo"
assert_never(obj)
```
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ from typing_extensions import assert_type

# Subtype does not count
def _(x: bool):
assert_type(x, int) # error: [type-assertion-failure]
assert_type(x, int) # error: [type-assertion-failure] "Type `int` does not match asserted type `bool`"

def _(a: type[int], b: type[Any]):
assert_type(a, type[Any]) # error: [type-assertion-failure]
assert_type(b, type[int]) # error: [type-assertion-failure]
assert_type(a, type[Any]) # error: [type-assertion-failure] "Type `type[Any]` does not match asserted type `type[int]`"
assert_type(b, type[int]) # error: [type-assertion-failure] "Type `type[int]` does not match asserted type `type[Any]`"

# The expression constructing the type is not taken into account
def _(a: type[int]):
Expand Down
4 changes: 2 additions & 2 deletions crates/ty_python_semantic/resources/mdtest/enums.md
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ def color_name_misses_one_variant(color: Color) -> str:
elif color is Color.GREEN:
return "Green"
else:
assert_never(color) # error: [type-assertion-failure] "Argument does not have asserted type `Never`"
assert_never(color) # error: [type-assertion-failure] "Type `Literal[Color.BLUE]` is not equivalent to `Never`"

class Singleton(Enum):
VALUE = 1
Expand Down Expand Up @@ -956,7 +956,7 @@ def color_name_misses_one_variant(color: Color) -> str:
case Color.GREEN:
return "Green"
case _:
assert_never(color) # error: [type-assertion-failure] "Argument does not have asserted type `Never`"
assert_never(color) # error: [type-assertion-failure] "Type `Literal[Color.BLUE]` is not equivalent to `Never`"

class Singleton(Enum):
VALUE = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/subscript/assignment_dia
# Diagnostics

```
error[invalid-key]: Invalid key for TypedDict `Config`
error[invalid-key]: Unknown key "Retries" for TypedDict `Config`
--> src/mdtest_snippet.py:7:5
|
6 | def _(config: Config) -> None:
7 | config["Retries"] = 30.0 # error: [invalid-key]
| ------ ^^^^^^^^^ Unknown key "Retries" - did you mean "retries"?
| ------ ^^^^^^^^^ Did you mean "retries"?
| |
| TypedDict `Config`
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/subscript/assignment_dia
# Diagnostics

```
error[invalid-key]: Invalid key for TypedDict `Person`
error[invalid-key]: Unknown key "surname" for TypedDict `Person`
--> src/mdtest_snippet.py:13:5
|
11 | # error: [invalid-key]
12 | # error: [invalid-key]
13 | being["surname"] = "unknown"
| ----- ^^^^^^^^^ Unknown key "surname" - did you mean "name"?
| ----- ^^^^^^^^^ Did you mean "name"?
| |
| TypedDict `Person` in union type `Person | Animal`
|
Expand All @@ -45,13 +45,13 @@ info: rule `invalid-key` is enabled by default
```

```
error[invalid-key]: Invalid key for TypedDict `Animal`
error[invalid-key]: Unknown key "surname" for TypedDict `Animal`
--> src/mdtest_snippet.py:13:5
|
11 | # error: [invalid-key]
12 | # error: [invalid-key]
13 | being["surname"] = "unknown"
| ----- ^^^^^^^^^ Unknown key "surname" - did you mean "name"?
| ----- ^^^^^^^^^ Did you mean "name"?
| |
| TypedDict `Animal` in union type `Person | Animal`
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/subscript/assignment_dia
# Diagnostics

```
error[invalid-key]: Invalid key for TypedDict `Person`
error[invalid-key]: Unknown key "legs" for TypedDict `Person`
--> src/mdtest_snippet.py:11:5
|
10 | def _(being: Person | Animal) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/typed_dict.md
# Diagnostics

```
error[invalid-key]: Invalid key for TypedDict `Person`
error[invalid-key]: Unknown key "naem" for TypedDict `Person`
--> src/mdtest_snippet.py:8:5
|
7 | def access_invalid_literal_string_key(person: Person):
8 | person["naem"] # error: [invalid-key]
| ------ ^^^^^^ Unknown key "naem" - did you mean "name"?
| ------ ^^^^^^ Did you mean "name"?
| |
| TypedDict `Person`
9 |
Expand All @@ -73,7 +73,7 @@ info: rule `invalid-key` is enabled by default
```

```
error[invalid-key]: Invalid key for TypedDict `Person`
error[invalid-key]: Unknown key "naem" for TypedDict `Person`
--> src/mdtest_snippet.py:13:5
|
12 | def access_invalid_key(person: Person):
Expand Down Expand Up @@ -130,12 +130,12 @@ info: rule `invalid-assignment` is enabled by default
```

```
error[invalid-key]: Invalid key for TypedDict `Person`
error[invalid-key]: Unknown key "naem" for TypedDict `Person`
--> src/mdtest_snippet.py:22:5
|
21 | def write_to_non_existing_key(person: Person):
22 | person["naem"] = "Alice" # error: [invalid-key]
| ------ ^^^^^^ Unknown key "naem" - did you mean "name"?
| ------ ^^^^^^ Did you mean "name"?
| |
| TypedDict `Person`
23 |
Expand All @@ -160,7 +160,7 @@ info: rule `invalid-key` is enabled by default
```

```
error[invalid-key]: Invalid key for TypedDict `Person`
error[invalid-key]: Unknown key "unknown" for TypedDict `Person`
--> src/mdtest_snippet.py:29:21
|
27 | def create_with_invalid_string_key():
Expand All @@ -178,7 +178,7 @@ info: rule `invalid-key` is enabled by default
```

```
error[invalid-key]: Invalid key for TypedDict `Person`
error[invalid-key]: Unknown key "unknown" for TypedDict `Person`
--> src/mdtest_snippet.py:32:11
|
31 | # error: [invalid-key]
Expand Down
Loading