Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
carljm committed Jul 18, 2024
1 parent 3c0722a commit 68775e1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
3 changes: 2 additions & 1 deletion crates/red_knot_python_semantic/src/builtins.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use red_knot_module_resolver::builtins_module;

use crate::semantic_index::global_scope;
use crate::{semantic_index::symbol::ScopeId, Db};
use crate::semantic_index::symbol::ScopeId;
use crate::Db;

#[salsa::tracked]
pub(crate) fn builtins_scope(db: &dyn Db) -> Option<ScopeId<'_>> {
Expand Down
47 changes: 43 additions & 4 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ use ruff_db::parsed::parsed_module;
use ruff_python_ast as ast;
use ruff_python_ast::{ExprContext, TypeParams};

use crate::builtins::builtins_scope;
use crate::semantic_index::ast_ids::{HasScopedAstId, HasScopedUseId, ScopedExpressionId};
use crate::semantic_index::definition::{Definition, DefinitionKind, DefinitionNodeKey};
use crate::semantic_index::expression::Expression;
use crate::semantic_index::semantic_index;
use crate::semantic_index::symbol::NodeWithScopeKind;
use crate::semantic_index::symbol::{NodeWithScopeRef, ScopeId};
use crate::semantic_index::symbol::{FileScopeId, NodeWithScopeKind, NodeWithScopeRef, ScopeId};
use crate::semantic_index::SemanticIndex;
use crate::types::{
builtins_symbol_ty_by_name, definitions_ty, global_symbol_ty_by_name, ClassType, FunctionType,
Expand Down Expand Up @@ -685,8 +685,15 @@ impl<'db> TypeInferenceBuilder<'db> {
let symbol = symbols.symbol_by_name(id).unwrap();
if !symbol.is_defined() || !self.scope.is_function_like(self.db) {
// implicit global
let mut unbound_ty = global_symbol_ty_by_name(self.db, self.file, id);
if matches!(unbound_ty, Type::Unbound) {
let mut unbound_ty = if file_scope_id == FileScopeId::global() {
Type::Unbound
} else {
global_symbol_ty_by_name(self.db, self.file, id)
};
// fallback to builtins
if matches!(unbound_ty, Type::Unbound)
&& Some(self.scope) != builtins_scope(self.db)
{
unbound_ty = builtins_symbol_ty_by_name(self.db, id);
}
Some(unbound_ty)
Expand Down Expand Up @@ -1393,14 +1400,18 @@ mod tests {
#[test]
fn builtin_symbol_vendored_stdlib() -> anyhow::Result<()> {
let mut db = setup_db();

db.write_file("/src/a.py", "c = copyright")?;

assert_public_ty(&db, "/src/a.py", "c", "Literal[copyright]");

Ok(())
}

#[test]
fn builtin_symbol_custom_stdlib() -> anyhow::Result<()> {
let mut db = setup_db_with_custom_typeshed("/typeshed");

db.write_files([
("/src/a.py", "c = copyright"),
(
Expand All @@ -1409,7 +1420,35 @@ mod tests {
),
("/typeshed/stdlib/VERSIONS", "builtins: 3.8-"),
])?;

assert_public_ty(&db, "/src/a.py", "c", "Literal[copyright]");

Ok(())
}

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

db.write_file("/src/a.py", "x = foo; foo = 1")?;

assert_public_ty(&db, "/src/a.py", "x", "Unbound");

Ok(())
}

#[test]
fn unknown_builtin_later_defined() -> anyhow::Result<()> {
let mut db = setup_db_with_custom_typeshed("/typeshed");

db.write_files([
("/src/a.py", "x = foo"),
("/typeshed/stdlib/builtins.pyi", "foo = bar; bar = 1"),
("/typeshed/stdlib/VERSIONS", "builtins: 3.8-"),
])?;

assert_public_ty(&db, "/src/a.py", "x", "Unbound");

Ok(())
}

Expand Down

0 comments on commit 68775e1

Please sign in to comment.