Skip to content

Commit ea78827

Browse files
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
1 parent 989a89d commit ea78827

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

astroid/nodes/node_classes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,12 @@ def _infer_attribute(
10991099
if isinstance(owner, (ClassDef, Instance)):
11001100
frame = owner if isinstance(owner, ClassDef) else owner._proxied
11011101
context.constraints[node.attrname] = get_constraints(node, frame=frame)
1102-
yield from owner.igetattr(node.attrname, context)
1102+
if node.attrname == "argv" and owner.name == "sys":
1103+
# sys.argv will never be inferable during static analysis
1104+
# It's value would be the args passed to the linter itself
1105+
yield util.Uninferable
1106+
else:
1107+
yield from owner.igetattr(node.attrname, context)
11031108
except (
11041109
AttributeInferenceError,
11051110
InferenceError,

tests/test_inference.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7238,3 +7238,18 @@ def test_old_style_string_formatting_with_specs(self) -> None:
72387238
inferred = next(node.infer())
72397239
assert isinstance(inferred, nodes.Const)
72407240
assert inferred.value == "My name is Daniel, I'm 12.00"
7241+
7242+
7243+
def test_sys_argv_uninferable() -> None:
7244+
"""Regression test for https://github.com/pylint-dev/pylint/issues/7710."""
7245+
a: nodes.List = extract_node(
7246+
textwrap.dedent(
7247+
"""
7248+
import sys
7249+
7250+
sys.argv"""
7251+
)
7252+
)
7253+
sys_argv_value = list(a._infer())
7254+
assert len(sys_argv_value) == 1
7255+
assert sys_argv_value[0] is Uninferable

0 commit comments

Comments
 (0)