Skip to content

Commit

Permalink
Convert some already generic things to GenericAlias to be consistent (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatyping authored Feb 25, 2020
1 parent 43a97b9 commit 4783c2e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 39 deletions.
6 changes: 4 additions & 2 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import stat as st

from _collections_abc import _check_methods
from types import GenericAlias

_names = sys.builtin_module_names

Expand Down Expand Up @@ -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':
Expand Down
28 changes: 7 additions & 21 deletions Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import warnings
import contextlib
from time import monotonic as _time
import types

try:
import pwd
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
15 changes: 4 additions & 11 deletions Lib/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import tempfile
import threading
import time
import types
import unittest
import uuid
import warnings
Expand Down Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import tempfile
import time
import traceback
import types
import selectors
import sysconfig
import select
Expand Down Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import warnings
import contextlib
import stat
import types
import weakref
from unittest import mock

Expand Down Expand Up @@ -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:

Expand Down

0 comments on commit 4783c2e

Please sign in to comment.