Split CallPath into QualifiedName and UnqualifiedName (#10210)

## Summary

Charlie can probably explain this better than I but it turns out,
`CallPath` is used for two different things:

* To represent unqualified names like `version` where `version` can be a
local variable or imported (e.g. `from sys import version` where the
full qualified name is `sys.version`)
* To represent resolved, full qualified names

This PR splits `CallPath` into two types to make this destinction clear.

> Note: I haven't renamed all `call_path` variables to `qualified_name`
or `unqualified_name`. I can do that if that's welcomed but I first want
to get feedback on the approach and naming overall.

## Test Plan

`cargo test`
This commit is contained in:
Micha Reiser 2024-03-04 10:06:51 +01:00 committed by GitHub
parent ba4328226d
commit a6d892b1f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
181 changed files with 1692 additions and 1412 deletions

View file

@ -418,11 +418,11 @@ impl<'a> Visitor<'a> for Checker<'a> {
self.semantic.add_module(module);
if alias.asname.is_none() && alias.name.contains('.') {
let call_path: Box<[&str]> = alias.name.split('.').collect();
let qualified_name: Box<[&str]> = alias.name.split('.').collect();
self.add_binding(
module,
alias.identifier(),
BindingKind::SubmoduleImport(SubmoduleImport { call_path }),
BindingKind::SubmoduleImport(SubmoduleImport { qualified_name }),
BindingFlags::EXTERNAL,
);
} else {
@ -439,11 +439,11 @@ impl<'a> Visitor<'a> for Checker<'a> {
}
let name = alias.asname.as_ref().unwrap_or(&alias.name);
let call_path: Box<[&str]> = alias.name.split('.').collect();
let qualified_name: Box<[&str]> = alias.name.split('.').collect();
self.add_binding(
name,
alias.identifier(),
BindingKind::Import(Import { call_path }),
BindingKind::Import(Import { qualified_name }),
flags,
);
}
@ -503,12 +503,12 @@ impl<'a> Visitor<'a> for Checker<'a> {
// Attempt to resolve any relative imports; but if we don't know the current
// module path, or the relative import extends beyond the package root,
// fallback to a literal representation (e.g., `[".", "foo"]`).
let call_path = collect_import_from_member(level, module, &alias.name)
let qualified_name = collect_import_from_member(level, module, &alias.name)
.into_boxed_slice();
self.add_binding(
name,
alias.identifier(),
BindingKind::FromImport(FromImport { call_path }),
BindingKind::FromImport(FromImport { qualified_name }),
flags,
);
}
@ -751,8 +751,8 @@ impl<'a> Visitor<'a> for Checker<'a> {
}) => {
let mut handled_exceptions = Exceptions::empty();
for type_ in extract_handled_exceptions(handlers) {
if let Some(call_path) = self.semantic.resolve_call_path(type_) {
match call_path.segments() {
if let Some(qualified_name) = self.semantic.resolve_qualified_name(type_) {
match qualified_name.segments() {
["", "NameError"] => {
handled_exceptions |= Exceptions::NAME_ERROR;
}
@ -1065,25 +1065,37 @@ impl<'a> Visitor<'a> for Checker<'a> {
}) => {
self.visit_expr(func);
let callable = self.semantic.resolve_call_path(func).and_then(|call_path| {
if self.semantic.match_typing_call_path(&call_path, "cast") {
let callable =
self.semantic
.resolve_qualified_name(func)
.and_then(|qualified_name| {
if self
.semantic
.match_typing_qualified_name(&qualified_name, "cast")
{
Some(typing::Callable::Cast)
} else if self.semantic.match_typing_call_path(&call_path, "NewType") {
} else if self
.semantic
.match_typing_qualified_name(&qualified_name, "NewType")
{
Some(typing::Callable::NewType)
} else if self.semantic.match_typing_call_path(&call_path, "TypeVar") {
} else if self
.semantic
.match_typing_qualified_name(&qualified_name, "TypeVar")
{
Some(typing::Callable::TypeVar)
} else if self
.semantic
.match_typing_call_path(&call_path, "NamedTuple")
.match_typing_qualified_name(&qualified_name, "NamedTuple")
{
Some(typing::Callable::NamedTuple)
} else if self
.semantic
.match_typing_call_path(&call_path, "TypedDict")
.match_typing_qualified_name(&qualified_name, "TypedDict")
{
Some(typing::Callable::TypedDict)
} else if matches!(
call_path.segments(),
qualified_name.segments(),
[
"mypy_extensions",
"Arg"
@ -1095,7 +1107,7 @@ impl<'a> Visitor<'a> for Checker<'a> {
]
) {
Some(typing::Callable::MypyExtension)
} else if matches!(call_path.segments(), ["", "bool"]) {
} else if matches!(qualified_name.segments(), ["", "bool"]) {
Some(typing::Callable::Bool)
} else {
None

View file

@ -1,44 +1,7 @@
use libcst_native::{
Expression, Name, NameOrAttribute, ParenthesizableWhitespace, SimpleWhitespace, UnaryOperation,
Expression, Name, ParenthesizableWhitespace, SimpleWhitespace, UnaryOperation,
};
fn compose_call_path_inner<'a>(expr: &'a Expression, parts: &mut Vec<&'a str>) {
match expr {
Expression::Call(expr) => {
compose_call_path_inner(&expr.func, parts);
}
Expression::Attribute(expr) => {
compose_call_path_inner(&expr.value, parts);
parts.push(expr.attr.value);
}
Expression::Name(expr) => {
parts.push(expr.value);
}
_ => {}
}
}
pub(crate) fn compose_call_path(expr: &Expression) -> Option<String> {
let mut segments = vec![];
compose_call_path_inner(expr, &mut segments);
if segments.is_empty() {
None
} else {
Some(segments.join("."))
}
}
pub(crate) fn compose_module_path(module: &NameOrAttribute) -> String {
match module {
NameOrAttribute::N(name) => name.value.to_string(),
NameOrAttribute::A(attr) => {
let name = attr.attr.value;
let prefix = compose_call_path(&attr.value);
prefix.map_or_else(|| name.to_string(), |prefix| format!("{prefix}.{name}"))
}
}
}
/// Return a [`ParenthesizableWhitespace`] containing a single space.
pub(crate) fn space() -> ParenthesizableWhitespace<'static> {
ParenthesizableWhitespace::SimpleWhitespace(SimpleWhitespace(" "))

View file

@ -2,14 +2,16 @@
//! and return the modified code snippet as output.
use anyhow::{bail, Result};
use libcst_native::{
Codegen, CodegenState, ImportNames, ParenthesizableWhitespace, SmallStatement, Statement,
Codegen, CodegenState, Expression, ImportNames, NameOrAttribute, ParenthesizableWhitespace,
SmallStatement, Statement,
};
use ruff_python_ast::name::UnqualifiedName;
use smallvec::{smallvec, SmallVec};
use ruff_python_ast::Stmt;
use ruff_python_codegen::Stylist;
use ruff_source_file::Locator;
use crate::cst::helpers::compose_module_path;
use crate::cst::matchers::match_statement;
/// Glue code to make libcst codegen work with ruff's Stylist
@ -78,7 +80,7 @@ pub(crate) fn remove_imports<'a>(
for member in member_names {
let alias_index = aliases
.iter()
.position(|alias| member == compose_module_path(&alias.name));
.position(|alias| member == qualified_name_from_name_or_attribute(&alias.name));
if let Some(index) = alias_index {
aliases.remove(index);
}
@ -142,7 +144,7 @@ pub(crate) fn retain_imports(
aliases.retain(|alias| {
member_names
.iter()
.any(|member| *member == compose_module_path(&alias.name))
.any(|member| *member == qualified_name_from_name_or_attribute(&alias.name))
});
// But avoid destroying any trailing comments.
@ -164,3 +166,40 @@ pub(crate) fn retain_imports(
Ok(tree.codegen_stylist(stylist))
}
fn collect_segments<'a>(expr: &'a Expression, parts: &mut SmallVec<[&'a str; 8]>) {
match expr {
Expression::Call(expr) => {
collect_segments(&expr.func, parts);
}
Expression::Attribute(expr) => {
collect_segments(&expr.value, parts);
parts.push(expr.attr.value);
}
Expression::Name(expr) => {
parts.push(expr.value);
}
_ => {}
}
}
fn unqualified_name_from_expression<'a>(expr: &'a Expression<'a>) -> Option<UnqualifiedName<'a>> {
let mut segments = smallvec![];
collect_segments(expr, &mut segments);
if segments.is_empty() {
None
} else {
Some(segments.into_iter().collect())
}
}
fn qualified_name_from_name_or_attribute(module: &NameOrAttribute) -> String {
match module {
NameOrAttribute::N(name) => name.value.to_string(),
NameOrAttribute::A(attr) => {
let name = attr.attr.value;
let prefix = unqualified_name_from_expression(&attr.value);
prefix.map_or_else(|| name.to_string(), |prefix| format!("{prefix}.{name}"))
}
}
}

View file

@ -232,7 +232,7 @@ impl Renamer {
}
BindingKind::SubmoduleImport(import) => {
// Ex) Rename `import pandas.core` to `import pandas as pd`.
let module_name = import.call_path.first().unwrap();
let module_name = import.qualified_name.first().unwrap();
Some(Edit::range_replacement(
format!("{module_name} as {target}"),
binding.range(),

View file

@ -68,8 +68,8 @@ pub(crate) fn variable_name_task_id(
// If the function doesn't come from Airflow, we can't do anything.
if !checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.segments()[0], "airflow"))
.resolve_qualified_name(func)
.is_some_and(|qualified_name| matches!(qualified_name.segments()[0], "airflow"))
{
return None;
}

View file

@ -4,6 +4,6 @@ use ruff_python_semantic::SemanticModel;
pub(super) fn is_sys(expr: &Expr, target: &str, semantic: &SemanticModel) -> bool {
semantic
.resolve_call_path(expr)
.is_some_and(|call_path| call_path.segments() == ["sys", target])
.resolve_qualified_name(expr)
.is_some_and(|qualified_name| qualified_name.segments() == ["sys", target])
}

View file

@ -53,8 +53,8 @@ pub(crate) fn name_or_attribute(checker: &mut Checker, expr: &Expr) {
if checker
.semantic()
.resolve_call_path(expr)
.is_some_and(|call_path| matches!(call_path.segments(), ["six", "PY3"]))
.resolve_qualified_name(expr)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["six", "PY3"]))
{
checker
.diagnostics

View file

@ -2,7 +2,7 @@ use ruff_python_ast::ExprCall;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::CallPath;
use ruff_python_ast::name::QualifiedName;
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
@ -41,9 +41,9 @@ impl Violation for BlockingHttpCallInAsyncFunction {
}
}
fn is_blocking_http_call(call_path: &CallPath) -> bool {
fn is_blocking_http_call(qualified_name: &QualifiedName) -> bool {
matches!(
call_path.segments(),
qualified_name.segments(),
["urllib", "request", "urlopen"]
| [
"httpx" | "requests",
@ -65,7 +65,7 @@ pub(crate) fn blocking_http_call(checker: &mut Checker, call: &ExprCall) {
if checker.semantic().in_async_context() {
if checker
.semantic()
.resolve_call_path(call.func.as_ref())
.resolve_qualified_name(call.func.as_ref())
.as_ref()
.is_some_and(is_blocking_http_call)
{

View file

@ -2,7 +2,7 @@ use ruff_python_ast::ExprCall;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::CallPath;
use ruff_python_ast::name::QualifiedName;
use ruff_python_semantic::Modules;
use ruff_text_size::Ranged;
@ -47,7 +47,7 @@ pub(crate) fn blocking_os_call(checker: &mut Checker, call: &ExprCall) {
if checker.semantic().in_async_context() {
if checker
.semantic()
.resolve_call_path(call.func.as_ref())
.resolve_qualified_name(call.func.as_ref())
.as_ref()
.is_some_and(is_unsafe_os_method)
{
@ -60,9 +60,9 @@ pub(crate) fn blocking_os_call(checker: &mut Checker, call: &ExprCall) {
}
}
fn is_unsafe_os_method(call_path: &CallPath) -> bool {
fn is_unsafe_os_method(qualified_name: &QualifiedName) -> bool {
matches!(
call_path.segments(),
qualified_name.segments(),
[
"os",
"popen"

View file

@ -58,9 +58,11 @@ pub(crate) fn open_sleep_or_subprocess_call(checker: &mut Checker, call: &ast::E
/// Returns `true` if the expression resolves to a blocking call, like `time.sleep` or
/// `subprocess.run`.
fn is_open_sleep_or_subprocess_call(func: &Expr, semantic: &SemanticModel) -> bool {
semantic.resolve_call_path(func).is_some_and(|call_path| {
semantic
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["", "open"]
| ["time", "sleep"]
| [
@ -94,10 +96,10 @@ fn is_open_call_from_pathlib(func: &Expr, semantic: &SemanticModel) -> bool {
// Path("foo").open()
// ```
if let Expr::Call(call) = value.as_ref() {
let Some(call_path) = semantic.resolve_call_path(call.func.as_ref()) else {
let Some(qualified_name) = semantic.resolve_qualified_name(call.func.as_ref()) else {
return false;
};
if call_path.segments() == ["pathlib", "Path"] {
if qualified_name.segments() == ["pathlib", "Path"] {
return true;
}
}
@ -123,6 +125,6 @@ fn is_open_call_from_pathlib(func: &Expr, semantic: &SemanticModel) -> bool {
};
semantic
.resolve_call_path(call.func.as_ref())
.is_some_and(|call_path| call_path.segments() == ["pathlib", "Path"])
.resolve_qualified_name(call.func.as_ref())
.is_some_and(|qualified_name| qualified_name.segments() == ["pathlib", "Path"])
}

View file

@ -23,13 +23,23 @@ pub(super) fn is_untyped_exception(type_: Option<&Expr>, semantic: &SemanticMode
type_.map_or(true, |type_| {
if let Expr::Tuple(ast::ExprTuple { elts, .. }) = &type_ {
elts.iter().any(|type_| {
semantic.resolve_call_path(type_).is_some_and(|call_path| {
matches!(call_path.segments(), ["", "Exception" | "BaseException"])
semantic
.resolve_qualified_name(type_)
.is_some_and(|qualified_name| {
matches!(
qualified_name.segments(),
["", "Exception" | "BaseException"]
)
})
})
} else {
semantic.resolve_call_path(type_).is_some_and(|call_path| {
matches!(call_path.segments(), ["", "Exception" | "BaseException"])
semantic
.resolve_qualified_name(type_)
.is_some_and(|qualified_name| {
matches!(
qualified_name.segments(),
["", "Exception" | "BaseException"]
)
})
}
})

View file

@ -2,7 +2,7 @@ use anyhow::Result;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::CallPath;
use ruff_python_ast::name::QualifiedName;
use ruff_python_ast::{self as ast, Expr, Operator};
use ruff_python_semantic::{Modules, SemanticModel};
use ruff_text_size::Ranged;
@ -66,8 +66,8 @@ pub(crate) fn bad_file_permissions(checker: &mut Checker, call: &ast::ExprCall)
if checker
.semantic()
.resolve_call_path(&call.func)
.is_some_and(|call_path| matches!(call_path.segments(), ["os", "chmod"]))
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["os", "chmod"]))
{
if let Some(mode_arg) = call.arguments.find_argument("mode", 1) {
match parse_mask(mode_arg, checker.semantic()) {
@ -101,8 +101,8 @@ pub(crate) fn bad_file_permissions(checker: &mut Checker, call: &ast::ExprCall)
const WRITE_WORLD: u16 = 0o2;
const EXECUTE_GROUP: u16 = 0o10;
fn py_stat(call_path: &CallPath) -> Option<u16> {
match call_path.segments() {
fn py_stat(qualified_name: &QualifiedName) -> Option<u16> {
match qualified_name.segments() {
["stat", "ST_MODE"] => Some(0o0),
["stat", "S_IFDOOR"] => Some(0o0),
["stat", "S_IFPORT"] => Some(0o0),
@ -155,7 +155,10 @@ fn parse_mask(expr: &Expr, semantic: &SemanticModel) -> Result<Option<u16>> {
Some(value) => Ok(Some(value)),
None => anyhow::bail!("int value out of range"),
},
Expr::Attribute(_) => Ok(semantic.resolve_call_path(expr).as_ref().and_then(py_stat)),
Expr::Attribute(_) => Ok(semantic
.resolve_qualified_name(expr)
.as_ref()
.and_then(py_stat)),
Expr::BinOp(ast::ExprBinOp {
left,
op,

View file

@ -42,10 +42,10 @@ pub(crate) fn django_raw_sql(checker: &mut Checker, call: &ast::ExprCall) {
if checker
.semantic()
.resolve_call_path(&call.func)
.is_some_and(|call_path| {
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["django", "db", "models", "expressions", "RawSQL"]
)
})

View file

@ -35,8 +35,8 @@ impl Violation for ExecBuiltin {
pub(crate) fn exec_used(checker: &mut Checker, func: &Expr) {
if checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.segments(), ["" | "builtin", "exec"]))
.resolve_qualified_name(func)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["" | "builtin", "exec"]))
{
checker
.diagnostics

View file

@ -65,7 +65,7 @@ pub(crate) fn flask_debug_true(checker: &mut Checker, call: &ExprCall) {
}
if typing::resolve_assignment(value, checker.semantic())
.is_some_and(|call_path| matches!(call_path.segments(), ["flask", "Flask"]))
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["flask", "Flask"]))
{
checker
.diagnostics

View file

@ -74,8 +74,8 @@ pub(crate) fn hardcoded_tmp_directory(checker: &mut Checker, string: StringLike)
{
if checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.segments(), ["tempfile", ..]))
.resolve_qualified_name(func)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["tempfile", ..]))
{
return;
}

View file

@ -61,11 +61,10 @@ impl Violation for HashlibInsecureHashFunction {
/// S324
pub(crate) fn hashlib_insecure_hash_functions(checker: &mut Checker, call: &ast::ExprCall) {
if let Some(hashlib_call) =
checker
if let Some(hashlib_call) = checker
.semantic()
.resolve_call_path(&call.func)
.and_then(|call_path| match call_path.segments() {
.resolve_qualified_name(&call.func)
.and_then(|qualified_name| match qualified_name.segments() {
["hashlib", "new"] => Some(HashlibCall::New),
["hashlib", "md4"] => Some(HashlibCall::WeakHash("md4")),
["hashlib", "md5"] => Some(HashlibCall::WeakHash("md5")),

View file

@ -61,8 +61,10 @@ impl Violation for Jinja2AutoescapeFalse {
pub(crate) fn jinja2_autoescape_false(checker: &mut Checker, call: &ast::ExprCall) {
if checker
.semantic()
.resolve_call_path(&call.func)
.is_some_and(|call_path| matches!(call_path.segments(), ["jinja2", "Environment"]))
.resolve_qualified_name(&call.func)
.is_some_and(|qualifieed_name| {
matches!(qualifieed_name.segments(), ["jinja2", "Environment"])
})
{
if let Some(keyword) = call.arguments.find_keyword("autoescape") {
match &keyword.value {

View file

@ -42,8 +42,10 @@ pub(crate) fn logging_config_insecure_listen(checker: &mut Checker, call: &ast::
if checker
.semantic()
.resolve_call_path(&call.func)
.is_some_and(|call_path| matches!(call_path.segments(), ["logging", "config", "listen"]))
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["logging", "config", "listen"])
})
{
if call.arguments.find_keyword("verify").is_some() {
return;

View file

@ -47,8 +47,10 @@ impl Violation for MakoTemplates {
pub(crate) fn mako_templates(checker: &mut Checker, call: &ast::ExprCall) {
if checker
.semantic()
.resolve_call_path(&call.func)
.is_some_and(|call_path| matches!(call_path.segments(), ["mako", "template", "Template"]))
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["mako", "template", "Template"])
})
{
checker
.diagnostics

View file

@ -39,8 +39,10 @@ impl Violation for ParamikoCall {
pub(crate) fn paramiko_call(checker: &mut Checker, func: &Expr) {
if checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.segments(), ["paramiko", "exec_command"]))
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["paramiko", "exec_command"])
})
{
checker
.diagnostics

View file

@ -49,8 +49,8 @@ impl Violation for RequestWithNoCertValidation {
pub(crate) fn request_with_no_cert_validation(checker: &mut Checker, call: &ast::ExprCall) {
if let Some(target) = checker
.semantic()
.resolve_call_path(&call.func)
.and_then(|call_path| match call_path.segments() {
.resolve_qualified_name(&call.func)
.and_then(|qualified_name| match qualified_name.segments() {
["requests", "get" | "options" | "head" | "post" | "put" | "patch" | "delete"] => {
Some("requests")
}

View file

@ -52,10 +52,10 @@ impl Violation for RequestWithoutTimeout {
pub(crate) fn request_without_timeout(checker: &mut Checker, call: &ast::ExprCall) {
if checker
.semantic()
.resolve_call_path(&call.func)
.is_some_and(|call_path| {
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
[
"requests",
"get" | "options" | "head" | "post" | "put" | "patch" | "delete"

View file

@ -419,8 +419,8 @@ enum CallKind {
/// Return the [`CallKind`] of the given function call.
fn get_call_kind(func: &Expr, semantic: &SemanticModel) -> Option<CallKind> {
semantic
.resolve_call_path(func)
.and_then(|call_path| match call_path.segments() {
.resolve_qualified_name(func)
.and_then(|qualified_name| match qualified_name.segments() {
&[module, submodule] => match module {
"os" => match submodule {
"execl" | "execle" | "execlp" | "execlpe" | "execv" | "execve" | "execvp"

View file

@ -44,9 +44,12 @@ impl Violation for SnmpInsecureVersion {
pub(crate) fn snmp_insecure_version(checker: &mut Checker, call: &ast::ExprCall) {
if checker
.semantic()
.resolve_call_path(&call.func)
.is_some_and(|call_path| {
matches!(call_path.segments(), ["pysnmp", "hlapi", "CommunityData"])
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| {
matches!(
qualified_name.segments(),
["pysnmp", "hlapi", "CommunityData"]
)
})
{
if let Some(keyword) = call.arguments.find_keyword("mpModel") {

View file

@ -45,9 +45,12 @@ pub(crate) fn snmp_weak_cryptography(checker: &mut Checker, call: &ast::ExprCall
if call.arguments.len() < 3 {
if checker
.semantic()
.resolve_call_path(&call.func)
.is_some_and(|call_path| {
matches!(call_path.segments(), ["pysnmp", "hlapi", "UsmUserData"])
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| {
matches!(
qualified_name.segments(),
["pysnmp", "hlapi", "UsmUserData"]
)
})
{
checker

View file

@ -60,10 +60,10 @@ pub(crate) fn ssh_no_host_key_verification(checker: &mut Checker, call: &ExprCal
// Detect either, e.g., `paramiko.client.AutoAddPolicy` or `paramiko.client.AutoAddPolicy()`.
if !checker
.semantic()
.resolve_call_path(map_callable(policy_argument))
.is_some_and(|call_path| {
.resolve_qualified_name(map_callable(policy_argument))
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["paramiko", "client", "AutoAddPolicy" | "WarningPolicy"]
| ["paramiko", "AutoAddPolicy" | "WarningPolicy"]
)
@ -72,9 +72,9 @@ pub(crate) fn ssh_no_host_key_verification(checker: &mut Checker, call: &ExprCal
return;
}
if typing::resolve_assignment(value, checker.semantic()).is_some_and(|call_path| {
if typing::resolve_assignment(value, checker.semantic()).is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["paramiko", "client", "SSHClient"] | ["paramiko", "SSHClient"]
)
}) {

View file

@ -51,8 +51,8 @@ impl Violation for SslInsecureVersion {
pub(crate) fn ssl_insecure_version(checker: &mut Checker, call: &ExprCall) {
let Some(keyword) = checker
.semantic()
.resolve_call_path(call.func.as_ref())
.and_then(|call_path| match call_path.segments() {
.resolve_qualified_name(call.func.as_ref())
.and_then(|qualified_name| match qualified_name.segments() {
["ssl", "wrap_socket"] => Some("ssl_version"),
["OpenSSL", "SSL", "Context"] => Some("method"),
_ => None,

View file

@ -39,8 +39,8 @@ impl Violation for SslWithNoVersion {
pub(crate) fn ssl_with_no_version(checker: &mut Checker, call: &ExprCall) {
if checker
.semantic()
.resolve_call_path(call.func.as_ref())
.is_some_and(|call_path| matches!(call_path.segments(), ["ssl", "wrap_socket"]))
.resolve_qualified_name(call.func.as_ref())
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["ssl", "wrap_socket"]))
{
if call.arguments.find_keyword("ssl_version").is_none() {
checker

View file

@ -825,8 +825,8 @@ impl Violation for SuspiciousFTPLibUsage {
/// S301, S302, S303, S304, S305, S306, S307, S308, S310, S311, S312, S313, S314, S315, S316, S317, S318, S319, S320, S321, S323
pub(crate) fn suspicious_function_call(checker: &mut Checker, call: &ExprCall) {
let Some(diagnostic_kind) = checker.semantic().resolve_call_path(call.func.as_ref()).and_then(|call_path| {
match call_path.segments() {
let Some(diagnostic_kind) = checker.semantic().resolve_qualified_name(call.func.as_ref()).and_then(|qualified_name| {
match qualified_name.segments() {
// Pickle
["pickle" | "dill", "load" | "loads" | "Unpickler"] |
["shelve", "open" | "DbfilenameShelf"] |
@ -906,9 +906,9 @@ pub(crate) fn suspicious_function_call(checker: &mut Checker, call: &ExprCall) {
pub(crate) fn suspicious_function_decorator(checker: &mut Checker, decorator: &Decorator) {
let Some(diagnostic_kind) = checker
.semantic()
.resolve_call_path(&decorator.expression)
.and_then(|call_path| {
match call_path.segments() {
.resolve_qualified_name(&decorator.expression)
.and_then(|qualified_name| {
match qualified_name.segments() {
// MarkSafe
["django", "utils", "safestring" | "html", "mark_safe"] => {
Some(SuspiciousMarkSafeUsage.into())

View file

@ -62,16 +62,16 @@ impl Violation for UnsafeYAMLLoad {
pub(crate) fn unsafe_yaml_load(checker: &mut Checker, call: &ast::ExprCall) {
if checker
.semantic()
.resolve_call_path(&call.func)
.is_some_and(|call_path| matches!(call_path.segments(), ["yaml", "load"]))
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["yaml", "load"]))
{
if let Some(loader_arg) = call.arguments.find_argument("Loader", 1) {
if !checker
.semantic()
.resolve_call_path(loader_arg)
.is_some_and(|call_path| {
.resolve_qualified_name(loader_arg)
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["yaml", "SafeLoader" | "CSafeLoader"]
| ["yaml", "loader", "SafeLoader" | "CSafeLoader"]
)

View file

@ -101,8 +101,8 @@ fn extract_cryptographic_key(
checker: &mut Checker,
call: &ExprCall,
) -> Option<(CryptographicKey, TextRange)> {
let call_path = checker.semantic().resolve_call_path(&call.func)?;
match call_path.segments() {
let qualified_name = checker.semantic().resolve_qualified_name(&call.func)?;
match qualified_name.segments() {
["cryptography", "hazmat", "primitives", "asymmetric", function, "generate_private_key"] => {
match *function {
"dsa" => {
@ -116,9 +116,9 @@ fn extract_cryptographic_key(
"ec" => {
let argument = call.arguments.find_argument("curve", 0)?;
let ExprAttribute { attr, value, .. } = argument.as_attribute_expr()?;
let call_path = checker.semantic().resolve_call_path(value)?;
let qualified_name = checker.semantic().resolve_qualified_name(value)?;
if matches!(
call_path.segments(),
qualified_name.segments(),
["cryptography", "hazmat", "primitives", "asymmetric", "ec"]
) {
Some((

View file

@ -140,8 +140,8 @@ pub(crate) fn blind_except(
Expr::Name(ast::ExprName { .. }) => {
if checker
.semantic()
.resolve_call_path(func.as_ref())
.is_some_and(|call_path| match call_path.segments() {
.resolve_qualified_name(func.as_ref())
.is_some_and(|qualified_name| match qualified_name.segments() {
["logging", "exception"] => true,
["logging", "error"] => {
if let Some(keyword) = arguments.find_keyword("exc_info") {

View file

@ -1,6 +1,6 @@
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::CallPath;
use ruff_python_ast::name::UnqualifiedName;
use ruff_python_ast::{Decorator, ParameterWithDefault, Parameters};
use ruff_python_semantic::analyze::visibility;
use ruff_text_size::Ranged;
@ -119,8 +119,8 @@ pub(crate) fn boolean_default_value_positional_argument(
{
// Allow Boolean defaults in setters.
if decorator_list.iter().any(|decorator| {
CallPath::from_expr(&decorator.expression)
.is_some_and(|call_path| call_path.segments() == [name, "setter"])
UnqualifiedName::from_expr(&decorator.expression)
.is_some_and(|unqualified_name| unqualified_name.segments() == [name, "setter"])
}) {
return;
}

View file

@ -1,9 +1,8 @@
use ruff_python_ast::{self as ast, Decorator, Expr, ParameterWithDefault, Parameters};
use ruff_diagnostics::Diagnostic;
use ruff_diagnostics::Violation;
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::CallPath;
use ruff_python_ast::name::UnqualifiedName;
use ruff_python_ast::{self as ast, Decorator, Expr, ParameterWithDefault, Parameters};
use ruff_python_semantic::analyze::visibility;
use ruff_python_semantic::SemanticModel;
use ruff_text_size::Ranged;
@ -136,8 +135,8 @@ pub(crate) fn boolean_type_hint_positional_argument(
// Allow Boolean type hints in setters.
if decorator_list.iter().any(|decorator| {
CallPath::from_expr(&decorator.expression)
.is_some_and(|call_path| call_path.segments() == [name, "setter"])
UnqualifiedName::from_expr(&decorator.expression)
.is_some_and(|unqualified_name| unqualified_name.segments() == [name, "setter"])
}) {
return;
}
@ -196,11 +195,10 @@ fn match_annotation_to_complex_bool(annotation: &Expr, semantic: &SemanticModel)
return false;
}
let call_path = semantic.resolve_call_path(value);
if call_path
.as_ref()
.is_some_and(|call_path| semantic.match_typing_call_path(call_path, "Union"))
{
let qualified_name = semantic.resolve_qualified_name(value);
if qualified_name.as_ref().is_some_and(|qualified_name| {
semantic.match_typing_qualified_name(qualified_name, "Union")
}) {
if let Expr::Tuple(ast::ExprTuple { elts, .. }) = slice.as_ref() {
elts.iter()
.any(|elt| match_annotation_to_complex_bool(elt, semantic))
@ -208,10 +206,9 @@ fn match_annotation_to_complex_bool(annotation: &Expr, semantic: &SemanticModel)
// Union with a single type is an invalid type annotation
false
}
} else if call_path
.as_ref()
.is_some_and(|call_path| semantic.match_typing_call_path(call_path, "Optional"))
{
} else if qualified_name.as_ref().is_some_and(|qualified_name| {
semantic.match_typing_qualified_name(qualified_name, "Optional")
}) {
match_annotation_to_complex_bool(slice, semantic)
} else {
false

View file

@ -109,12 +109,14 @@ fn is_abc_class(bases: &[Expr], keywords: &[Keyword], semantic: &SemanticModel)
keywords.iter().any(|keyword| {
keyword.arg.as_ref().is_some_and(|arg| arg == "metaclass")
&& semantic
.resolve_call_path(&keyword.value)
.is_some_and(|call_path| matches!(call_path.segments(), ["abc", "ABCMeta"]))
.resolve_qualified_name(&keyword.value)
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["abc", "ABCMeta"])
})
}) || bases.iter().any(|base| {
semantic
.resolve_call_path(base)
.is_some_and(|call_path| matches!(call_path.segments(), ["abc", "ABC"]))
.resolve_qualified_name(base)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["abc", "ABC"]))
})
}

View file

@ -95,10 +95,11 @@ pub(crate) fn assert_raises_exception(checker: &mut Checker, items: &[WithItem])
return;
};
let Some(exception) = checker
let Some(exception) =
checker
.semantic()
.resolve_call_path(arg)
.and_then(|call_path| match call_path.segments() {
.resolve_qualified_name(arg)
.and_then(|qualified_name| match qualified_name.segments() {
["", "Exception"] => Some(ExceptionKind::Exception),
["", "BaseException"] => Some(ExceptionKind::BaseException),
_ => None,
@ -112,8 +113,8 @@ pub(crate) fn assert_raises_exception(checker: &mut Checker, items: &[WithItem])
AssertionKind::AssertRaises
} else if checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.segments(), ["pytest", "raises"]))
.resolve_qualified_name(func)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["pytest", "raises"]))
&& arguments.find_keyword("match").is_none()
{
AssertionKind::PytestRaises

View file

@ -71,8 +71,13 @@ impl Violation for CachedInstanceMethod {
}
fn is_cache_func(expr: &Expr, semantic: &SemanticModel) -> bool {
semantic.resolve_call_path(expr).is_some_and(|call_path| {
matches!(call_path.segments(), ["functools", "lru_cache" | "cache"])
semantic
.resolve_qualified_name(expr)
.is_some_and(|qualified_name| {
matches!(
qualified_name.segments(),
["functools", "lru_cache" | "cache"]
)
})
}

View file

@ -1,12 +1,12 @@
use itertools::Itertools;
use ruff_python_ast::{self as ast, ExceptHandler, Expr, ExprContext};
use ruff_text_size::{Ranged, TextRange};
use rustc_hash::{FxHashMap, FxHashSet};
use ruff_diagnostics::{AlwaysFixableViolation, Violation};
use ruff_diagnostics::{Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::CallPath;
use ruff_python_ast::name::UnqualifiedName;
use ruff_python_ast::{self as ast, ExceptHandler, Expr, ExprContext};
use ruff_text_size::{Ranged, TextRange};
use crate::checkers::ast::Checker;
use crate::fix::edits::pad;
@ -118,16 +118,16 @@ fn duplicate_handler_exceptions<'a>(
checker: &mut Checker,
expr: &'a Expr,
elts: &'a [Expr],
) -> FxHashMap<CallPath<'a>, &'a Expr> {
let mut seen: FxHashMap<CallPath, &Expr> = FxHashMap::default();
let mut duplicates: FxHashSet<CallPath> = FxHashSet::default();
) -> FxHashMap<UnqualifiedName<'a>, &'a Expr> {
let mut seen: FxHashMap<UnqualifiedName, &Expr> = FxHashMap::default();
let mut duplicates: FxHashSet<UnqualifiedName> = FxHashSet::default();
let mut unique_elts: Vec<&Expr> = Vec::default();
for type_ in elts {
if let Some(call_path) = CallPath::from_expr(type_) {
if seen.contains_key(&call_path) {
duplicates.insert(call_path);
if let Some(name) = UnqualifiedName::from_expr(type_) {
if seen.contains_key(&name) {
duplicates.insert(name);
} else {
seen.entry(call_path).or_insert(type_);
seen.entry(name).or_insert(type_);
unique_elts.push(type_);
}
}
@ -140,7 +140,7 @@ fn duplicate_handler_exceptions<'a>(
DuplicateHandlerException {
names: duplicates
.into_iter()
.map(|call_path| call_path.segments().join("."))
.map(|qualified_name| qualified_name.segments().join("."))
.sorted()
.collect::<Vec<String>>(),
},
@ -171,8 +171,8 @@ fn duplicate_handler_exceptions<'a>(
/// B025
pub(crate) fn duplicate_exceptions(checker: &mut Checker, handlers: &[ExceptHandler]) {
let mut seen: FxHashSet<CallPath> = FxHashSet::default();
let mut duplicates: FxHashMap<CallPath, Vec<&Expr>> = FxHashMap::default();
let mut seen: FxHashSet<UnqualifiedName> = FxHashSet::default();
let mut duplicates: FxHashMap<UnqualifiedName, Vec<&Expr>> = FxHashMap::default();
for handler in handlers {
let ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
type_: Some(type_),
@ -183,11 +183,11 @@ pub(crate) fn duplicate_exceptions(checker: &mut Checker, handlers: &[ExceptHand
};
match type_.as_ref() {
Expr::Attribute(_) | Expr::Name(_) => {
if let Some(call_path) = CallPath::from_expr(type_) {
if seen.contains(&call_path) {
duplicates.entry(call_path).or_default().push(type_);
if let Some(name) = UnqualifiedName::from_expr(type_) {
if seen.contains(&name) {
duplicates.entry(name).or_default().push(type_);
} else {
seen.insert(call_path);
seen.insert(name);
}
}
}

View file

@ -4,7 +4,7 @@ use ruff_text_size::{Ranged, TextRange};
use ruff_diagnostics::Violation;
use ruff_diagnostics::{Diagnostic, DiagnosticKind};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::{compose_call_path, CallPath};
use ruff_python_ast::name::{QualifiedName, UnqualifiedName};
use ruff_python_ast::visitor;
use ruff_python_ast::visitor::Visitor;
use ruff_python_semantic::analyze::typing::{
@ -81,12 +81,15 @@ impl Violation for FunctionCallInDefaultArgument {
struct ArgumentDefaultVisitor<'a, 'b> {
semantic: &'a SemanticModel<'b>,
extend_immutable_calls: &'a [CallPath<'b>],
extend_immutable_calls: &'a [QualifiedName<'b>],
diagnostics: Vec<(DiagnosticKind, TextRange)>,
}
impl<'a, 'b> ArgumentDefaultVisitor<'a, 'b> {
fn new(semantic: &'a SemanticModel<'b>, extend_immutable_calls: &'a [CallPath<'b>]) -> Self {
fn new(
semantic: &'a SemanticModel<'b>,
extend_immutable_calls: &'a [QualifiedName<'b>],
) -> Self {
Self {
semantic,
extend_immutable_calls,
@ -104,7 +107,7 @@ impl Visitor<'_> for ArgumentDefaultVisitor<'_, '_> {
{
self.diagnostics.push((
FunctionCallInDefaultArgument {
name: compose_call_path(func),
name: UnqualifiedName::from_expr(func).map(|name| name.to_string()),
}
.into(),
expr.range(),
@ -123,12 +126,12 @@ impl Visitor<'_> for ArgumentDefaultVisitor<'_, '_> {
/// B008
pub(crate) fn function_call_in_argument_default(checker: &mut Checker, parameters: &Parameters) {
// Map immutable calls to (module, member) format.
let extend_immutable_calls: Vec<CallPath> = checker
let extend_immutable_calls: Vec<QualifiedName> = checker
.settings
.flake8_bugbear
.extend_immutable_calls
.iter()
.map(|target| CallPath::from_qualified_name(target))
.map(|target| QualifiedName::from_dotted_name(target))
.collect();
let mut visitor = ArgumentDefaultVisitor::new(checker.semantic(), &extend_immutable_calls);

View file

@ -1,7 +1,7 @@
use ast::call_path::CallPath;
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::is_docstring_stmt;
use ruff_python_ast::name::QualifiedName;
use ruff_python_ast::{self as ast, Expr, Parameter, ParameterWithDefault, Stmt};
use ruff_python_codegen::{Generator, Stylist};
use ruff_python_index::Indexer;
@ -98,12 +98,12 @@ pub(crate) fn mutable_argument_default(checker: &mut Checker, function_def: &ast
continue;
};
let extend_immutable_calls: Vec<CallPath> = checker
let extend_immutable_calls: Vec<QualifiedName> = checker
.settings
.flake8_bugbear
.extend_immutable_calls
.iter()
.map(|target| CallPath::from_qualified_name(target))
.map(|target| QualifiedName::from_dotted_name(target))
.collect();
if is_mutable_expr(default, checker.semantic())

View file

@ -40,8 +40,8 @@ impl Violation for NoExplicitStacklevel {
pub(crate) fn no_explicit_stacklevel(checker: &mut Checker, call: &ast::ExprCall) {
if !checker
.semantic()
.resolve_call_path(&call.func)
.is_some_and(|call_path| matches!(call_path.segments(), ["warnings", "warn"]))
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["warnings", "warn"]))
{
return;
}

View file

@ -63,8 +63,8 @@ pub(crate) fn re_sub_positional_args(checker: &mut Checker, call: &ast::ExprCall
let Some(method) = checker
.semantic()
.resolve_call_path(&call.func)
.and_then(|call_path| match call_path.segments() {
.resolve_qualified_name(&call.func)
.and_then(|qualified_name| match qualified_name.segments() {
["re", "sub"] => Some(Method::Sub),
["re", "subn"] => Some(Method::Subn),
["re", "split"] => Some(Method::Split),

View file

@ -310,8 +310,8 @@ pub(crate) fn reuse_of_groupby_generator(
// Check if the function call is `itertools.groupby`
if !checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.segments(), ["itertools", "groupby"]))
.resolve_qualified_name(func)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["itertools", "groupby"]))
{
return;
}

View file

@ -59,8 +59,10 @@ pub(crate) fn useless_contextlib_suppress(
if args.is_empty()
&& checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.segments(), ["contextlib", "suppress"]))
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["contextlib", "suppress"])
})
{
checker
.diagnostics

View file

@ -99,8 +99,10 @@ fn is_infinite_iterator(arg: &Expr, semantic: &SemanticModel) -> bool {
return false;
};
semantic.resolve_call_path(func).is_some_and(|call_path| {
match call_path.segments() {
semantic
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
match qualified_name.segments() {
["itertools", "cycle" | "count"] => true,
["itertools", "repeat"] => {
// Ex) `itertools.repeat(1)`

View file

@ -64,9 +64,12 @@ pub(crate) fn call_date_fromtimestamp(checker: &mut Checker, func: &Expr, locati
if checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| {
matches!(call_path.segments(), ["datetime", "date", "fromtimestamp"])
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(
qualified_name.segments(),
["datetime", "date", "fromtimestamp"]
)
})
{
checker

View file

@ -63,8 +63,10 @@ pub(crate) fn call_date_today(checker: &mut Checker, func: &Expr, location: Text
if checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.segments(), ["datetime", "date", "today"]))
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["datetime", "date", "today"])
})
{
checker
.diagnostics

View file

@ -67,10 +67,10 @@ pub(crate) fn call_datetime_fromtimestamp(checker: &mut Checker, call: &ast::Exp
if !checker
.semantic()
.resolve_call_path(&call.func)
.is_some_and(|call_path| {
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["datetime", "datetime", "fromtimestamp"]
)
})

View file

@ -63,8 +63,10 @@ pub(crate) fn call_datetime_now_without_tzinfo(checker: &mut Checker, call: &ast
if !checker
.semantic()
.resolve_call_path(&call.func)
.is_some_and(|call_path| matches!(call_path.segments(), ["datetime", "datetime", "now"]))
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["datetime", "datetime", "now"])
})
{
return;
}

View file

@ -71,9 +71,12 @@ pub(crate) fn call_datetime_strptime_without_zone(checker: &mut Checker, call: &
if !checker
.semantic()
.resolve_call_path(&call.func)
.is_some_and(|call_path| {
matches!(call_path.segments(), ["datetime", "datetime", "strptime"])
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| {
matches!(
qualified_name.segments(),
["datetime", "datetime", "strptime"]
)
})
{
return;

View file

@ -62,8 +62,10 @@ pub(crate) fn call_datetime_today(checker: &mut Checker, func: &Expr, location:
if !checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.segments(), ["datetime", "datetime", "today"]))
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["datetime", "datetime", "today"])
})
{
return;
}

View file

@ -70,10 +70,10 @@ pub(crate) fn call_datetime_utcfromtimestamp(
if !checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| {
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["datetime", "datetime", "utcfromtimestamp"]
)
})

View file

@ -66,8 +66,13 @@ pub(crate) fn call_datetime_utcnow(checker: &mut Checker, func: &Expr, location:
if !checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.segments(), ["datetime", "datetime", "utcnow"]))
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(
qualified_name.segments(),
["datetime", "datetime", "utcnow"]
)
})
{
return;
}

View file

@ -59,8 +59,8 @@ pub(crate) fn call_datetime_without_tzinfo(checker: &mut Checker, call: &ast::Ex
if !checker
.semantic()
.resolve_call_path(&call.func)
.is_some_and(|call_path| matches!(call_path.segments(), ["datetime", "datetime"]))
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["datetime", "datetime"]))
{
return;
}

View file

@ -2,7 +2,7 @@ use ruff_python_ast::{Expr, Stmt};
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::{CallPath, CallPathBuilder};
use ruff_python_ast::name::{QualifiedName, QualifiedNameBuilder};
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
@ -48,12 +48,13 @@ impl Violation for Debugger {
/// Checks for the presence of a debugger call.
pub(crate) fn debugger_call(checker: &mut Checker, expr: &Expr, func: &Expr) {
if let Some(using_type) = checker
if let Some(using_type) =
checker
.semantic()
.resolve_call_path(func)
.and_then(|call_path| {
if is_debugger_call(&call_path) {
Some(DebuggerUsingType::Call(call_path.to_string()))
.resolve_qualified_name(func)
.and_then(|qualified_name| {
if is_debugger_call(&qualified_name) {
Some(DebuggerUsingType::Call(qualified_name.to_string()))
} else {
None
}
@ -68,22 +69,23 @@ pub(crate) fn debugger_call(checker: &mut Checker, expr: &Expr, func: &Expr) {
/// Checks for the presence of a debugger import.
pub(crate) fn debugger_import(stmt: &Stmt, module: Option<&str>, name: &str) -> Option<Diagnostic> {
if let Some(module) = module {
let mut builder = CallPathBuilder::from_path(CallPath::from_unqualified_name(module));
let mut builder =
QualifiedNameBuilder::from_qualified_name(QualifiedName::imported(module));
builder.push(name);
let call_path = builder.build();
let qualified_name = builder.build();
if is_debugger_call(&call_path) {
if is_debugger_call(&qualified_name) {
return Some(Diagnostic::new(
Debugger {
using_type: DebuggerUsingType::Import(call_path.to_string()),
using_type: DebuggerUsingType::Import(qualified_name.to_string()),
},
stmt.range(),
));
}
} else {
let call_path: CallPath = CallPath::from_unqualified_name(name);
let qualified_name = QualifiedName::imported(name);
if is_debugger_import(&call_path) {
if is_debugger_import(&qualified_name) {
return Some(Diagnostic::new(
Debugger {
using_type: DebuggerUsingType::Import(name.to_string()),
@ -95,9 +97,9 @@ pub(crate) fn debugger_import(stmt: &Stmt, module: Option<&str>, name: &str) ->
None
}
fn is_debugger_call(call_path: &CallPath) -> bool {
fn is_debugger_call(qualified_name: &QualifiedName) -> bool {
matches!(
call_path.segments(),
qualified_name.segments(),
["pdb" | "pudb" | "ipdb", "set_trace"]
| ["ipdb", "sset_trace"]
| ["IPython", "terminal", "embed", "InteractiveShellEmbed"]
@ -115,13 +117,13 @@ fn is_debugger_call(call_path: &CallPath) -> bool {
)
}
fn is_debugger_import(call_path: &CallPath) -> bool {
fn is_debugger_import(qualified_name: &QualifiedName) -> bool {
// Constructed by taking every pattern in `is_debugger_call`, removing the last element in
// each pattern, and de-duplicating the values.
// As a special-case, we omit `builtins` to allow `import builtins`, which is far more general
// than (e.g.) `import celery.contrib.rdb`.
matches!(
call_path.segments(),
qualified_name.segments(),
["pdb" | "pudb" | "ipdb" | "debugpy" | "ptvsd"]
| ["IPython", "terminal", "embed"]
| ["IPython", "frontend", "terminal", "embed",]

View file

@ -4,16 +4,19 @@ use ruff_python_semantic::{analyze, SemanticModel};
/// Return `true` if a Python class appears to be a Django model, based on its base classes.
pub(super) fn is_model(class_def: &ast::StmtClassDef, semantic: &SemanticModel) -> bool {
analyze::class::any_call_path(class_def, semantic, &|call_path| {
matches!(call_path.segments(), ["django", "db", "models", "Model"])
analyze::class::any_qualified_name(class_def, semantic, &|qualified_name| {
matches!(
qualified_name.segments(),
["django", "db", "models", "Model"]
)
})
}
/// Return `true` if a Python class appears to be a Django model form, based on its base classes.
pub(super) fn is_model_form(class_def: &ast::StmtClassDef, semantic: &SemanticModel) -> bool {
analyze::class::any_call_path(class_def, semantic, &|call_path| {
analyze::class::any_qualified_name(class_def, semantic, &|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["django", "forms", "ModelForm"] | ["django", "forms", "models", "ModelForm"]
)
})
@ -21,8 +24,10 @@ pub(super) fn is_model_form(class_def: &ast::StmtClassDef, semantic: &SemanticMo
/// Return `true` if the expression is constructor for a Django model field.
pub(super) fn is_model_field(expr: &Expr, semantic: &SemanticModel) -> bool {
semantic.resolve_call_path(expr).is_some_and(|call_path| {
call_path
semantic
.resolve_qualified_name(expr)
.is_some_and(|qualified_name| {
qualified_name
.segments()
.starts_with(&["django", "db", "models"])
})
@ -33,11 +38,13 @@ pub(super) fn get_model_field_name<'a>(
expr: &'a Expr,
semantic: &'a SemanticModel,
) -> Option<&'a str> {
semantic.resolve_call_path(expr).and_then(|call_path| {
let call_path = call_path.segments();
if !call_path.starts_with(&["django", "db", "models"]) {
semantic
.resolve_qualified_name(expr)
.and_then(|qualified_name| {
let qualified_name = qualified_name.segments();
if !qualified_name.starts_with(&["django", "db", "models"]) {
return None;
}
call_path.last().copied()
qualified_name.last().copied()
})
}

View file

@ -51,8 +51,10 @@ pub(crate) fn locals_in_render_function(checker: &mut Checker, call: &ast::ExprC
if !checker
.semantic()
.resolve_call_path(&call.func)
.is_some_and(|call_path| matches!(call_path.segments(), ["django", "shortcuts", "render"]))
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["django", "shortcuts", "render"])
})
{
return;
}
@ -72,6 +74,6 @@ fn is_locals_call(expr: &Expr, semantic: &SemanticModel) -> bool {
return false;
};
semantic
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.segments(), ["", "locals"]))
.resolve_qualified_name(func)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["", "locals"]))
}

View file

@ -61,9 +61,12 @@ pub(crate) fn non_leading_receiver_decorator(checker: &mut Checker, decorator_li
let is_receiver = decorator.expression.as_call_expr().is_some_and(|call| {
checker
.semantic()
.resolve_call_path(&call.func)
.is_some_and(|call_path| {
matches!(call_path.segments(), ["django", "dispatch", "receiver"])
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| {
matches!(
qualified_name.segments(),
["django", "dispatch", "receiver"]
)
})
});
if i > 0 && is_receiver && !seen_receiver {

View file

@ -80,7 +80,7 @@ impl Violation for FutureRewritableTypeAnnotation {
pub(crate) fn future_rewritable_type_annotation(checker: &mut Checker, expr: &Expr) {
let name = checker
.semantic()
.resolve_call_path(expr)
.resolve_qualified_name(expr)
.map(|binding| binding.to_string());
if let Some(name) = name {

View file

@ -61,8 +61,8 @@ pub(crate) fn direct_logger_instantiation(checker: &mut Checker, call: &ast::Exp
if checker
.semantic()
.resolve_call_path(call.func.as_ref())
.is_some_and(|call_path| matches!(call_path.segments(), ["logging", "Logger"]))
.resolve_qualified_name(call.func.as_ref())
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["logging", "Logger"]))
{
let mut diagnostic = Diagnostic::new(DirectLoggerInstantiation, call.func.range());
diagnostic.try_set_fix(|| {

View file

@ -62,8 +62,10 @@ pub(crate) fn exception_without_exc_info(checker: &mut Checker, call: &ExprCall)
Expr::Name(_) => {
if !checker
.semantic()
.resolve_call_path(call.func.as_ref())
.is_some_and(|call_path| matches!(call_path.segments(), ["logging", "exception"]))
.resolve_qualified_name(call.func.as_ref())
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["logging", "exception"])
})
{
return;
}

View file

@ -77,8 +77,8 @@ pub(crate) fn invalid_get_logger_argument(checker: &mut Checker, call: &ast::Exp
if !checker
.semantic()
.resolve_call_path(call.func.as_ref())
.is_some_and(|call_path| matches!(call_path.segments(), ["logging", "getLogger"]))
.resolve_qualified_name(call.func.as_ref())
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["logging", "getLogger"]))
{
return;
}

View file

@ -55,8 +55,8 @@ pub(crate) fn undocumented_warn(checker: &mut Checker, expr: &Expr) {
if checker
.semantic()
.resolve_call_path(expr)
.is_some_and(|call_path| matches!(call_path.segments(), ["logging", "WARN"]))
.resolve_qualified_name(expr)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["logging", "WARN"]))
{
let mut diagnostic = Diagnostic::new(UndocumentedWarn, expr.range());
diagnostic.try_set_fix(|| {

View file

@ -110,8 +110,8 @@ fn check_log_record_attr_clash(checker: &mut Checker, extra: &Keyword) {
}) => {
if checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.segments(), ["", "dict"]))
.resolve_qualified_name(func)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["", "dict"]))
{
for keyword in keywords.iter() {
if let Some(attr) = &keyword.arg {
@ -165,10 +165,13 @@ pub(crate) fn logging_call(checker: &mut Checker, call: &ast::ExprCall) {
(call_type, attr.range())
}
Expr::Name(_) => {
let Some(call_path) = checker.semantic().resolve_call_path(call.func.as_ref()) else {
let Some(qualified_name) = checker
.semantic()
.resolve_qualified_name(call.func.as_ref())
else {
return;
};
let ["logging", attribute] = call_path.segments() else {
let ["logging", attribute] = qualified_name.segments() else {
return;
};
let Some(call_type) = LoggingCallType::from_attribute(attribute) else {

View file

@ -62,8 +62,8 @@ pub(crate) fn non_unique_enums(checker: &mut Checker, parent: &Stmt, body: &[Stm
if !parent.bases().iter().any(|expr| {
checker
.semantic()
.resolve_call_path(expr)
.is_some_and(|call_path| matches!(call_path.segments(), ["enum", "Enum"]))
.resolve_qualified_name(expr)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["enum", "Enum"]))
}) {
return;
}
@ -77,8 +77,8 @@ pub(crate) fn non_unique_enums(checker: &mut Checker, parent: &Stmt, body: &[Stm
if let Expr::Call(ast::ExprCall { func, .. }) = value.as_ref() {
if checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.segments(), ["enum", "auto"]))
.resolve_qualified_name(func)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["enum", "auto"]))
{
continue;
}

View file

@ -98,30 +98,31 @@ impl Violation for PPrint {
/// T201, T203
pub(crate) fn print_call(checker: &mut Checker, call: &ast::ExprCall) {
let mut diagnostic = {
let call_path = checker.semantic().resolve_call_path(&call.func);
if call_path
let qualified_name = checker.semantic().resolve_qualified_name(&call.func);
if qualified_name
.as_ref()
.is_some_and(|call_path| matches!(call_path.segments(), ["", "print"]))
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["", "print"]))
{
// If the print call has a `file=` argument (that isn't `None`, `"sys.stdout"`,
// or `"sys.stderr"`), don't trigger T201.
if let Some(keyword) = call.arguments.find_keyword("file") {
if !keyword.value.is_none_literal_expr() {
if checker.semantic().resolve_call_path(&keyword.value).map_or(
true,
|call_path| {
call_path.segments() != ["sys", "stdout"]
&& call_path.segments() != ["sys", "stderr"]
},
) {
if checker
.semantic()
.resolve_qualified_name(&keyword.value)
.map_or(true, |qualified_name| {
qualified_name.segments() != ["sys", "stdout"]
&& qualified_name.segments() != ["sys", "stderr"]
})
{
return;
}
}
}
Diagnostic::new(Print, call.func.range())
} else if call_path
} else if qualified_name
.as_ref()
.is_some_and(|call_path| matches!(call_path.segments(), ["pprint", "pprint"]))
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["pprint", "pprint"]))
{
Diagnostic::new(PPrint, call.func.range())
} else {

View file

@ -125,10 +125,10 @@ pub(crate) fn bad_generator_return_type(
// Determine the module from which the existing annotation is imported (e.g., `typing` or
// `collections.abc`)
let (method, module, member) = {
let Some(call_path) = semantic.resolve_call_path(map_subscript(returns)) else {
let Some(qualified_name) = semantic.resolve_qualified_name(map_subscript(returns)) else {
return;
};
match (name, call_path.segments()) {
match (name, qualified_name.segments()) {
("__iter__", ["typing", "Generator"]) => {
(Method::Iter, Module::Typing, Generator::Generator)
}

View file

@ -75,8 +75,8 @@ pub(crate) fn bad_version_info_comparison(checker: &mut Checker, test: &Expr) {
if !checker
.semantic()
.resolve_call_path(left)
.is_some_and(|call_path| matches!(call_path.segments(), ["sys", "version_info"]))
.resolve_qualified_name(left)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["sys", "version_info"]))
{
return;
}

View file

@ -57,8 +57,10 @@ pub(crate) fn collections_named_tuple(checker: &mut Checker, expr: &Expr) {
if checker
.semantic()
.resolve_call_path(expr)
.is_some_and(|call_path| matches!(call_path.segments(), ["collections", "namedtuple"]))
.resolve_qualified_name(expr)
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["collections", "namedtuple"])
})
{
checker
.diagnostics

View file

@ -67,9 +67,12 @@ pub(crate) fn complex_if_statement_in_stub(checker: &mut Checker, test: &Expr) {
if checker
.semantic()
.resolve_call_path(left)
.is_some_and(|call_path| {
matches!(call_path.segments(), ["sys", "version_info" | "platform"])
.resolve_qualified_name(left)
.is_some_and(|qualified_name| {
matches!(
qualified_name.segments(),
["sys", "version_info" | "platform"]
)
})
{
return;

View file

@ -244,11 +244,11 @@ fn non_none_annotation_element<'a>(
) -> Option<&'a Expr> {
// E.g., `typing.Union` or `typing.Optional`
if let Expr::Subscript(ExprSubscript { value, slice, .. }) = annotation {
let call_path = semantic.resolve_call_path(value);
let qualified_name = semantic.resolve_qualified_name(value);
if call_path
if qualified_name
.as_ref()
.is_some_and(|value| semantic.match_typing_call_path(value, "Optional"))
.is_some_and(|value| semantic.match_typing_qualified_name(value, "Optional"))
{
return if slice.is_none_literal_expr() {
None
@ -257,9 +257,9 @@ fn non_none_annotation_element<'a>(
};
}
if !call_path
if !qualified_name
.as_ref()
.is_some_and(|value| semantic.match_typing_call_path(value, "Union"))
.is_some_and(|value| semantic.match_typing_qualified_name(value, "Union"))
{
return None;
}
@ -305,11 +305,11 @@ fn non_none_annotation_element<'a>(
/// Return `true` if the [`Expr`] is the `object` builtin or the `_typeshed.Unused` type.
fn is_object_or_unused(expr: &Expr, semantic: &SemanticModel) -> bool {
semantic
.resolve_call_path(expr)
.resolve_qualified_name(expr)
.as_ref()
.is_some_and(|call_path| {
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["" | "builtins", "object"] | ["_typeshed", "Unused"]
)
})
@ -318,17 +318,24 @@ fn is_object_or_unused(expr: &Expr, semantic: &SemanticModel) -> bool {
/// Return `true` if the [`Expr`] is `BaseException`.
fn is_base_exception(expr: &Expr, semantic: &SemanticModel) -> bool {
semantic
.resolve_call_path(expr)
.resolve_qualified_name(expr)
.as_ref()
.is_some_and(|call_path| matches!(call_path.segments(), ["" | "builtins", "BaseException"]))
.is_some_and(|qualified_name| {
matches!(
qualified_name.segments(),
["" | "builtins", "BaseException"]
)
})
}
/// Return `true` if the [`Expr`] is the `types.TracebackType` type.
fn is_traceback_type(expr: &Expr, semantic: &SemanticModel) -> bool {
semantic
.resolve_call_path(expr)
.resolve_qualified_name(expr)
.as_ref()
.is_some_and(|call_path| matches!(call_path.segments(), ["types", "TracebackType"]))
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["types", "TracebackType"])
})
}
/// Return `true` if the [`Expr`] is, e.g., `Type[BaseException]`.
@ -339,9 +346,11 @@ fn is_base_exception_type(expr: &Expr, semantic: &SemanticModel) -> bool {
if semantic.match_typing_expr(value, "Type")
|| semantic
.resolve_call_path(value)
.resolve_qualified_name(value)
.as_ref()
.is_some_and(|call_path| matches!(call_path.segments(), ["" | "builtins", "type"]))
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["" | "builtins", "type"])
})
{
is_base_exception(slice, semantic)
} else {

View file

@ -88,16 +88,16 @@ pub(crate) fn iter_method_return_iterable(checker: &mut Checker, definition: &De
if checker
.semantic()
.resolve_call_path(map_subscript(annotation))
.is_some_and(|call_path| {
.resolve_qualified_name(map_subscript(annotation))
.is_some_and(|qualified_name| {
if is_async {
matches!(
call_path.segments(),
qualified_name.segments(),
["typing", "AsyncIterable"] | ["collections", "abc", "AsyncIterable"]
)
} else {
matches!(
call_path.segments(),
qualified_name.segments(),
["typing", "Iterable"] | ["collections", "abc", "Iterable"]
)
}

View file

@ -231,9 +231,11 @@ fn is_metaclass(class_def: &ast::StmtClassDef, semantic: &SemanticModel) -> bool
/// Returns `true` if the given expression resolves to a metaclass.
fn is_metaclass_base(base: &Expr, semantic: &SemanticModel) -> bool {
semantic.resolve_call_path(base).is_some_and(|call_path| {
semantic
.resolve_qualified_name(base)
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["" | "builtins", "type"] | ["abc", "ABCMeta"] | ["enum", "EnumMeta" | "EnumType"]
)
})
@ -279,10 +281,10 @@ fn is_iterator(arguments: Option<&Arguments>, semantic: &SemanticModel) -> bool
};
bases.iter().any(|expr| {
semantic
.resolve_call_path(map_subscript(expr))
.is_some_and(|call_path| {
.resolve_qualified_name(map_subscript(expr))
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["typing", "Iterator"] | ["collections", "abc", "Iterator"]
)
})
@ -292,10 +294,10 @@ fn is_iterator(arguments: Option<&Arguments>, semantic: &SemanticModel) -> bool
/// Return `true` if the given expression resolves to `collections.abc.Iterable`.
fn is_iterable(expr: &Expr, semantic: &SemanticModel) -> bool {
semantic
.resolve_call_path(map_subscript(expr))
.is_some_and(|call_path| {
.resolve_qualified_name(map_subscript(expr))
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["typing", "Iterable" | "Iterator"]
| ["collections", "abc", "Iterable" | "Iterator"]
)
@ -309,10 +311,10 @@ fn is_async_iterator(arguments: Option<&Arguments>, semantic: &SemanticModel) ->
};
bases.iter().any(|expr| {
semantic
.resolve_call_path(map_subscript(expr))
.is_some_and(|call_path| {
.resolve_qualified_name(map_subscript(expr))
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["typing", "AsyncIterator"] | ["collections", "abc", "AsyncIterator"]
)
})
@ -322,10 +324,10 @@ fn is_async_iterator(arguments: Option<&Arguments>, semantic: &SemanticModel) ->
/// Return `true` if the given expression resolves to `collections.abc.AsyncIterable`.
fn is_async_iterable(expr: &Expr, semantic: &SemanticModel) -> bool {
semantic
.resolve_call_path(map_subscript(expr))
.is_some_and(|call_path| {
.resolve_qualified_name(map_subscript(expr))
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["typing", "AsyncIterable" | "AsyncIterator"]
| ["collections", "abc", "AsyncIterable" | "AsyncIterator"]
)

View file

@ -81,21 +81,21 @@ pub(crate) fn prefix_type_params(checker: &mut Checker, value: &Expr, targets: &
let Some(kind) = checker
.semantic()
.resolve_call_path(func)
.and_then(|call_path| {
.resolve_qualified_name(func)
.and_then(|qualified_name| {
if checker
.semantic()
.match_typing_call_path(&call_path, "ParamSpec")
.match_typing_qualified_name(&qualified_name, "ParamSpec")
{
Some(VarKind::ParamSpec)
} else if checker
.semantic()
.match_typing_call_path(&call_path, "TypeVar")
.match_typing_qualified_name(&qualified_name, "TypeVar")
{
Some(VarKind::TypeVar)
} else if checker
.semantic()
.match_typing_call_path(&call_path, "TypeVarTuple")
.match_typing_qualified_name(&qualified_name, "TypeVarTuple")
{
Some(VarKind::TypeVarTuple)
} else {

View file

@ -90,11 +90,11 @@ fn check_annotation(checker: &mut Checker, annotation: &Expr) {
let mut has_int = false;
let mut func = |expr: &Expr, _parent: &Expr| {
let Some(call_path) = checker.semantic().resolve_call_path(expr) else {
let Some(qualified_name) = checker.semantic().resolve_qualified_name(expr) else {
return;
};
match call_path.segments() {
match qualified_name.segments() {
["" | "builtins", "int"] => has_int = true,
["" | "builtins", "float"] => has_float = true,
["" | "builtins", "complex"] => has_complex = true,

View file

@ -2,8 +2,8 @@ use rustc_hash::FxHashSet;
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::CallPath;
use ruff_python_ast::helpers::map_subscript;
use ruff_python_ast::name::QualifiedName;
use ruff_python_ast::{
self as ast, Expr, Operator, ParameterWithDefault, Parameters, Stmt, UnaryOp,
};
@ -248,13 +248,16 @@ impl AlwaysFixableViolation for TypeAliasWithoutAnnotation {
}
}
fn is_allowed_negated_math_attribute(call_path: &CallPath) -> bool {
matches!(call_path.segments(), ["math", "inf" | "e" | "pi" | "tau"])
fn is_allowed_negated_math_attribute(qualified_name: &QualifiedName) -> bool {
matches!(
qualified_name.segments(),
["math", "inf" | "e" | "pi" | "tau"]
)
}
fn is_allowed_math_attribute(call_path: &CallPath) -> bool {
fn is_allowed_math_attribute(qualified_name: &QualifiedName) -> bool {
matches!(
call_path.segments(),
qualified_name.segments(),
["math", "inf" | "nan" | "e" | "pi" | "tau"]
| [
"sys",
@ -324,7 +327,7 @@ fn is_valid_default_value_with_annotation(
// Ex) `-math.inf`, `-math.pi`, etc.
Expr::Attribute(_) => {
if semantic
.resolve_call_path(operand)
.resolve_qualified_name(operand)
.as_ref()
.is_some_and(is_allowed_negated_math_attribute)
{
@ -373,7 +376,7 @@ fn is_valid_default_value_with_annotation(
// Ex) `math.inf`, `sys.stdin`, etc.
Expr::Attribute(_) => {
if semantic
.resolve_call_path(default)
.resolve_qualified_name(default)
.as_ref()
.is_some_and(is_allowed_math_attribute)
{
@ -435,9 +438,11 @@ fn is_type_var_like_call(expr: &Expr, semantic: &SemanticModel) -> bool {
let Expr::Call(ast::ExprCall { func, .. }) = expr else {
return false;
};
semantic.resolve_call_path(func).is_some_and(|call_path| {
semantic
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
[
"typing" | "typing_extensions",
"TypeVar" | "TypeVarTuple" | "NewType" | "ParamSpec"
@ -480,10 +485,10 @@ fn is_enum(class_def: &ast::StmtClassDef, semantic: &SemanticModel) -> bool {
class_def.bases().iter().any(|expr| {
// If the base class is `enum.Enum`, `enum.Flag`, etc., then this is an enum.
if semantic
.resolve_call_path(map_subscript(expr))
.is_some_and(|call_path| {
.resolve_qualified_name(map_subscript(expr))
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
[
"enum",
"Enum" | "Flag" | "IntEnum" | "IntFlag" | "StrEnum" | "ReprEnum"

View file

@ -80,9 +80,9 @@ pub(crate) fn str_or_repr_defined_in_stub(checker: &mut Checker, stmt: &Stmt) {
if checker
.semantic()
.resolve_call_path(returns)
.map_or(true, |call_path| {
!matches!(call_path.segments(), ["" | "builtins", "str"])
.resolve_qualified_name(returns)
.map_or(true, |qualified_name| {
!matches!(qualified_name.segments(), ["" | "builtins", "str"])
})
{
return;

View file

@ -84,10 +84,10 @@ fn is_warnings_dot_deprecated(expr: Option<&ast::Expr>, semantic: &SemanticModel
return false;
};
semantic
.resolve_call_path(&call.func)
.is_some_and(|call_path| {
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["warnings" | "typing_extensions", "deprecated"]
)
})

View file

@ -80,8 +80,10 @@ pub(crate) fn unnecessary_type_union<'a>(checker: &mut Checker, union: &'a Expr)
let unwrapped = subscript.unwrap();
if checker
.semantic()
.resolve_call_path(unwrapped.value.as_ref())
.is_some_and(|call_path| matches!(call_path.segments(), ["" | "builtins", "type"]))
.resolve_qualified_name(unwrapped.value.as_ref())
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["" | "builtins", "type"])
})
{
type_exprs.push(unwrapped.slice.as_ref());
} else {

View file

@ -107,8 +107,8 @@ pub(crate) fn unrecognized_platform(checker: &mut Checker, test: &Expr) {
if !checker
.semantic()
.resolve_call_path(left)
.is_some_and(|call_path| matches!(call_path.segments(), ["sys", "platform"]))
.resolve_qualified_name(left)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["sys", "platform"]))
{
return;
}

View file

@ -135,8 +135,8 @@ pub(crate) fn unrecognized_version_info(checker: &mut Checker, test: &Expr) {
if !checker
.semantic()
.resolve_call_path(map_subscript(left))
.is_some_and(|call_path| matches!(call_path.segments(), ["sys", "version_info"]))
.resolve_qualified_name(map_subscript(left))
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["sys", "version_info"]))
{
return;
}

View file

@ -195,17 +195,22 @@ pub(crate) fn unused_private_type_var(
};
let semantic = checker.semantic();
let Some(type_var_like_kind) = semantic.resolve_call_path(func).and_then(|call_path| {
if semantic.match_typing_call_path(&call_path, "TypeVar") {
let Some(type_var_like_kind) =
semantic
.resolve_qualified_name(func)
.and_then(|qualified_name| {
if semantic.match_typing_qualified_name(&qualified_name, "TypeVar") {
Some("TypeVar")
} else if semantic.match_typing_call_path(&call_path, "ParamSpec") {
} else if semantic.match_typing_qualified_name(&qualified_name, "ParamSpec") {
Some("ParamSpec")
} else if semantic.match_typing_call_path(&call_path, "TypeVarTuple") {
} else if semantic.match_typing_qualified_name(&qualified_name, "TypeVarTuple")
{
Some("TypeVarTuple")
} else {
None
}
}) else {
})
else {
continue;
};

View file

@ -3,8 +3,8 @@ use std::fmt;
use ruff_diagnostics::{AlwaysFixableViolation, Violation};
use ruff_diagnostics::{Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::CallPath;
use ruff_python_ast::identifier::Identifier;
use ruff_python_ast::name::UnqualifiedName;
use ruff_python_ast::visitor;
use ruff_python_ast::visitor::Visitor;
use ruff_python_ast::Decorator;
@ -646,9 +646,9 @@ impl<'a> Visitor<'a> for SkipFunctionsVisitor<'a> {
}
}
Expr::Call(ast::ExprCall { func, .. }) => {
if CallPath::from_expr(func).is_some_and(|call_path| {
matches!(call_path.segments(), ["request", "addfinalizer"])
}) {
if UnqualifiedName::from_expr(func)
.is_some_and(|name| matches!(name.segments(), ["request", "addfinalizer"]))
{
self.addfinalizer_call = Some(expr);
};
visitor::walk_expr(self, expr);

View file

@ -1,7 +1,6 @@
use ruff_python_ast::call_path::CallPath;
use ruff_python_ast::{self as ast, Decorator, Expr, Keyword};
use ruff_python_ast::helpers::map_callable;
use ruff_python_ast::name::UnqualifiedName;
use ruff_python_ast::{self as ast, Decorator, Expr, Keyword};
use ruff_python_semantic::SemanticModel;
use ruff_python_trivia::PythonWhitespace;
@ -9,10 +8,10 @@ pub(super) fn get_mark_decorators(
decorators: &[Decorator],
) -> impl Iterator<Item = (&Decorator, &str)> {
decorators.iter().filter_map(|decorator| {
let Some(call_path) = CallPath::from_expr(map_callable(&decorator.expression)) else {
let Some(name) = UnqualifiedName::from_expr(map_callable(&decorator.expression)) else {
return None;
};
let ["pytest", "mark", marker] = call_path.segments() else {
let ["pytest", "mark", marker] = name.segments() else {
return None;
};
Some((decorator, *marker))
@ -21,26 +20,30 @@ pub(super) fn get_mark_decorators(
pub(super) fn is_pytest_fail(call: &Expr, semantic: &SemanticModel) -> bool {
semantic
.resolve_call_path(call)
.is_some_and(|call_path| matches!(call_path.segments(), ["pytest", "fail"]))
.resolve_qualified_name(call)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["pytest", "fail"]))
}
pub(super) fn is_pytest_fixture(decorator: &Decorator, semantic: &SemanticModel) -> bool {
semantic
.resolve_call_path(map_callable(&decorator.expression))
.is_some_and(|call_path| matches!(call_path.segments(), ["pytest", "fixture"]))
.resolve_qualified_name(map_callable(&decorator.expression))
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["pytest", "fixture"]))
}
pub(super) fn is_pytest_yield_fixture(decorator: &Decorator, semantic: &SemanticModel) -> bool {
semantic
.resolve_call_path(map_callable(&decorator.expression))
.is_some_and(|call_path| matches!(call_path.segments(), ["pytest", "yield_fixture"]))
.resolve_qualified_name(map_callable(&decorator.expression))
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["pytest", "yield_fixture"])
})
}
pub(super) fn is_pytest_parametrize(decorator: &Decorator, semantic: &SemanticModel) -> bool {
semantic
.resolve_call_path(map_callable(&decorator.expression))
.is_some_and(|call_path| matches!(call_path.segments(), ["pytest", "mark", "parametrize"]))
.resolve_qualified_name(map_callable(&decorator.expression))
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["pytest", "mark", "parametrize"])
})
}
pub(super) fn keyword_is_literal(keyword: &Keyword, literal: &str) -> bool {

View file

@ -1,6 +1,6 @@
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::CallPath;
use ruff_python_ast::name::UnqualifiedName;
use ruff_python_ast::visitor;
use ruff_python_ast::visitor::Visitor;
use ruff_python_ast::{self as ast, Expr, Parameters};
@ -104,10 +104,10 @@ fn check_patch_call(call: &ast::ExprCall, index: usize) -> Option<Diagnostic> {
/// PT008
pub(crate) fn patch_with_lambda(call: &ast::ExprCall) -> Option<Diagnostic> {
let call_path = CallPath::from_expr(&call.func)?;
let name = UnqualifiedName::from_expr(&call.func)?;
if matches!(
call_path.segments(),
name.segments(),
[
"mocker"
| "class_mocker"
@ -120,7 +120,7 @@ pub(crate) fn patch_with_lambda(call: &ast::ExprCall) -> Option<Diagnostic> {
) {
check_patch_call(call, 1)
} else if matches!(
call_path.segments(),
name.segments(),
[
"mocker"
| "class_mocker"

View file

@ -153,8 +153,8 @@ impl Violation for PytestRaisesWithoutException {
fn is_pytest_raises(func: &Expr, semantic: &SemanticModel) -> bool {
semantic
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.segments(), ["pytest", "raises"]))
.resolve_qualified_name(func)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["pytest", "raises"]))
}
const fn is_non_trivial_with_body(body: &[Stmt]) -> bool {
@ -226,11 +226,11 @@ pub(crate) fn complex_raises(
/// PT011
fn exception_needs_match(checker: &mut Checker, exception: &Expr) {
if let Some(call_path) = checker
if let Some(qualified_name) = checker
.semantic()
.resolve_call_path(exception)
.and_then(|call_path| {
let call_path = call_path.to_string();
.resolve_qualified_name(exception)
.and_then(|qualified_name| {
let qualified_name = qualified_name.to_string();
checker
.settings
.flake8_pytest_style
@ -242,13 +242,13 @@ fn exception_needs_match(checker: &mut Checker, exception: &Expr) {
.flake8_pytest_style
.raises_extend_require_match_for,
)
.any(|pattern| pattern.matches(&call_path))
.then_some(call_path)
.any(|pattern| pattern.matches(&qualified_name))
.then_some(qualified_name)
})
{
checker.diagnostics.push(Diagnostic::new(
PytestRaisesTooBroad {
exception: call_path,
exception: qualified_name,
},
exception.range(),
));

View file

@ -99,8 +99,10 @@ pub(crate) fn unnecessary_paren_on_raise_exception(checker: &mut Checker, expr:
// we might as well get it right.
if checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.segments(), ["ctypes", "WinError"]))
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["ctypes", "WinError"])
})
{
return;
}

View file

@ -400,16 +400,19 @@ fn implicit_return_value(checker: &mut Checker, stack: &Stack) {
fn is_noreturn_func(func: &Expr, semantic: &SemanticModel) -> bool {
// First, look for known functions that never return from the standard library and popular
// libraries.
if semantic.resolve_call_path(func).is_some_and(|call_path| {
if semantic
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["" | "builtins" | "sys" | "_thread" | "pytest", "exit"]
| ["" | "builtins", "quit"]
| ["os" | "posix", "_exit" | "abort"]
| ["_winapi", "ExitProcess"]
| ["pytest", "fail" | "skip" | "xfail"]
) || semantic.match_typing_call_path(&call_path, "assert_never")
}) {
) || semantic.match_typing_qualified_name(&qualified_name, "assert_never")
})
{
return true;
}
@ -430,11 +433,11 @@ fn is_noreturn_func(func: &Expr, semantic: &SemanticModel) -> bool {
return false;
};
let Some(call_path) = semantic.resolve_call_path(returns) else {
let Some(qualified_name) = semantic.resolve_qualified_name(returns) else {
return false;
};
semantic.match_typing_call_path(&call_path, "NoReturn")
semantic.match_typing_qualified_name(&qualified_name, "NoReturn")
}
/// RET503

View file

@ -198,8 +198,8 @@ fn has_conditional_body(with: &ast::StmtWith, semantic: &SemanticModel) -> bool
else {
return false;
};
if let Some(call_path) = semantic.resolve_call_path(func) {
if call_path.segments() == ["contextlib", "suppress"] {
if let Some(qualified_name) = semantic.resolve_qualified_name(func) {
if qualified_name.segments() == ["contextlib", "suppress"] {
return true;
}
}

View file

@ -1,6 +1,6 @@
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::CallPath;
use ruff_python_ast::name::UnqualifiedName;
use ruff_python_ast::{self as ast, Expr};
use ruff_python_semantic::{BindingKind, ScopeKind};
use ruff_text_size::Ranged;
@ -141,24 +141,24 @@ pub(crate) fn private_member_access(checker: &mut Checker, expr: &Expr) {
}
// Allow some documented private methods, like `os._exit()`.
if let Some(call_path) = checker.semantic().resolve_call_path(expr) {
if matches!(call_path.segments(), ["os", "_exit"]) {
if let Some(qualified_name) = checker.semantic().resolve_qualified_name(expr) {
if matches!(qualified_name.segments(), ["os", "_exit"]) {
return;
}
}
if let Expr::Call(ast::ExprCall { func, .. }) = value.as_ref() {
// Ignore `super()` calls.
if let Some(call_path) = CallPath::from_expr(func) {
if matches!(call_path.segments(), ["super"]) {
if let Some(name) = UnqualifiedName::from_expr(func) {
if matches!(name.segments(), ["super"]) {
return;
}
}
}
if let Some(call_path) = CallPath::from_expr(value) {
if let Some(name) = UnqualifiedName::from_expr(value) {
// Ignore `self` and `cls` accesses.
if matches!(call_path.segments(), ["self" | "cls" | "mcs"]) {
if matches!(name.segments(), ["self" | "cls" | "mcs"]) {
return;
}
}

View file

@ -149,10 +149,10 @@ pub(crate) fn use_capital_environment_variables(checker: &mut Checker, expr: &Ex
};
if !checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| {
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["os", "environ", "get"] | ["os", "getenv"]
)
})

View file

@ -99,10 +99,10 @@ fn explicit_with_items(checker: &mut Checker, with_items: &[WithItem]) -> bool {
};
checker
.semantic()
.resolve_call_path(&expr_call.func)
.is_some_and(|call_path| {
.resolve_qualified_name(&expr_call.func)
.is_some_and(|qualified_name| {
matches!(
call_path.segments(),
qualified_name.segments(),
["asyncio", "timeout" | "timeout_at"]
| ["anyio", "CancelScope" | "fail_after" | "move_on_after"]
| [

View file

@ -63,9 +63,12 @@ fn match_async_exit_stack(semantic: &SemanticModel) -> bool {
if let Stmt::With(ast::StmtWith { items, .. }) = parent {
for item in items {
if let Expr::Call(ast::ExprCall { func, .. }) = &item.context_expr {
if semantic.resolve_call_path(func).is_some_and(|call_path| {
matches!(call_path.segments(), ["contextlib", "AsyncExitStack"])
}) {
if semantic
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["contextlib", "AsyncExitStack"])
})
{
return true;
}
}
@ -94,9 +97,12 @@ fn match_exit_stack(semantic: &SemanticModel) -> bool {
if let Stmt::With(ast::StmtWith { items, .. }) = parent {
for item in items {
if let Expr::Call(ast::ExprCall { func, .. }) = &item.context_expr {
if semantic.resolve_call_path(func).is_some_and(|call_path| {
matches!(call_path.segments(), ["contextlib", "ExitStack"])
}) {
if semantic
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["contextlib", "ExitStack"])
})
{
return true;
}
}
@ -114,8 +120,10 @@ fn is_open(checker: &mut Checker, func: &Expr) -> bool {
match value.as_ref() {
Expr::Call(ast::ExprCall { func, .. }) => checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.segments(), ["pathlib", "Path"])),
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["pathlib", "Path"])
}),
_ => false,
}
}

View file

@ -1,7 +1,7 @@
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::compose_call_path;
use ruff_python_ast::helpers;
use ruff_python_ast::name::UnqualifiedName;
use ruff_python_ast::{self as ast, ExceptHandler, Stmt};
use ruff_text_size::Ranged;
use ruff_text_size::{TextLen, TextRange};
@ -107,7 +107,7 @@ pub(crate) fn suppressible_exception(
let Some(handler_names) = helpers::extract_handled_exceptions(handlers)
.into_iter()
.map(compose_call_path)
.map(|expr| UnqualifiedName::from_expr(expr).map(|name| name.to_string()))
.collect::<Option<Vec<String>>>()
else {
return;

View file

@ -73,8 +73,10 @@ pub(crate) fn no_slots_in_namedtuple_subclass(
};
checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.segments(), ["collections", "namedtuple"]))
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["collections", "namedtuple"])
})
}) {
if !has_slots(&class.body) {
checker.diagnostics.push(Diagnostic::new(

View file

@ -69,8 +69,8 @@ pub(crate) fn no_slots_in_str_subclass(checker: &mut Checker, stmt: &Stmt, class
fn is_str_subclass(bases: &[Expr], semantic: &SemanticModel) -> bool {
let mut is_str_subclass = false;
for base in bases {
if let Some(call_path) = semantic.resolve_call_path(base) {
match call_path.segments() {
if let Some(qualified_name) = semantic.resolve_qualified_name(base) {
match qualified_name.segments() {
["" | "builtins", "str"] => {
is_str_subclass = true;
}

View file

@ -58,12 +58,12 @@ pub(crate) fn no_slots_in_tuple_subclass(checker: &mut Checker, stmt: &Stmt, cla
if bases.iter().any(|base| {
checker
.semantic()
.resolve_call_path(map_subscript(base))
.is_some_and(|call_path| {
matches!(call_path.segments(), ["" | "builtins", "tuple"])
.resolve_qualified_name(map_subscript(base))
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["" | "builtins", "tuple"])
|| checker
.semantic()
.match_typing_call_path(&call_path, "Tuple")
.match_typing_qualified_name(&qualified_name, "Tuple")
})
}) {
if !has_slots(&class.body) {

View file

@ -2,7 +2,7 @@ use ruff_python_ast::Expr;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::CallPath;
use ruff_python_ast::name::QualifiedName;
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
@ -65,10 +65,10 @@ pub(crate) fn banned_attribute_access(checker: &mut Checker, expr: &Expr) {
if let Some((banned_path, ban)) =
checker
.semantic()
.resolve_call_path(expr)
.and_then(|call_path| {
.resolve_qualified_name(expr)
.and_then(|qualified_name| {
banned_api.iter().find(|(banned_path, ..)| {
call_path == CallPath::from_qualified_name(banned_path)
qualified_name == QualifiedName::from_dotted_name(banned_path)
})
})
{

View file

@ -1,4 +1,4 @@
use ruff_python_ast::call_path::CallPath;
use ruff_python_ast::name::QualifiedName;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub(super) enum MethodName {
@ -70,8 +70,8 @@ impl MethodName {
}
impl MethodName {
pub(super) fn try_from(call_path: &CallPath<'_>) -> Option<Self> {
match call_path.segments() {
pub(super) fn try_from(qualified_name: &QualifiedName<'_>) -> Option<Self> {
match qualified_name.segments() {
["trio", "CancelScope"] => Some(Self::CancelScope),
["trio", "aclose_forcefully"] => Some(Self::AcloseForcefully),
["trio", "fail_after"] => Some(Self::FailAfter),

Some files were not shown because too many files have changed in this diff Show more