-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mark PYI025 fix as safe in more cases for stub files (#10547)
## Summary The fix for PYI025 is currently marked as unsafe in non-global scopes for both `.py` and `.pyi` files, on the grounds that all global-scope symbols in Python are implicitly exported from the module, so changing the name of something in the global scope could break other modules that import the module we're fixing. Unlike in `.py` files, however, imported symbols are never implicitly re-exported from stub files. Symbols are only understood by static analysis tools as being re-exported from stubs if they are marked as explicit re-exports, which take three forms: ```py from foo import * # all symbols from foo are re-exported from the stub # the "redundant" alias marks it as an explicit re-export # (note that the alias needs to be identical to the symbol's "actual" name # in order for it to be a re-export) from bar import barrr as barrr # inclusion in __all__ also marks it as an explicit re-export, # just like in `.py` files from baz import bazzz __all__ = ["bazzz"] ``` This is [specc'd in PEP 484](https://peps.python.org/pep-0484/#stub-files), and means that we can mark the fix for PYI025 as safe in more cases for `.pyi` files. ## Test Plan `cargo test`. An existing test case goes from being an unsafe fix to a safe fix in a `.pyi` fixture. I also added a new fixture so we have coverage of global-scope imports that are marked as re-exports using "redundant" `from collections.abc import Set as Set` aliases.
- Loading branch information
1 parent
7cc40d5
commit 021f0bd
Showing
7 changed files
with
92 additions
and
20 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI025_3.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
""" | ||
Tests for PYI025 where the import is marked as re-exported | ||
through usage of a "redundant" `import Set as Set` alias | ||
""" | ||
|
||
from collections.abc import Set as Set # PYI025 triggered but fix is not marked as safe |
6 changes: 6 additions & 0 deletions
6
crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI025_3.pyi
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
""" | ||
Tests for PYI025 where the import is marked as re-exported | ||
through usage of a "redundant" `import Set as Set` alias | ||
""" | ||
|
||
from collections.abc import Set as Set # PYI025 triggered but fix is not marked as safe |
This file contains 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
This file contains 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
This file contains 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
18 changes: 18 additions & 0 deletions
18
...rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI025_PYI025_3.py.snap
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI025_3.py:6:36: PYI025 [*] Use `from collections.abc import Set as AbstractSet` to avoid confusion with the `set` builtin | ||
| | ||
4 | """ | ||
5 | | ||
6 | from collections.abc import Set as Set # PYI025 triggered but fix is not marked as safe | ||
| ^^^ PYI025 | ||
| | ||
= help: Alias `Set` to `AbstractSet` | ||
|
||
ℹ Unsafe fix | ||
3 3 | through usage of a "redundant" `import Set as Set` alias | ||
4 4 | """ | ||
5 5 | | ||
6 |-from collections.abc import Set as Set # PYI025 triggered but fix is not marked as safe | ||
6 |+from collections.abc import Set as AbstractSet # PYI025 triggered but fix is not marked as safe |
18 changes: 18 additions & 0 deletions
18
...ules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI025_PYI025_3.pyi.snap
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI025_3.pyi:6:36: PYI025 [*] Use `from collections.abc import Set as AbstractSet` to avoid confusion with the `set` builtin | ||
| | ||
4 | """ | ||
5 | | ||
6 | from collections.abc import Set as Set # PYI025 triggered but fix is not marked as safe | ||
| ^^^ PYI025 | ||
| | ||
= help: Alias `Set` to `AbstractSet` | ||
|
||
ℹ Unsafe fix | ||
3 3 | through usage of a "redundant" `import Set as Set` alias | ||
4 4 | """ | ||
5 5 | | ||
6 |-from collections.abc import Set as Set # PYI025 triggered but fix is not marked as safe | ||
6 |+from collections.abc import Set as AbstractSet # PYI025 triggered but fix is not marked as safe |