Skip to content

Comments

[flake8-pie] Avoid false positive for multiple assignment with auto() (PIE796)#17274

Merged
ntBre merged 2 commits intoastral-sh:mainfrom
twentyone212121:fix-issue-16868
Apr 8, 2025
Merged

[flake8-pie] Avoid false positive for multiple assignment with auto() (PIE796)#17274
ntBre merged 2 commits intoastral-sh:mainfrom
twentyone212121:fix-issue-16868

Conversation

@twentyone212121
Copy link
Contributor

This fix closes #16868

I noticed the issue is assigned, but the assignee appears to be actively working on another pull request. I hope that’s okay!

Summary

As of Python 3.11.1, enum.auto() can be used in multiple assignments. This pattern should not trigger non-unique-enums check.
Reference: Python docs on enum.auto()

This fix updates the check logic to skip enum variant statements where the right-hand side is a tuple containing a call to enum.auto().

Test Plan

The added test case uses the example from the original issue. It previously triggered a false positive, but now passes successfully.

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! A couple of stylistic suggestions, but this looks good to me.

Comment on lines 105 to 112
if let Expr::Call(ast::ExprCall { func, .. }) = expr {
checker
.semantic()
.resolve_qualified_name(func)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["enum", "auto"]))
} else {
false
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is slightly neater, maybe:

Suggested change
if let Expr::Call(ast::ExprCall { func, .. }) = expr {
checker
.semantic()
.resolve_qualified_name(func)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["enum", "auto"]))
} else {
false
}
expr.as_call_expr().map_or(false, |call| {
checker
.semantic()
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["enum", "auto"]))
})

We could also consider passing only the SemanticModel into this function instead of the whole Checker, but I don't feel too strongly about that.

Copy link
Contributor Author

@twentyone212121 twentyone212121 Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! I updated the code in the latest commit.
I also replaced Checker with SemanticModel in helper functions because:

  1. They only need the SemanticModel.
  2. We already have a semantic variable on the call site.
  3. Since Checker can be mutated through shared references, keeping its usage minimal felt like a safer option.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 7, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+2 -2 violations, +0 -0 fixes in 1 projects; 54 projects unchanged)

apache/airflow (+2 -2 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL

+ performance/src/performance_dags/performance_dag/performance_dag.py:95:13: AIR302 Import path `airflow.operators.bash` is moved into `standard` provider in Airflow 3.0;
- performance/src/performance_dags/performance_dag/performance_dag.py:95:13: AIR302 `airflow.operators.bash.BashOperator` is moved into `standard` provider in Airflow 3.0;
+ providers/edge/src/airflow/providers/edge/example_dags/integration_test.py:116:24: AIR302 Import path `airflow.operators.bash` is moved into `standard` provider in Airflow 3.0;
- providers/edge/src/airflow/providers/edge/example_dags/integration_test.py:116:24: AIR302 `airflow.operators.bash.BashOperator` is moved into `standard` provider in Airflow 3.0;

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
AIR302 4 2 2 0 0

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, thanks!

@ntBre ntBre changed the title [flake8-pie] Avoid false positive for multiple assignment with auto() (PIE796) [flake8-pie] Avoid false positive for multiple assignment with auto() (PIE796) Apr 8, 2025
@ntBre ntBre merged commit ed14dbb into astral-sh:main Apr 8, 2025
22 checks passed
dcreager added a commit that referenced this pull request Apr 9, 2025
* origin/main:
  [red-knot] Default `python-platform` to current platform (#17183)
  [red-knot] Add new 'unreachable code' test case (#17306)
  [red-knot] mypy_primer: Run on `async-utils` (#17303)
  [red-knot] Add custom `__setattr__` support (#16748)
  [red-knot] Add `__init__` arguments check when doing `try_call` on a class literal (#16512)
  [`flake8-pie`] Avoid false positive for multiple assignment with `auto()` (`PIE796`) (#17274)
  [syntax-errors] Async comprehension in sync comprehension (#17177)
  [`airflow`] Expand module path check to individual symbols (`AIR302`) (#17278)
  [syntax-errors] Check annotations in annotated assignments (#17283)
  [syntax-errors] Extend annotation checks to `await` (#17282)
  [red-knot] Add support for `assert_never` (#17287)
  [`flake8-pytest-style`] Avoid false positive for legacy form of `pytest.raises` (`PT011`) (#17231)
  [red-knot] Do not show types for literal expressions on hover (#17290)
  [red-knot] Fix dead-code clippy warning (#17291)
  [red-knot] Reachability analysis (#17199)
  [red-knot] Don't use latency-sensitive for handlers (#17227)
dcreager added a commit that referenced this pull request Apr 9, 2025
* dcreager/special-class: (26 commits)
  lint
  Add TODO about property test data
  Better todos
  Narrow type(generic) better
  More Python-like displays for specializations
  Add xfail for generic method inside generic class
  Comment other non-specializations
  Explain self_instance not being specialized
  Generic aliases are literals in type display
  Better TODO fallback type
  [red-knot] Default `python-platform` to current platform (#17183)
  [red-knot] Add new 'unreachable code' test case (#17306)
  [red-knot] mypy_primer: Run on `async-utils` (#17303)
  [red-knot] Add custom `__setattr__` support (#16748)
  [red-knot] Add `__init__` arguments check when doing `try_call` on a class literal (#16512)
  [`flake8-pie`] Avoid false positive for multiple assignment with `auto()` (`PIE796`) (#17274)
  [syntax-errors] Async comprehension in sync comprehension (#17177)
  [`airflow`] Expand module path check to individual symbols (`AIR302`) (#17278)
  [syntax-errors] Check annotations in annotated assignments (#17283)
  [syntax-errors] Extend annotation checks to `await` (#17282)
  ...
Glyphack pushed a commit to Glyphack/ruff that referenced this pull request Apr 9, 2025
…o()` (`PIE796`) (astral-sh#17274)

This fix closes astral-sh#16868 

I noticed the issue is assigned, but the assignee appears to be actively
working on another pull request. I hope that’s okay!

<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->

As of Python 3.11.1, `enum.auto()` can be used in multiple assignments.
This pattern should not trigger non-unique-enums check.
Reference: [Python docs on
enum.auto()](https://docs.python.org/3/library/enum.html#enum.auto)

This fix updates the check logic to skip enum variant statements where
the right-hand side is a tuple containing a call to `enum.auto()`.

## Test Plan

<!-- How was it tested? -->

The added test case uses the example from the original issue. It
previously triggered a false positive, but now passes successfully.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

non-unique-enums (PIE796): false positive

2 participants