Skip to content

Commit

Permalink
[red-knot] Improve tests relating to type inference for exception han…
Browse files Browse the repository at this point in the history
…dlers (#13643)
  • Loading branch information
AlexWaygood authored Oct 5, 2024
1 parent 2a365bb commit 7c5a7d9
Showing 1 changed file with 55 additions and 32 deletions.
87 changes: 55 additions & 32 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5968,19 +5968,22 @@ mod tests {
"src/a.py",
"
import re
from typing_extensions import reveal_type
try:
x
except NameError as e:
pass
reveal_type(e)
except re.error as f:
pass
reveal_type(f)
",
)?;

assert_public_ty(&db, "src/a.py", "e", "NameError");
assert_public_ty(&db, "src/a.py", "f", "error");
assert_file_diagnostics(&db, "src/a.py", &[]);
assert_file_diagnostics(
&db,
"src/a.py",
&["Revealed type is `NameError`", "Revealed type is `error`"],
);

Ok(())
}
Expand All @@ -5993,21 +5996,25 @@ mod tests {
"src/a.py",
"
from nonexistent_module import foo
from typing_extensions import reveal_type
try:
x
except foo as e:
pass
reveal_type(foo)
reveal_type(e)
",
)?;

assert_file_diagnostics(
&db,
"src/a.py",
&["Cannot resolve import `nonexistent_module`"],
&[
"Cannot resolve import `nonexistent_module`",
"Revealed type is `Unknown`",
"Revealed type is `Unknown`",
],
);
assert_public_ty(&db, "src/a.py", "foo", "Unknown");
assert_public_ty(&db, "src/a.py", "e", "Unknown");

Ok(())
}
Expand All @@ -6019,25 +6026,28 @@ mod tests {
db.write_dedented(
"src/a.py",
"
from typing_extensions import reveal_type
EXCEPTIONS = (AttributeError, TypeError)
try:
x
except (RuntimeError, OSError) as e:
pass
reveal_type(e)
except EXCEPTIONS as f:
pass
reveal_type(f)
",
)?;

assert_file_diagnostics(&db, "src/a.py", &[]);

// For these TODOs we need support for `tuple` types:
let expected_diagnostics = &[
// TODO: Should be `RuntimeError | OSError` --Alex
"Revealed type is `@Todo`",
// TODO: Should be `AttributeError | TypeError` --Alex
"Revealed type is `@Todo`",
];

// TODO: Should be `RuntimeError | OSError` --Alex
assert_public_ty(&db, "src/a.py", "e", "@Todo");
// TODO: Should be `AttributeError | TypeError` --Alex
assert_public_ty(&db, "src/a.py", "e", "@Todo");
assert_file_diagnostics(&db, "src/a.py", expected_diagnostics);

Ok(())
}
Expand All @@ -6049,15 +6059,16 @@ mod tests {
db.write_dedented(
"src/a.py",
"
from typing_extensions import reveal_type
try:
x
except as e:
pass
reveal_type(e)
",
)?;

assert_file_diagnostics(&db, "src/a.py", &[]);
assert_public_ty(&db, "src/a.py", "e", "Unknown");
assert_file_diagnostics(&db, "src/a.py", &["Revealed type is `Unknown`"]);

Ok(())
}
Expand All @@ -6069,19 +6080,23 @@ mod tests {
db.write_dedented(
"src/a.py",
"
from typing_extensions import reveal_type
try:
x
except* BaseException as e:
pass
reveal_type(e)
",
)?;

assert_file_diagnostics(&db, "src/a.py", &[]);

// TODO: once we support `sys.version_info` branches,
// we can set `--target-version=py311` in this test
// and the inferred type will just be `BaseExceptionGroup` --Alex
assert_public_ty(&db, "src/a.py", "e", "Unknown | BaseExceptionGroup");
assert_file_diagnostics(
&db,
"src/a.py",
&["Revealed type is `Unknown | BaseExceptionGroup`"],
);

Ok(())
}
Expand All @@ -6093,21 +6108,25 @@ mod tests {
db.write_dedented(
"src/a.py",
"
from typing_extensions import reveal_type
try:
x
except* OSError as e:
pass
reveal_type(e)
",
)?;

assert_file_diagnostics(&db, "src/a.py", &[]);

// TODO: once we support `sys.version_info` branches,
// we can set `--target-version=py311` in this test
// and the inferred type will just be `BaseExceptionGroup` --Alex
//
// TODO more precise would be `ExceptionGroup[OSError]` --Alex
assert_public_ty(&db, "src/a.py", "e", "Unknown | BaseExceptionGroup");
assert_file_diagnostics(
&db,
"src/a.py",
&["Revealed type is `Unknown | BaseExceptionGroup`"],
);

Ok(())
}
Expand All @@ -6119,21 +6138,25 @@ mod tests {
db.write_dedented(
"src/a.py",
"
from typing_extensions import reveal_type
try:
x
except* (TypeError, AttributeError) as e:
pass
reveal_type(e)
",
)?;

assert_file_diagnostics(&db, "src/a.py", &[]);

// TODO: once we support `sys.version_info` branches,
// we can set `--target-version=py311` in this test
// and the inferred type will just be `BaseExceptionGroup` --Alex
//
// TODO more precise would be `ExceptionGroup[TypeError | AttributeError]` --Alex
assert_public_ty(&db, "src/a.py", "e", "Unknown | BaseExceptionGroup");
assert_file_diagnostics(
&db,
"src/a.py",
&["Revealed type is `Unknown | BaseExceptionGroup`"],
);

Ok(())
}
Expand Down

0 comments on commit 7c5a7d9

Please sign in to comment.