Skip to content

Commit

Permalink
[Backport maintenance/2.15.x] Make sys.argv uninferable because it …
Browse files Browse the repository at this point in the history
…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>
  • Loading branch information
jacobtylerwalls authored Sep 23, 2023
1 parent 5cfbcbf commit 0e0dd9c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion astroid/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions tests/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 util.Uninferable

0 comments on commit 0e0dd9c

Please sign in to comment.