Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ruff] Do not report when Optional has no type arguments (RUF013) #14181

Merged
merged 5 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF013_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# https://github.com/astral-sh/ruff/issues/13833

from typing import Optional


def no_default(arg: Optional): ...


def has_default(arg: Optional = None): ...


def multiple_1(arg1: Optional, arg2: Optional = None): ...


def multiple_2(arg1: Optional, arg2: Optional = None, arg3: int = None): ...


def return_type(arg: Optional = None) -> Optional: ...


def has_type_argument(arg: Optional[int] = None): ...
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/ruff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mod tests {
#[test_case(Rule::ImplicitOptional, Path::new("RUF013_1.py"))]
#[test_case(Rule::ImplicitOptional, Path::new("RUF013_2.py"))]
#[test_case(Rule::ImplicitOptional, Path::new("RUF013_3.py"))]
#[test_case(Rule::ImplicitOptional, Path::new("RUF013_4.py"))]
#[test_case(Rule::MutableClassDefault, Path::new("RUF012.py"))]
#[test_case(Rule::MutableDataclassDefault, Path::new("RUF008.py"))]
#[test_case(Rule::ZipInsteadOfPairwise, Path::new("RUF007.py"))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: crates/ruff_linter/src/rules/ruff/mod.rs
snapshot_kind: text
---
RUF013_4.py:15:61: RUF013 [*] PEP 484 prohibits implicit `Optional`
|
15 | def multiple_2(arg1: Optional, arg2: Optional = None, arg3: int = None): ...
| ^^^ RUF013
|
= help: Convert to `T | None`

ℹ Unsafe fix
12 12 | def multiple_1(arg1: Optional, arg2: Optional = None): ...
13 13 |
14 14 |
15 |-def multiple_2(arg1: Optional, arg2: Optional = None, arg3: int = None): ...
15 |+def multiple_2(arg1: Optional, arg2: Optional = None, arg3: int | None = None): ...
16 16 |
17 17 |
18 18 | def return_type(arg: Optional = None) -> Optional: ...
21 changes: 18 additions & 3 deletions crates/ruff_linter/src/rules/ruff/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ impl<'a> TypingTarget<'a> {
}
}

/// Whether `annotation` is `typing.Optional` with no type arguments.
///
/// See [#13833](https://github.com/astral-sh/ruff/issues/13833).
fn annotation_is_bare_optional(checker: &Checker, annotation: &Expr) -> bool {
let Some(qualified_name) = checker.semantic().resolve_qualified_name(annotation) else {
return false;
};

matches!(qualified_name.segments(), ["typing", "Optional"])
}

/// Check if the given annotation [`Expr`] explicitly allows `None`.
///
/// This function will return `None` if the annotation explicitly allows `None`
Expand All @@ -252,10 +263,14 @@ pub(crate) fn type_hint_explicitly_allows_none<'a>(
}
Some(target) => {
if target.contains_none(checker, minor_version) {
None
} else {
Some(annotation)
return None;
}

if annotation_is_bare_optional(checker, annotation) {
return None;
}

Some(annotation)
}
}
}
Expand Down
Loading