sys-exit-alias (PLR1722)
Added in v0.0.156 · Related issues · View source
Derived from the Pylint linter.
Fix is sometimes available.
What it does
Checks for uses of the exit() and quit().
Why is this bad?
exit and quit come from the site module, which is typically imported
automatically during startup. However, it is not guaranteed to be
imported, and so using these functions may result in a NameError at
runtime. Generally, these constants are intended to be used in an interactive
interpreter, and not in programs.
Prefer sys.exit(), as the sys module is guaranteed to exist in all
contexts.
Fix safety
This fix is always unsafe. When replacing exit or quit with sys.exit,
the behavior can change in the following ways:
-
If the code runs in an environment where the
sitemodule is not imported (e.g., withpython -S), the original code would raise aNameError, while the fixed code would execute normally. -
site.exitandsys.exithandle tuple arguments differently.site.exittreats tuples as regular objects and always returns exit code 1, whilesys.exitinterprets tuple contents to determine the exit code: an empty tuple () results in exit code 0, and a single-element tuple like (2,) uses that element's value (2) as the exit code.
Example
Use instead: