Skip to content

Commit

Permalink
brain: Add __class_getitem__ to subprocess.Popen starting from Py…
Browse files Browse the repository at this point in the history
…thon 3.9

This is necessary for pylint to detect that `subprocess.Popen` is
subscriptable, starting from Python 3.9 (see pylint-dev/pylint#4034).

    $ python3.9
    >>> import subprocess
    >>> subprocess.Popen.__class_getitem__
    <bound method GenericAlias of <class 'subprocess.Popen'>>
  • Loading branch information
dbaty committed Jan 18, 2021
1 parent c9fd193 commit 294623e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
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 += """
@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

0 comments on commit 294623e

Please sign in to comment.