Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def good_func(arg1: Literal[int] | None):
...


# Regression test for https://github.com/astral-sh/ruff/issues/20729.
# Rewriting this changes `typing.get_args(options)` at runtime, so the fix is unsafe.
options = Literal["foo", "bar", None]


Comment thread
ntBre marked this conversation as resolved.
Outdated
# From flake8-pyi
Literal[None] # Y061 None inside "Literal[]" expression. Replace with "None"
Literal[True, None] # Y061 None inside "Literal[]" expression. Replace with "Literal[True] | None"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ use crate::{Applicability, Edit, Fix, FixAvailability, Violation};
/// ```
///
/// ## Fix safety and availability
/// This rule's fix is marked as safe unless the literal contains comments.
/// In Python files, this rule's fix is marked as unsafe because replacing
/// `Literal[...]` can change runtime-visible annotation objects, such as the
/// result of `typing.get_args`.
///
/// In stub files, the fix is marked as safe unless the literal contains comments.
///
/// There is currently no fix available when applying the fix would lead to
/// a `TypeError` from an expression of the form `None | None` or when we
Expand All @@ -62,10 +66,10 @@ impl Violation for RedundantNoneLiteral {
match self.union_kind {
UnionKind::NoUnion => "Use `None` rather than `Literal[None]`".to_string(),
UnionKind::TypingOptional => {
"Use `Optional[Literal[...]]` rather than `Literal[None, ...]` ".to_string()
"Use `Optional[Literal[...]]` rather than `Literal[None, ...]`".to_string()
}
UnionKind::BitOr => {
"Use `Literal[...] | None` rather than `Literal[None, ...]` ".to_string()
"Use `Literal[...] | None` rather than `Literal[None, ...]`".to_string()
}
}
}
Expand Down Expand Up @@ -192,7 +196,9 @@ fn create_fix(
}
}

let applicability = if checker.comment_ranges().intersects(literal_expr.range()) {
let applicability = if checker.comment_ranges().intersects(literal_expr.range())
|| !checker.source_type.is_stub()
{
Applicability::Unsafe
} else {
Applicability::Safe
Expand Down
Loading
Loading