bpo-39019: Implement missing __class_getitem__ for subprocess classes (GH-17558)

This commit is contained in:
Batuhan Taşkaya 2019-12-30 19:02:04 +03:00 committed by Ivan Levkivskyi
parent 89aa7f0ede
commit 4dc5a9df59
3 changed files with 29 additions and 0 deletions

View File

@ -446,6 +446,19 @@ class CompletedProcess(object):
args.append('stderr={!r}'.format(self.stderr)) args.append('stderr={!r}'.format(self.stderr))
return "{}({})".format(type(self).__name__, ', '.join(args)) 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): def check_returncode(self):
"""Raise CalledProcessError if the exit code is non-zero.""" """Raise CalledProcessError if the exit code is non-zero."""
if self.returncode: if self.returncode:
@ -987,6 +1000,17 @@ class Popen(object):
obj_repr = obj_repr[:76] + "...>" obj_repr = obj_repr[:76] + "...>"
return obj_repr 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 @property
def universal_newlines(self): def universal_newlines(self):
# universal_newlines as retained as an alias of text_mode for API # universal_newlines as retained as an alias of text_mode for API

View File

@ -1435,6 +1435,9 @@ class ProcessTestCase(BaseTestCase):
subprocess.Popen(['exit', '0'], cwd='/some/nonexistent/directory') subprocess.Popen(['exit', '0'], cwd='/some/nonexistent/directory')
self.assertEqual(c.exception.filename, '/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): class RunFuncTestCase(BaseTestCase):
def run_python(self, code, **kwargs): def run_python(self, code, **kwargs):

View File

@ -0,0 +1,2 @@
Implement dummy ``__class_getitem__`` for ``subprocess.Popen``,
``subprocess.CompletedProcess``