pylint
pylint copied to clipboard
using-constant-test triggers on a built-in exception property
Bug description
The using-constant-test warning shows up on conditional use of SystemExit's code property. This appears similar to, but not the same as https://github.com/pylint-dev/pylint/issues/3484.
Given the following code, run pylint on it. Note the comment.
test_pylint.py
import sys
def test_invalid_W0125():
"""
This test demonstrates a false positive for W0640.
Message emitted:
Using a conditional statement with a constant value
Description:
Emitted when a conditional statement (If or ternary if) uses a constant
value for its test. This might not be what the user intended to do.
"""
for i in range(5):
try:
sys.exit(i)
except SystemExit as e:
# pylint incorrectly thinks that the following conditional uses
# a constant value
if e.code:
print(f"sys.exit() called with argument: {e.code}")
assert e.code == i
Configuration
No response
Command used
pylint test_pylint.py
Pylint output
************* Module test
test.py:23: [W0125(using-constant-test), test_invalid_W0125] Using a conditional statement with a constant value
------------------------------------------------------------------
Your code has been rated at 8.89/10 (previous run: 5.62/10, +3.26)
Expected behavior
No error
Pylint version
pylint 3.0.3
astroid 3.0.2
Python 3.12.2 (main, Apr 2 2024, 18:40:52) [GCC 13.2.0]
OS / Environment
Ubuntu 24.04
Additional dependencies
No response