Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

brain: Add __class_getitem__ to subprocess.Popen starting from Python 3.9 #882

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ What's New in astroid 2.5.0?
============================
Release Date: TBA

* Add ``__class_getitem__`` method to ``subprocess.Popen`` brain under Python 3.9 so that it is seen as subscriptable by pylint.

Fixes PyCQA/pylint#4034

* Adds `degrees`, `radians`, which are `numpy ufunc`Β functions, in the `numpy`Β brain. Adds `random` function in the `numpy.random`Β brain.

Fixes PyCQA/pylint#3856
Expand Down
7 changes: 7 additions & 0 deletions astroid/brain/brain_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import astroid


PY39 = sys.version_info >= (3, 9)
PY37 = sys.version_info >= (3, 7)
PY36 = sys.version_info >= (3, 6)

Expand Down Expand Up @@ -147,6 +148,12 @@ def kill(self):
"py3_args": py3_args,
}
)
if PY39:
code += """
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we use textwrap.dedent here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hippo91 : I don't think we can. dedent() removes all common leading whitespaces, which means that calling this:

textwrap.dedent(
    """
    def __class_getitem__(cls, item):
        pass
    """
)

... returns this string:

def __class_getitem__(cls, item):
    pass

... which looks like a function, whereas we want the code to be indented to denote a method. Of course we could use textwrap.indent() too, like this:

textwrap.indent(
    textwrap.dedent(
        """
        def __class_getitem__(cls, item):
            pass
        """
    ),
    ' ' * 4,
)

but I am not sure it's more readable. :)

I thus prefer the current version. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dbaty you are totally right!
Thanks for this explanation and this PR!

@classmethod
def __class_getitem__(cls, item):
pass
"""

init_lines = textwrap.dedent(init).splitlines()
indented_init = "\n".join(" " * 4 + line for line in init_lines)
Expand Down
7 changes: 7 additions & 0 deletions tests/unittest_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,13 @@ def test_subprcess_check_output(self):
assert isinstance(inferred, astroid.Const)
assert isinstance(inferred.value, (str, bytes))

@test_utils.require_version("3.9")
def test_popen_does_not_have_class_getitem(self):
code = """import subprocess; subprocess.Popen"""
node = astroid.extract_node(code)
inferred = next(node.infer())
assert "__class_getitem__" in inferred


class TestIsinstanceInference:
"""Test isinstance builtin inference"""
Expand Down