Skip to content

Commit 4dc5a9d

Browse files
isidenticalilevkivskyi
authored andcommitted
bpo-39019: Implement missing __class_getitem__ for subprocess classes (GH-17558)
1 parent 89aa7f0 commit 4dc5a9d

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

Lib/subprocess.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,19 @@ def __repr__(self):
446446
args.append('stderr={!r}'.format(self.stderr))
447447
return "{}({})".format(type(self).__name__, ', '.join(args))
448448

449+
def __class_getitem__(cls, type):
450+
"""Provide minimal support for using this class as generic
451+
(for example in type annotations).
452+
453+
See PEP 484 and PEP 560 for more details. For example,
454+
`CompletedProcess[bytes]` is a valid expression at runtime
455+
(type argument `bytes` indicates the type used for stdout).
456+
Note, no type checking happens at runtime, but a static type
457+
checker can be used.
458+
"""
459+
return cls
460+
461+
449462
def check_returncode(self):
450463
"""Raise CalledProcessError if the exit code is non-zero."""
451464
if self.returncode:
@@ -987,6 +1000,17 @@ def __repr__(self):
9871000
obj_repr = obj_repr[:76] + "...>"
9881001
return obj_repr
9891002

1003+
def __class_getitem__(cls, type):
1004+
"""Provide minimal support for using this class as generic
1005+
(for example in type annotations).
1006+
1007+
See PEP 484 and PEP 560 for more details. For example, `Popen[bytes]`
1008+
is a valid expression at runtime (type argument `bytes` indicates the
1009+
type used for stdout). Note, no type checking happens at runtime, but
1010+
a static type checker can be used.
1011+
"""
1012+
return cls
1013+
9901014
@property
9911015
def universal_newlines(self):
9921016
# universal_newlines as retained as an alias of text_mode for API

Lib/test/test_subprocess.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,9 @@ def test_file_not_found_with_bad_cwd(self):
14351435
subprocess.Popen(['exit', '0'], cwd='/some/nonexistent/directory')
14361436
self.assertEqual(c.exception.filename, '/some/nonexistent/directory')
14371437

1438+
def test_class_getitems(self):
1439+
self.assertIs(subprocess.Popen[bytes], subprocess.Popen)
1440+
self.assertIs(subprocess.CompletedProcess[str], subprocess.CompletedProcess)
14381441

14391442
class RunFuncTestCase(BaseTestCase):
14401443
def run_python(self, code, **kwargs):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Implement dummy ``__class_getitem__`` for ``subprocess.Popen``,
2+
``subprocess.CompletedProcess``

0 commit comments

Comments
 (0)