Skip to content

Commit

Permalink
[red-knot] dedicated error message for all-union-elements not callable (
Browse files Browse the repository at this point in the history
#13412)

This was mentioned in an earlier review, and seemed easy enough to just
do it. No need to repeat all the types twice when it gives no additional
information.
  • Loading branch information
carljm authored Sep 20, 2024
1 parent 03f3a4e commit 40c65dc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
14 changes: 11 additions & 3 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,18 +817,26 @@ impl<'db> CallOutcome<'db> {
node,
"call-non-callable",
format_args!(
"Union element '{}' of type '{}' is not callable.",
"Object of type '{}' is not callable (due to union element '{}').",
called_ty.display(db),
elem.display(db),
),
),
_ if not_callable.len() == outcomes.len() => builder.add_diagnostic(
node,
"call-non-callable",
format_args!(
"Object of type '{}' is not callable.",
called_ty.display(db)
),
),
_ => builder.add_diagnostic(
node,
"call-non-callable",
format_args!(
"Union elements {} of type '{}' are not callable.",
"Object of type '{}' is not callable (due to union elements {}).",
called_ty.display(db),
not_callable.display(db),
called_ty.display(db)
),
),
}
Expand Down
29 changes: 27 additions & 2 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3486,7 +3486,7 @@ mod tests {
assert_file_diagnostics(
&db,
"src/a.py",
&["Union element 'Literal[1]' of type 'Literal[1] | Literal[f]' is not callable."],
&["Object of type 'Literal[1] | Literal[f]' is not callable (due to union element 'Literal[1]')."],
);
assert_public_ty(&db, "src/a.py", "x", "Unknown | int");

Expand Down Expand Up @@ -3515,14 +3515,39 @@ mod tests {
&db,
"src/a.py",
&[
r#"Union elements Literal[1], Literal["foo"] of type 'Literal[1] | Literal["foo"] | Literal[f]' are not callable."#,
r#"Object of type 'Literal[1] | Literal["foo"] | Literal[f]' is not callable (due to union elements Literal[1], Literal["foo"])."#,
],
);
assert_public_ty(&db, "src/a.py", "x", "Unknown | int");

Ok(())
}

#[test]
fn call_union_with_all_not_callable() -> anyhow::Result<()> {
let mut db = setup_db();

db.write_dedented(
"src/a.py",
"
if flag:
f = 1
else:
f = 'foo'
x = f()
",
)?;

assert_file_diagnostics(
&db,
"src/a.py",
&[r#"Object of type 'Literal[1] | Literal["foo"]' is not callable."#],
);
assert_public_ty(&db, "src/a.py", "x", "Unknown");

Ok(())
}

#[test]
fn invalid_callable() {
let mut db = setup_db();
Expand Down

0 comments on commit 40c65dc

Please sign in to comment.