Skip to content

Commit 4783c2e

Browse files
authored
Convert some already generic things to GenericAlias to be consistent (python#14)
1 parent 43a97b9 commit 4783c2e

File tree

6 files changed

+23
-39
lines changed

6 files changed

+23
-39
lines changed

Lib/os.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import stat as st
2828

2929
from _collections_abc import _check_methods
30+
from types import GenericAlias
3031

3132
_names = sys.builtin_module_names
3233

@@ -1060,8 +1061,9 @@ def __subclasshook__(cls, subclass):
10601061
return _check_methods(subclass, '__fspath__')
10611062
return NotImplemented
10621063

1063-
def __class_getitem__(cls, type):
1064-
return cls
1064+
def __class_getitem__(cls, item):
1065+
"""Internal: PEP 585."""
1066+
return GenericAlias(cls, item)
10651067

10661068

10671069
if name == 'nt':

Lib/subprocess.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import warnings
5353
import contextlib
5454
from time import monotonic as _time
55+
import types
5556

5657
try:
5758
import pwd
@@ -446,17 +447,9 @@ def __repr__(self):
446447
args.append('stderr={!r}'.format(self.stderr))
447448
return "{}({})".format(type(self).__name__, ', '.join(args))
448449

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
450+
def __class_getitem__(cls, item):
451+
"""Internal: PEP 585."""
452+
return types.GenericAlias(cls, item)
460453

461454

462455
def check_returncode(self):
@@ -1000,16 +993,9 @@ def __repr__(self):
1000993
obj_repr = obj_repr[:76] + "...>"
1001994
return obj_repr
1002995

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
996+
def __class_getitem__(cls, item):
997+
"""Internal: PEP 585."""
998+
return types.GenericAlias(cls, item)
1013999

10141000
@property
10151001
def universal_newlines(self):

Lib/tempfile.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import errno as _errno
4545
from random import Random as _Random
4646
import sys as _sys
47+
import types as _types
4748
import weakref as _weakref
4849
import _thread
4950
_allocate_lock = _thread.allocate_lock
@@ -643,17 +644,9 @@ def __init__(self, max_size=0, mode='w+b', buffering=-1,
643644
'encoding': encoding, 'newline': newline,
644645
'dir': dir, 'errors': errors}
645646

646-
def __class_getitem__(cls, type):
647-
"""Provide minimal support for using this class as generic
648-
(for example in type annotations).
649-
650-
See PEP 484 and PEP 560 for more details. For example,
651-
`SpooledTemporaryFile[str]` is a valid expression at runtime (type
652-
argument `str` indicates whether the file is open in bytes or text
653-
mode). Note, no type checking happens at runtime, but a static type
654-
checker can be used.
655-
"""
656-
return cls
647+
def __class_getitem__(cls, item):
648+
"""Internal: PEP 585."""
649+
return _types.GenericAlias(cls, item)
657650

658651
def _check(self, file):
659652
if self._rolled: return

Lib/test/test_os.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import tempfile
2626
import threading
2727
import time
28+
import types
2829
import unittest
2930
import uuid
3031
import warnings
@@ -4080,7 +4081,7 @@ class A(os.PathLike):
40804081
self.assertTrue(issubclass(FakePath, os.PathLike))
40814082

40824083
def test_pathlike_class_getitem(self):
4083-
self.assertIs(os.PathLike[bytes], os.PathLike)
4084+
self.assertIsInstance(os.PathLike[bytes], types.GenericAlias)
40844085

40854086

40864087
class TimesTests(unittest.TestCase):

Lib/test/test_subprocess.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import tempfile
1212
import time
1313
import traceback
14+
import types
1415
import selectors
1516
import sysconfig
1617
import select
@@ -1436,8 +1437,8 @@ def test_file_not_found_with_bad_cwd(self):
14361437
self.assertEqual(c.exception.filename, '/some/nonexistent/directory')
14371438

14381439
def test_class_getitems(self):
1439-
self.assertIs(subprocess.Popen[bytes], subprocess.Popen)
1440-
self.assertIs(subprocess.CompletedProcess[str], subprocess.CompletedProcess)
1440+
self.assertIsInstance(subprocess.Popen[bytes], types.GenericAlias)
1441+
self.assertIsInstance(subprocess.CompletedProcess[str], types.GenericAlias)
14411442

14421443
class RunFuncTestCase(BaseTestCase):
14431444
def run_python(self, code, **kwargs):

Lib/test/test_tempfile.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import warnings
1111
import contextlib
1212
import stat
13+
import types
1314
import weakref
1415
from unittest import mock
1516

@@ -1230,8 +1231,8 @@ def test_truncate_with_size_parameter(self):
12301231
self.assertEqual(os.fstat(f.fileno()).st_size, 20)
12311232

12321233
def test_class_getitem(self):
1233-
self.assertIs(tempfile.SpooledTemporaryFile[bytes],
1234-
tempfile.SpooledTemporaryFile)
1234+
self.assertIsInstance(tempfile.SpooledTemporaryFile[bytes],
1235+
types.GenericAlias)
12351236

12361237
if tempfile.NamedTemporaryFile is not tempfile.TemporaryFile:
12371238

0 commit comments

Comments
 (0)