Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark PYI025 fix as safe in more cases for stub files #10547

Merged
merged 1 commit into from Mar 24, 2024

Conversation

AlexWaygood
Copy link
Member

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:

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, 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.

@AlexWaygood
Copy link
Member Author

Fun fact: I wrote a version of this PR on Thursday, but bumped into #10508 while writing tests for it. That then meant I had to do #10525 and #10527 first, before I could file this 😄

Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@AlexWaygood AlexWaygood added the fixes Related to suggested fixes for violations label Mar 24, 2024
@AlexWaygood AlexWaygood merged commit 021f0bd into main Mar 24, 2024
17 checks passed
@AlexWaygood AlexWaygood deleted the safe-AbstractSet-rename branch March 24, 2024 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants