Skip to content
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
24 changes: 24 additions & 0 deletions Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,19 @@ def __repr__(self):
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:
Expand Down Expand Up @@ -987,6 +1000,17 @@ def __repr__(self):
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
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,9 @@ def test_file_not_found_with_bad_cwd(self):
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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Implement dummy ``__class_getitem__`` for ``subprocess.Popen``,
``subprocess.CompletedProcess``