Skip to content

Commit 0e0dd9c

Browse files
[Backport maintenance/2.15.x] Make sys.argv uninferable because it never is (#2244) (#2297)
* Make `sys.argv` uninferable because it never is (#2244) It's impossible to infer the value it will have outside of static analysis where it's our own value. See pylint-dev/pylint#7710 (cherry picked from commit ea78827) Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
1 parent 5cfbcbf commit 0e0dd9c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

astroid/inference.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,12 @@ def infer_attribute(
369369
context.constraints[self.attrname] = constraint.get_constraints(
370370
self, frame=frame
371371
)
372-
yield from owner.igetattr(self.attrname, context)
372+
if self.attrname == "argv" and owner.name == "sys":
373+
# sys.argv will never be inferable during static analysis
374+
# It's value would be the args passed to the linter itself
375+
yield util.Uninferable
376+
else:
377+
yield from owner.igetattr(self.attrname, context)
373378
except (
374379
AttributeInferenceError,
375380
InferenceError,

tests/test_inference.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7102,3 +7102,18 @@ def test_old_style_string_formatting_with_specs(self) -> None:
71027102
inferred = next(node.infer())
71037103
assert isinstance(inferred, nodes.Const)
71047104
assert inferred.value == "My name is Daniel, I'm 12.00"
7105+
7106+
7107+
def test_sys_argv_uninferable() -> None:
7108+
"""Regression test for https://github.com/pylint-dev/pylint/issues/7710."""
7109+
a: nodes.List = extract_node(
7110+
textwrap.dedent(
7111+
"""
7112+
import sys
7113+
7114+
sys.argv"""
7115+
)
7116+
)
7117+
sys_argv_value = list(a._infer())
7118+
assert len(sys_argv_value) == 1
7119+
assert sys_argv_value[0] is util.Uninferable

0 commit comments

Comments
 (0)