From 2436cb2918948e688368c83b15c0dc060c2ea75b Mon Sep 17 00:00:00 2001 From: Aditya Pillai <29032680+pilleye@users.noreply.github.com> Date: Sun, 29 Sep 2024 22:01:01 -0500 Subject: [PATCH] don't resort to Unknown type for public unbound declarations --- crates/red_knot_python_semantic/src/types.rs | 4 +-- .../src/types/infer.rs | 25 ++++++++++++++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/crates/red_knot_python_semantic/src/types.rs b/crates/red_knot_python_semantic/src/types.rs index 21278a423291a..1b396b7636da9 100644 --- a/crates/red_knot_python_semantic/src/types.rs +++ b/crates/red_knot_python_semantic/src/types.rs @@ -58,9 +58,7 @@ fn symbol_ty_by_id<'db>(db: &'db dyn Db, scope: ScopeId<'db>, symbol: ScopedSymb Some(bindings_ty( db, use_def.public_bindings(symbol), - use_def - .public_may_be_unbound(symbol) - .then_some(Type::Unknown), + use_def.public_may_be_unbound(symbol).then_some(Type::Never), )) } else { None diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index b2a0c3d768dd9..b2b78c3d82e36 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -3620,7 +3620,7 @@ mod tests { )?; // TODO: sys.version_info, and need to understand @final and @type_check_only - assert_public_ty(&db, "src/a.py", "x", "Unknown | EllipsisType"); + assert_public_ty(&db, "src/a.py", "x", "EllipsisType | Unknown"); Ok(()) } @@ -3952,6 +3952,23 @@ mod tests { Ok(()) } + #[test] + fn resolve_unbound_public() -> anyhow::Result<()> { + let mut db = setup_db(); + + db.write_dedented( + "src/a.py", + " + if flag: + x: int = 1 + ", + )?; + + assert_public_ty(&db, "src/a.py", "x", "int"); + + Ok(()) + } + #[test] fn literal_int_arithmetic() -> anyhow::Result<()> { let mut db = setup_db(); @@ -5452,7 +5469,7 @@ mod tests { // 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_public_ty(&db, "src/a.py", "e", "BaseExceptionGroup"); Ok(()) } @@ -5478,7 +5495,7 @@ mod tests { // 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_public_ty(&db, "src/a.py", "e", "BaseExceptionGroup"); Ok(()) } @@ -5504,7 +5521,7 @@ mod tests { // 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_public_ty(&db, "src/a.py", "e", "BaseExceptionGroup"); Ok(()) }