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 a49e7a9
Show file tree
Hide file tree
Showing 5 changed files with 26 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
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
---

4 changes: 2 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 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 a49e7a9

Please sign in to comment.