[flake8-pyi] Fix false positive for PEP 695 generic __exit__ methods (PYI036)#26355
Closed
kpeis695 wants to merge 2 commits into
Closed
[flake8-pyi] Fix false positive for PEP 695 generic __exit__ methods (PYI036)#26355kpeis695 wants to merge 2 commits into
__exit__ methods (PYI036)#26355kpeis695 wants to merge 2 commits into
Conversation
…T003`) Fixes astral-sh#17226. When a `Depends(...)` call is pre-assigned to a variable and that variable is used as metadata in an `Annotated` annotation, FAST003 incorrectly fires a false positive. The rule scans `Annotated` metadata elements for literal `Depends(...)` calls; a bare name like `FindItem` is not a call expression, so `depends_arguments` returns `None`, the element is silently skipped, and the path parameter appears unused. The fix treats a bare-name metadata element as `Dependency::Unknown` (unless it clearly resolves to a function, class, or import), which causes the check to abort conservatively rather than emitting a false positive.
…ods (`PYI036`) Fixes astral-sh#25905. When `__exit__` uses a PEP 695 type parameter bound to `BaseException` (e.g. `def __exit__[T: BaseException](...)`), annotations like `type[T] | None` and `T | None` are valid but PYI036 incorrectly flags them as bad. The rule's validators only matched `type[BaseException]` and `BaseException` literally. The fix passes the function's type params into the validators so they can recognise `type[T]` and `T` when `T` is a type param whose bound is `BaseException`. Type params with no bound (or a bound other than `BaseException`) continue to be flagged as before.
__exit__ methods (PYI036)__exit__ methods (PYI036)
Author
|
Closing to reopen from a clean branch (only contains the PYI036 fix, no unrelated commits). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #25905.
When
__exit__or__aexit__uses a PEP 695 type parameter bound toBaseException, annotations liketype[T] | NoneandT | Noneare valid butPYI036incorrectly flags them:The validators
is_base_exception_typeand the second-arg check only matchedtype[BaseException]andBaseExceptionliterally. The fix passes the function's PEP 695 type params into the validators sotype[T]andTare accepted whenTis a type parameter whose bound isBaseException.Type parameters with no bound (or a bound other than
BaseException) continue to be flagged as before.Test Plan
Snapshot tests (
cargo nextest runandcargo insta test).