From afd9a45d8d3f123f0efe9b2bd1035d1a4705261b Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Sat, 23 Sep 2023 19:00:19 +0200 Subject: [PATCH] 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 https://github.com/pylint-dev/pylint/issues/7710 (cherry picked from commit ea78827c9a812be0bdcfce3bf760f5656733f8ad) --- astroid/inference.py | 7 ++++++- tests/test_inference.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/astroid/inference.py b/astroid/inference.py index 65d03d3021..05eb13434a 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -369,7 +369,12 @@ def infer_attribute( context.constraints[self.attrname] = constraint.get_constraints( self, frame=frame ) - yield from owner.igetattr(self.attrname, context) + if self.attrname == "argv" and owner.name == "sys": + # sys.argv will never be inferable during static analysis + # It's value would be the args passed to the linter itself + yield util.Uninferable + else: + yield from owner.igetattr(self.attrname, context) except ( AttributeInferenceError, InferenceError, diff --git a/tests/test_inference.py b/tests/test_inference.py index 86fdbcf4ae..ce4c80cdc1 100644 --- a/tests/test_inference.py +++ b/tests/test_inference.py @@ -7102,3 +7102,18 @@ def test_old_style_string_formatting_with_specs(self) -> None: inferred = next(node.infer()) assert isinstance(inferred, nodes.Const) assert inferred.value == "My name is Daniel, I'm 12.00" + + +def test_sys_argv_uninferable() -> None: + """Regression test for https://github.com/pylint-dev/pylint/issues/7710.""" + a: nodes.List = extract_node( + textwrap.dedent( + """ + import sys + + sys.argv""" + ) + ) + sys_argv_value = list(a._infer()) + assert len(sys_argv_value) == 1 + assert sys_argv_value[0] is Uninferable