From 4783c2e8a92fa8e73b8a770f3af8ec43626a23fd Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Mon, 24 Feb 2020 21:06:23 -0800 Subject: [PATCH] Convert some already generic things to GenericAlias to be consistent (#14) --- Lib/os.py | 6 ++++-- Lib/subprocess.py | 28 +++++++--------------------- Lib/tempfile.py | 15 ++++----------- Lib/test/test_os.py | 3 ++- Lib/test/test_subprocess.py | 5 +++-- Lib/test/test_tempfile.py | 5 +++-- 6 files changed, 23 insertions(+), 39 deletions(-) diff --git a/Lib/os.py b/Lib/os.py index ab75b94d4fe45f..2587b6260266e7 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -27,6 +27,7 @@ import stat as st from _collections_abc import _check_methods +from types import GenericAlias _names = sys.builtin_module_names @@ -1060,8 +1061,9 @@ def __subclasshook__(cls, subclass): return _check_methods(subclass, '__fspath__') return NotImplemented - def __class_getitem__(cls, type): - return cls + def __class_getitem__(cls, item): + """Internal: PEP 585.""" + return GenericAlias(cls, item) if name == 'nt': diff --git a/Lib/subprocess.py b/Lib/subprocess.py index c8db387091ba39..ce99bc2ce34b34 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -52,6 +52,7 @@ import warnings import contextlib from time import monotonic as _time +import types try: import pwd @@ -446,17 +447,9 @@ 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 __class_getitem__(cls, item): + """Internal: PEP 585.""" + return types.GenericAlias(cls, item) def check_returncode(self): @@ -1000,16 +993,9 @@ 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 + def __class_getitem__(cls, item): + """Internal: PEP 585.""" + return types.GenericAlias(cls, item) @property def universal_newlines(self): diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 448163f04380d5..86c007b58d426e 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -44,6 +44,7 @@ import errno as _errno from random import Random as _Random import sys as _sys +import types as _types import weakref as _weakref import _thread _allocate_lock = _thread.allocate_lock @@ -643,17 +644,9 @@ def __init__(self, max_size=0, mode='w+b', buffering=-1, 'encoding': encoding, 'newline': newline, 'dir': dir, 'errors': errors} - 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, - `SpooledTemporaryFile[str]` is a valid expression at runtime (type - argument `str` indicates whether the file is open in bytes or text - mode). Note, no type checking happens at runtime, but a static type - checker can be used. - """ - return cls + def __class_getitem__(cls, item): + """Internal: PEP 585.""" + return _types.GenericAlias(cls, item) def _check(self, file): if self._rolled: return diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 9e3a1695dfb34e..1a90901b6d1fe7 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -25,6 +25,7 @@ import tempfile import threading import time +import types import unittest import uuid import warnings @@ -4080,7 +4081,7 @@ class A(os.PathLike): self.assertTrue(issubclass(FakePath, os.PathLike)) def test_pathlike_class_getitem(self): - self.assertIs(os.PathLike[bytes], os.PathLike) + self.assertIsInstance(os.PathLike[bytes], types.GenericAlias) class TimesTests(unittest.TestCase): diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 2bbdbaef84e992..eb31b32f664c27 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -11,6 +11,7 @@ import tempfile import time import traceback +import types import selectors import sysconfig import select @@ -1436,8 +1437,8 @@ def test_file_not_found_with_bad_cwd(self): 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) + self.assertIsInstance(subprocess.Popen[bytes], types.GenericAlias) + self.assertIsInstance(subprocess.CompletedProcess[str], types.GenericAlias) class RunFuncTestCase(BaseTestCase): def run_python(self, code, **kwargs): diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index 5fe9506b0b7ba1..6126d478b9495e 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -10,6 +10,7 @@ import warnings import contextlib import stat +import types import weakref from unittest import mock @@ -1230,8 +1231,8 @@ def test_truncate_with_size_parameter(self): self.assertEqual(os.fstat(f.fileno()).st_size, 20) def test_class_getitem(self): - self.assertIs(tempfile.SpooledTemporaryFile[bytes], - tempfile.SpooledTemporaryFile) + self.assertIsInstance(tempfile.SpooledTemporaryFile[bytes], + types.GenericAlias) if tempfile.NamedTemporaryFile is not tempfile.TemporaryFile: