Skip to content

Commit 237d357

Browse files
committed
Tweaks
1 parent 0b98920 commit 237d357

File tree

3 files changed

+40
-117
lines changed

3 files changed

+40
-117
lines changed

crates/ruff/src/checkers/ast/analyze/expression.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,21 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
7474
}
7575

7676
// Ex) Union[...]
77-
if checker.any_enabled(&[
78-
Rule::UnnecessaryLiteralUnion,
79-
Rule::DuplicateUnionMember,
80-
Rule::RedundantLiteralUnion,
81-
]) {
82-
// Determine if the current expression is an union
83-
// To avoid duplicate checks, we examine whether current `expr` is an `Union[...]`
84-
// without a parent, since these rules traverse nested unions
85-
let is_unchecked_union = checker.semantic.expr_parent().is_none()
86-
&& expr.as_subscript_expr().map_or(false, |subscript| {
87-
checker
88-
.semantic
89-
.match_typing_expr(&subscript.value, "Union")
77+
if checker.is_stub
78+
&& checker.any_enabled(&[
79+
Rule::UnnecessaryLiteralUnion,
80+
Rule::DuplicateUnionMember,
81+
Rule::RedundantLiteralUnion,
82+
])
83+
{
84+
// Avoid duplicate checks if the parent is an `Union[...]` since these rules
85+
// traverse nested unions.
86+
let is_unchecked_union = checker
87+
.semantic
88+
.expr_grandparent()
89+
.and_then(Expr::as_subscript_expr)
90+
.map_or(true, |parent| {
91+
!checker.semantic.match_typing_expr(&parent.value, "Union")
9092
});
9193

9294
if is_unchecked_union {
@@ -96,7 +98,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
9698
if checker.enabled(Rule::DuplicateUnionMember) {
9799
flake8_pyi::rules::duplicate_union_member(checker, expr);
98100
}
99-
if checker.is_stub && checker.enabled(Rule::RedundantLiteralUnion) {
101+
if checker.enabled(Rule::RedundantLiteralUnion) {
100102
flake8_pyi::rules::redundant_literal_union(checker, expr);
101103
}
102104
}
@@ -181,7 +183,9 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
181183
&& checker.semantic.in_annotation()
182184
&& !checker.settings.pyupgrade.keep_runtime_typing
183185
{
184-
flake8_future_annotations::rules::future_rewritable_type_annotation(checker, expr);
186+
flake8_future_annotations::rules::future_rewritable_type_annotation(
187+
checker, expr,
188+
);
185189
}
186190
}
187191
if checker.enabled(Rule::NonPEP585Annotation) {
@@ -387,8 +391,9 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
387391
.enabled(Rule::StringDotFormatExtraPositionalArguments)
388392
{
389393
pyflakes::rules::string_dot_format_extra_positional_arguments(
390-
checker, &summary, args, location,
391-
);
394+
checker,
395+
&summary, args, location,
396+
);
392397
}
393398
if checker.enabled(Rule::StringDotFormatMissingArguments) {
394399
pyflakes::rules::string_dot_format_missing_argument(

crates/ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI051_PYI051.pyi.snap

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
source: crates/ruff/src/rules/flake8_pyi/mod.rs
33
---
4-
PYI051.pyi:4:18: PYI051 `Literal["foo"]` is redundant in an union with `str`
4+
PYI051.pyi:4:18: PYI051 `Literal["foo"]` is redundant in a union with `str`
55
|
66
2 | from typing import Literal, TypeAlias, Union
77
3 |
@@ -11,16 +11,25 @@ PYI051.pyi:4:18: PYI051 `Literal["foo"]` is redundant in an union with `str`
1111
6 | C: TypeAlias = typing.Union[Literal[5], int, typing.Union[Literal["foo"], str]]
1212
|
1313

14-
PYI051.pyi:5:37: PYI051 `Literal[b"bar", b"foo"]` is redundant in an union with `bytes`
14+
PYI051.pyi:5:37: PYI051 `Literal[b"bar"]` is redundant in a union with `bytes`
1515
|
1616
4 | A: str | Literal["foo"]
1717
5 | B: TypeAlias = typing.Union[Literal[b"bar", b"foo"], bytes, str]
18-
| ^^^^^^^^^^^^^^ PYI051
18+
| ^^^^^^ PYI051
1919
6 | C: TypeAlias = typing.Union[Literal[5], int, typing.Union[Literal["foo"], str]]
2020
7 | D: TypeAlias = typing.Union[Literal[b"str_bytes", 42], bytes, int]
2121
|
2222

23-
PYI051.pyi:6:37: PYI051 `Literal[5]` is redundant in an union with `int`
23+
PYI051.pyi:5:45: PYI051 `Literal[b"foo"]` is redundant in a union with `bytes`
24+
|
25+
4 | A: str | Literal["foo"]
26+
5 | B: TypeAlias = typing.Union[Literal[b"bar", b"foo"], bytes, str]
27+
| ^^^^^^ PYI051
28+
6 | C: TypeAlias = typing.Union[Literal[5], int, typing.Union[Literal["foo"], str]]
29+
7 | D: TypeAlias = typing.Union[Literal[b"str_bytes", 42], bytes, int]
30+
|
31+
32+
PYI051.pyi:6:37: PYI051 `Literal[5]` is redundant in a union with `int`
2433
|
2534
4 | A: str | Literal["foo"]
2635
5 | B: TypeAlias = typing.Union[Literal[b"bar", b"foo"], bytes, str]
@@ -29,7 +38,7 @@ PYI051.pyi:6:37: PYI051 `Literal[5]` is redundant in an union with `int`
2938
7 | D: TypeAlias = typing.Union[Literal[b"str_bytes", 42], bytes, int]
3039
|
3140

32-
PYI051.pyi:6:67: PYI051 `Literal["foo"]` is redundant in an union with `str`
41+
PYI051.pyi:6:67: PYI051 `Literal["foo"]` is redundant in a union with `str`
3342
|
3443
4 | A: str | Literal["foo"]
3544
5 | B: TypeAlias = typing.Union[Literal[b"bar", b"foo"], bytes, str]
@@ -38,7 +47,7 @@ PYI051.pyi:6:67: PYI051 `Literal["foo"]` is redundant in an union with `str`
3847
7 | D: TypeAlias = typing.Union[Literal[b"str_bytes", 42], bytes, int]
3948
|
4049

41-
PYI051.pyi:7:37: PYI051 `Literal[b"str_bytes"]` is redundant in an union with `bytes`
50+
PYI051.pyi:7:37: PYI051 `Literal[b"str_bytes"]` is redundant in a union with `bytes`
4251
|
4352
5 | B: TypeAlias = typing.Union[Literal[b"bar", b"foo"], bytes, str]
4453
6 | C: TypeAlias = typing.Union[Literal[5], int, typing.Union[Literal["foo"], str]]
@@ -48,7 +57,7 @@ PYI051.pyi:7:37: PYI051 `Literal[b"str_bytes"]` is redundant in an union with `b
4857
9 | def func(x: complex | Literal[1J], y: Union[Literal[3.14], float]): ...
4958
|
5059

51-
PYI051.pyi:7:51: PYI051 `Literal[42]` is redundant in an union with `int`
60+
PYI051.pyi:7:51: PYI051 `Literal[42]` is redundant in a union with `int`
5261
|
5362
5 | B: TypeAlias = typing.Union[Literal[b"bar", b"foo"], bytes, str]
5463
6 | C: TypeAlias = typing.Union[Literal[5], int, typing.Union[Literal["foo"], str]]
@@ -58,7 +67,7 @@ PYI051.pyi:7:51: PYI051 `Literal[42]` is redundant in an union with `int`
5867
9 | def func(x: complex | Literal[1J], y: Union[Literal[3.14], float]): ...
5968
|
6069

61-
PYI051.pyi:9:31: PYI051 `Literal[1J]` is redundant in an union with `complex`
70+
PYI051.pyi:9:31: PYI051 `Literal[1J]` is redundant in a union with `complex`
6271
|
6372
7 | D: TypeAlias = typing.Union[Literal[b"str_bytes", 42], bytes, int]
6473
8 |
@@ -68,7 +77,7 @@ PYI051.pyi:9:31: PYI051 `Literal[1J]` is redundant in an union with `complex`
6877
11 | # OK
6978
|
7079

71-
PYI051.pyi:9:53: PYI051 `Literal[3.14]` is redundant in an union with `float`
80+
PYI051.pyi:9:53: PYI051 `Literal[3.14]` is redundant in a union with `float`
7281
|
7382
7 | D: TypeAlias = typing.Union[Literal[b"str_bytes", 42], bytes, int]
7483
8 |

crates/ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI051_PYI051.pyi.snap.new

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)