summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBatuhan Taşkaya <47358913+isidentical@users.noreply.github.com>2019-12-30 16:02:04 (GMT)
committerIvan Levkivskyi <levkivskyi@gmail.com>2019-12-30 16:02:04 (GMT)
commit4dc5a9df59837446ec1dc5b7a0e6ce95ae5b5cec (patch)
tree281d8268ce597e285384a72ab9307237a85d0b26 /Lib
parent89aa7f0ede1a11c020e83f24394593c577a61509 (diff)
downloadcpython-4dc5a9df59837446ec1dc5b7a0e6ce95ae5b5cec.zip
cpython-4dc5a9df59837446ec1dc5b7a0e6ce95ae5b5cec.tar.gz
cpython-4dc5a9df59837446ec1dc5b7a0e6ce95ae5b5cec.tar.bz2
bpo-39019: Implement missing __class_getitem__ for subprocess classes (GH-17558)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/subprocess.py24
-rw-r--r--Lib/test/test_subprocess.py3
2 files changed, 27 insertions, 0 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index ba6f198..30f0d1b 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -446,6 +446,19 @@ class CompletedProcess(object):
args.append('stderr={!r}'.format(self.stderr))
return "{}({})".format(type(self).__name__, ', '.join(args))
+ def __class_getitem__(cls, type):
+ """Provide minimal support for using this class as generic
+ (for example in type annotations).
+
+ See PEP 484 and PEP 560 for more details. For example,
+ `CompletedProcess[bytes]` is a valid expression at runtime
+ (type argument `bytes` indicates the type used for stdout).
+ Note, no type checking happens at runtime, but a static type
+ checker can be used.
+ """
+ return cls
+
+
def check_returncode(self):
"""Raise CalledProcessError if the exit code is non-zero."""
if self.returncode:
@@ -987,6 +1000,17 @@ class Popen(object):
obj_repr = obj_repr[:76] + "...>"
return obj_repr
+ def __class_getitem__(cls, type):
+ """Provide minimal support for using this class as generic
+ (for example in type annotations).
+
+ See PEP 484 and PEP 560 for more details. For example, `Popen[bytes]`
+ is a valid expression at runtime (type argument `bytes` indicates the
+ type used for stdout). Note, no type checking happens at runtime, but
+ a static type checker can be used.
+ """
+ return cls
+
@property
def universal_newlines(self):
# universal_newlines as retained as an alias of text_mode for API
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py
index 87322c6..2073fd1 100644
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -1435,6 +1435,9 @@ class ProcessTestCase(BaseTestCase):
subprocess.Popen(['exit', '0'], cwd='/some/nonexistent/directory')
self.assertEqual(c.exception.filename, '/some/nonexistent/directory')
+ def test_class_getitems(self):
+ self.assertIs(subprocess.Popen[bytes], subprocess.Popen)
+ self.assertIs(subprocess.CompletedProcess[str], subprocess.CompletedProcess)
class RunFuncTestCase(BaseTestCase):
def run_python(self, code, **kwargs):