Skip to content

Commit

Permalink
argparse brain: avoid spurious addition of "namespace" to function …
Browse files Browse the repository at this point in the history
…locals
  • Loading branch information
jacobtylerwalls authored and Pierre-Sassoulas committed Jun 13, 2022
1 parent b30977e commit 824abdb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Release date: TBA
* The Qt brain now correctly treats calling ``.disconnect()`` (with no
arguments) on a slot as valid.

* The argparse brain no longer incorrectly adds ``"Namespace"`` to the locals
of functions that return an ``argparse.Namespace`` object.

Refs PyCQA/pylint#6895

What's New in astroid 2.11.5?
=============================
Expand Down
5 changes: 4 additions & 1 deletion astroid/brain/brain_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def infer_namespace(node, context=None):
# Cannot make sense of it.
raise UseInferenceDefault()

class_node = nodes.ClassDef("Namespace", parent=node.parent)
class_node = nodes.ClassDef("Namespace")
# Set parent manually until ClassDef constructor fixed:
# https://github.com/PyCQA/astroid/issues/1490
class_node.parent = node.parent
for attr in set(callsite.keyword_arguments):
fake_node = nodes.EmptyNode()
fake_node.parent = class_node
Expand Down
21 changes: 21 additions & 0 deletions tests/unittest_brain_argparse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt

from astroid import bases, extract_node, nodes


class TestBrainArgparse:
@staticmethod
def test_infer_namespace() -> None:
func = extract_node(
"""
import argparse
def make_namespace(): #@
return argparse.Namespace(debug=True)
"""
)
assert isinstance(func, nodes.FunctionDef)
inferred = next(func.infer_call_result(func))
assert isinstance(inferred, bases.Instance)
assert not func.locals

0 comments on commit 824abdb

Please sign in to comment.