Skip to content

[pylint] add fix safety section (PLR1722)#17826

Merged
ntBre merged 2 commits intoastral-sh:mainfrom
yunchipang:docs/fix-safety-sys-exit-alias
May 5, 2025
Merged

[pylint] add fix safety section (PLR1722)#17826
ntBre merged 2 commits intoastral-sh:mainfrom
yunchipang:docs/fix-safety-sys-exit-alias

Conversation

@yunchipang
Copy link
Contributor

parent: #15584
fix introduced at: #816

@yunchipang yunchipang mentioned this pull request May 4, 2025
71 tasks
@AlexWaygood AlexWaygood added the documentation Improvements or additions to documentation label May 4, 2025
@github-actions
Copy link
Contributor

github-actions bot commented May 4, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@VascoSch92
Copy link
Contributor

I think the fix is always unsafe...

@yunchipang
Copy link
Contributor Author

@VascoSch92 Thanks for the input! I’m new to the project—could you elaborate a bit? I was referencing this:

const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes;

@ntBre
Copy link
Contributor

ntBre commented May 5, 2025

Fix availability is separate from fix safety. In some cases we might offer a diagnostic without a fix at all, regardless of safety. For this rule, that can happen if we fail to generate the new import edit, for example.

There's not really a centralized safety marker, you generally just have to search for Fix:: or safe/unsafe in the file (at least that's what I do). If all of the places where a Fix is constructed use something like Fix::unsafe_edit, then you know it's always unsafe.

Thanks for working on this!

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.

Related to my other comment, this is more of a description of the fix's availability rather than its safety. It's not immediately clear to me why this fix is unsafe, which is one of the challenging parts of these cases. You may need to look back at the original PR adding the rule for more discussion.

@dscorbett
Copy link

The rule description says “exit and quit come from the site module, which is typically imported automatically during startup. However, it is not guaranteed to be imported”. If it’s not imported, the fix can change behavior.

$ cat >plr1722_1.py <<'# EOF'
exit(2)
# EOF

$ python -S plr1722_1.py; echo $?
Traceback (most recent call last):
  File "plr1722_1.py", line 1, in <module>
    exit()
    ^^^^
NameError: name 'exit' is not defined
1

$ ruff --isolated check plr1722_1.py --select PLR1722 --unsafe-fixes --fix
Found 1 error (1 fixed, 0 remaining).

$ python -S plr1722_1.py; echo $?
2

Another difference between site.exit and sys.exit is how they treat 0- and 1-element tuples. (This is undocumented and is arguably a bug in sys.exit.)

$ cat >plr1722_2.py <<'# EOF'
exit(())
# EOF

$ cat >plr1722_3.py <<'# EOF'
exit((2,))
# EOF

$ python plr1722_2.py; echo $?
()
1

$ python plr1722_3.py; echo $?
(2,)
1

$ ruff --isolated check plr1722_{2,3}.py --select PLR1722 --unsafe-fixes --fix
Found 2 errors (2 fixed, 0 remaining).

$ python plr1722_2.py; echo $?
0

$ python plr1722_3.py; echo $?
2

@yunchipang yunchipang requested a review from ntBre May 5, 2025 18:07
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! This looks good to me.

@ntBre ntBre merged commit a4c8e43 into astral-sh:main May 5, 2025
34 checks passed
Glyphack pushed a commit to Glyphack/ruff that referenced this pull request May 6, 2025
@yunchipang yunchipang deleted the docs/fix-safety-sys-exit-alias branch June 7, 2025 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants