Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 1, 2023
1 parent 0b98920 commit 78941a3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 111 deletions.
25 changes: 14 additions & 11 deletions crates/ruff/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
Rule::DuplicateUnionMember,
Rule::RedundantLiteralUnion,
]) {
// Determine if the current expression is an union
// To avoid duplicate checks, we examine whether current `expr` is an `Union[...]`
// without a parent, since these rules traverse nested unions
let is_unchecked_union = checker.semantic.expr_parent().is_none()
&& expr.as_subscript_expr().map_or(false, |subscript| {
checker
.semantic
.match_typing_expr(&subscript.value, "Union")
// Avoid duplicate checks if the parent is an `Union[...]` since these rules
// traverse nested unions.
let is_unchecked_union = checker
.semantic
.expr_grandparent()
.and_then(Expr::as_subscript_expr)
.map_or(true, |parent| {
!checker.semantic.match_typing_expr(&parent.value, "Union")
});

if is_unchecked_union {
Expand Down Expand Up @@ -181,7 +181,9 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
&& checker.semantic.in_annotation()
&& !checker.settings.pyupgrade.keep_runtime_typing
{
flake8_future_annotations::rules::future_rewritable_type_annotation(checker, expr);
flake8_future_annotations::rules::future_rewritable_type_annotation(
checker, expr,
);
}
}
if checker.enabled(Rule::NonPEP585Annotation) {
Expand Down Expand Up @@ -387,8 +389,9 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
.enabled(Rule::StringDotFormatExtraPositionalArguments)
{
pyflakes::rules::string_dot_format_extra_positional_arguments(
checker, &summary, args, location,
);
checker,
&summary, args, location,
);
}
if checker.enabled(Rule::StringDotFormatMissingArguments) {
pyflakes::rules::string_dot_format_missing_argument(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff/src/rules/flake8_pyi/mod.rs
---
PYI051.pyi:4:18: PYI051 `Literal["foo"]` is redundant in an union with `str`
PYI051.pyi:4:18: PYI051 `Literal["foo"]` is redundant in a union with `str`
|
2 | from typing import Literal, TypeAlias, Union
3 |
Expand All @@ -11,16 +11,25 @@ PYI051.pyi:4:18: PYI051 `Literal["foo"]` is redundant in an union with `str`
6 | C: TypeAlias = typing.Union[Literal[5], int, typing.Union[Literal["foo"], str]]
|

PYI051.pyi:5:37: PYI051 `Literal[b"bar", b"foo"]` is redundant in an union with `bytes`
PYI051.pyi:5:37: PYI051 `Literal[b"bar"]` is redundant in a union with `bytes`
|
4 | A: str | Literal["foo"]
5 | B: TypeAlias = typing.Union[Literal[b"bar", b"foo"], bytes, str]
| ^^^^^^^^^^^^^^ PYI051
| ^^^^^^ PYI051
6 | C: TypeAlias = typing.Union[Literal[5], int, typing.Union[Literal["foo"], str]]
7 | D: TypeAlias = typing.Union[Literal[b"str_bytes", 42], bytes, int]
|

PYI051.pyi:6:37: PYI051 `Literal[5]` is redundant in an union with `int`
PYI051.pyi:5:45: PYI051 `Literal[b"foo"]` is redundant in a union with `bytes`
|
4 | A: str | Literal["foo"]
5 | B: TypeAlias = typing.Union[Literal[b"bar", b"foo"], bytes, str]
| ^^^^^^ PYI051
6 | C: TypeAlias = typing.Union[Literal[5], int, typing.Union[Literal["foo"], str]]
7 | D: TypeAlias = typing.Union[Literal[b"str_bytes", 42], bytes, int]
|

PYI051.pyi:6:37: PYI051 `Literal[5]` is redundant in a union with `int`
|
4 | A: str | Literal["foo"]
5 | B: TypeAlias = typing.Union[Literal[b"bar", b"foo"], bytes, str]
Expand All @@ -29,7 +38,7 @@ PYI051.pyi:6:37: PYI051 `Literal[5]` is redundant in an union with `int`
7 | D: TypeAlias = typing.Union[Literal[b"str_bytes", 42], bytes, int]
|

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

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

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

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

PYI051.pyi:9:53: PYI051 `Literal[3.14]` is redundant in an union with `float`
PYI051.pyi:9:53: PYI051 `Literal[3.14]` is redundant in a union with `float`
|
7 | D: TypeAlias = typing.Union[Literal[b"str_bytes", 42], bytes, int]
8 |
Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ fn handle_arguments_separator_comment<'a>(
comment_range,
comment.line_position(),
);
println!("placement: {:?}", placement);
if placement.is_some() {
return CommentPlacement::dangling(comment.enclosing_node(), comment);
}
Expand Down

0 comments on commit 78941a3

Please sign in to comment.