]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39019: Implement missing __class_getitem__ for subprocess classes (GH-17558)
authorBatuhan Taşkaya <47358913+isidentical@users.noreply.github.com>
Mon, 30 Dec 2019 16:02:04 +0000 (19:02 +0300)
committerIvan Levkivskyi <levkivskyi@gmail.com>
Mon, 30 Dec 2019 16:02:04 +0000 (16:02 +0000)
Lib/subprocess.py
Lib/test/test_subprocess.py
Misc/NEWS.d/next/Library/2019-12-10-21-03-34.bpo-39019.i8RpMZ.rst [new file with mode: 0644]

index ba6f1983a5a2273f7ad654b710b27a6ed1557ec1..30f0d1be154c4031bc259e82334b84d14242f32e 100644 (file)
@@ -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
index 87322c6406bd553ca6fa5f1d4e86be53bcb2965d..2073fd146177afd6286191e66601e7969675cd8d 100644 (file)
@@ -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):
diff --git a/Misc/NEWS.d/next/Library/2019-12-10-21-03-34.bpo-39019.i8RpMZ.rst b/Misc/NEWS.d/next/Library/2019-12-10-21-03-34.bpo-39019.i8RpMZ.rst
new file mode 100644 (file)
index 0000000..b64a56e
--- /dev/null
@@ -0,0 +1,2 @@
+Implement dummy ``__class_getitem__`` for ``subprocess.Popen``,
+``subprocess.CompletedProcess``