Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[red-knot] Improve tests relating to type inference for exception handlers #13643

Merged
merged 1 commit into from
Oct 5, 2024
Merged
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
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
Loading