Skip to content

Commit

Permalink
Respect attribute chains when resolving builtin call paths
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Dec 29, 2023
1 parent ec88acc commit db28395
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Test for attribute accesses on builtins."""

a = type({}).__dict__['fromkeys']
b = dict.__dict__['fromkeys']
assert a is b
9 changes: 9 additions & 0 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,15 @@ where
} else {
self.visit_expr(value);

println!("visit_expr: {:?}", value);
let x = typing::match_annotated_subscript(
value,
&self.semantic,
self.settings.typing_modules.iter().map(String::as_str),
&self.settings.pyflakes.extend_generics,
);
println!("got: {:?}", x);

match typing::match_annotated_subscript(
value,
&self.semantic,
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/pyflakes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ mod tests {
#[test_case(Rule::UndefinedName, Path::new("F821_22.ipynb"))]
#[test_case(Rule::UndefinedName, Path::new("F821_23.py"))]
#[test_case(Rule::UndefinedName, Path::new("F821_24.py"))]
#[test_case(Rule::UndefinedName, Path::new("F821_25.py"))]
#[test_case(Rule::UndefinedExport, Path::new("F822_0.py"))]
#[test_case(Rule::UndefinedExport, Path::new("F822_1.py"))]
#[test_case(Rule::UndefinedExport, Path::new("F822_2.py"))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---

6 changes: 4 additions & 2 deletions crates/ruff_python_semantic/src/analyze/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::analyze::type_inference::{PythonType, ResolvedPythonType};
use crate::model::SemanticModel;
use crate::{Binding, BindingKind};

#[derive(Copy, Clone)]
#[derive(Debug, Copy, Clone)]
pub enum Callable {
Bool,
Cast,
Expand All @@ -26,7 +26,7 @@ pub enum Callable {
MypyExtension,
}

#[derive(Copy, Clone)]
#[derive(Debug, Copy, Clone)]
pub enum SubscriptKind {
/// A subscript of the form `typing.Literal["foo", "bar"]`, i.e., a literal.
Literal,
Expand All @@ -43,6 +43,8 @@ pub fn match_annotated_subscript<'a>(
extend_generics: &[String],
) -> Option<SubscriptKind> {
semantic.resolve_call_path(expr).and_then(|call_path| {
println!("call_path: {:?}", call_path);

if is_standard_library_literal(call_path.as_slice()) {
return Some(SubscriptKind::Literal);
}
Expand Down
15 changes: 14 additions & 1 deletion crates/ruff_python_semantic/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,20 @@ impl<'a> SemanticModel<'a> {
};
Some(resolved)
}
BindingKind::Builtin => Some(smallvec!["", head.id.as_str()]),
BindingKind::Builtin => {
if value.is_name_expr() {
// Ex) `dict`
Some(smallvec!["", head.id.as_str()])
} else {
// Ex) `dict.__dict__`
let value_path = collect_call_path(value)?;
Some(
std::iter::once("")
.chain(value_path.iter().copied())
.collect(),
)
}
}
BindingKind::ClassDefinition(_) | BindingKind::FunctionDefinition(_) => {
let value_path = collect_call_path(value)?;
let resolved: CallPath = self
Expand Down

0 comments on commit db28395

Please sign in to comment.