Skip to content

Commit 84ebe18

Browse files
author
Bas van Beek
committed
Revert the type(...) to types.... replacements
Reverts 2791a23 and 0a97a88. Save this for a follow up.
1 parent 33caa6f commit 84ebe18

28 files changed

+72
-78
lines changed

Lib/ast.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
:copyright: Copyright 2008 by Armin Ronacher.
2525
:license: Python License.
2626
"""
27-
import types
2827
import sys
2928
from _ast import *
3029
from contextlib import contextmanager, nullcontext
@@ -572,22 +571,22 @@ def __new__(cls, *args, **kwargs):
572571
Num: (int, float, complex),
573572
Str: (str,),
574573
Bytes: (bytes,),
575-
NameConstant: (types.NoneType, bool),
576-
Ellipsis: (types.EllipsisType,),
574+
NameConstant: (type(None), bool),
575+
Ellipsis: (type(...),),
577576
}
578577
_const_types_not = {
579578
Num: (bool,),
580579
}
581580

582581
_const_node_type_names = {
583582
bool: 'NameConstant', # should be before int
584-
types.NoneType: 'NameConstant',
583+
type(None): 'NameConstant',
585584
int: 'Num',
586585
float: 'Num',
587586
complex: 'Num',
588587
str: 'Str',
589588
bytes: 'Bytes',
590-
types.EllipsisType: 'Ellipsis',
589+
type(...): 'Ellipsis',
591590
}
592591

593592
class slice(AST):

Lib/copy.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ def copy(x):
106106

107107
def _copy_immutable(x):
108108
return x
109-
for t in (types.NoneType, int, float, bool, complex, str, tuple,
109+
for t in (type(None), int, float, bool, complex, str, tuple,
110110
bytes, frozenset, type, range, slice, property,
111-
types.BuiltinFunctionType, types.EllipsisType,
112-
types.NotImplementedType, types.FunctionType, weakref.ref):
111+
types.BuiltinFunctionType, type(Ellipsis), type(NotImplemented),
112+
types.FunctionType, weakref.ref):
113113
d[t] = _copy_immutable
114114
t = getattr(types, "CodeType", None)
115115
if t is not None:
@@ -181,9 +181,9 @@ def deepcopy(x, memo=None, _nil=[]):
181181

182182
def _deepcopy_atomic(x, memo):
183183
return x
184-
d[types.NoneType] = _deepcopy_atomic
185-
d[types.EllipsisType] = _deepcopy_atomic
186-
d[types.NotImplementedType] = _deepcopy_atomic
184+
d[type(None)] = _deepcopy_atomic
185+
d[type(Ellipsis)] = _deepcopy_atomic
186+
d[type(NotImplemented)] = _deepcopy_atomic
187187
d[int] = _deepcopy_atomic
188188
d[float] = _deepcopy_atomic
189189
d[bool] = _deepcopy_atomic

Lib/distutils/unixccompiler.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* link shared library handled by 'cc -shared'
1414
"""
1515

16-
import os, sys, re, types
16+
import os, sys, re
1717

1818
from distutils import sysconfig
1919
from distutils.dep_util import newer
@@ -157,7 +157,7 @@ def link(self, target_desc, objects,
157157

158158
lib_opts = gen_lib_options(self, library_dirs, runtime_library_dirs,
159159
libraries)
160-
if not isinstance(output_dir, (str, types.NoneType)):
160+
if not isinstance(output_dir, (str, type(None))):
161161
raise TypeError("'output_dir' must be a string or None")
162162
if output_dir is not None:
163163
output_filename = os.path.join(output_dir, output_filename)

Lib/idlelib/run.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import _thread as thread
1616
import threading
1717
import warnings
18-
import types
1918

2019
from idlelib import autocomplete # AutoComplete, fetch_encodings
2120
from idlelib import calltip # Calltip
@@ -559,7 +558,7 @@ def runcode(self, code):
559558
except SystemExit as e:
560559
if e.args: # SystemExit called with an argument.
561560
ob = e.args[0]
562-
if not isinstance(ob, (types.NoneType, int)):
561+
if not isinstance(ob, (type(None), int)):
563562
print('SystemExit: ' + str(ob), file=sys.stderr)
564563
# Return to the interactive prompt.
565564
except:

Lib/imp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def find_module(name, path=None):
264264
"""
265265
if not isinstance(name, str):
266266
raise TypeError("'name' must be a str, not {}".format(type(name)))
267-
elif not isinstance(path, (types.NoneType, list)):
267+
elif not isinstance(path, (type(None), list)):
268268
# Backwards-compatibility
269269
raise RuntimeError("'path' must be None or a list, "
270270
"not {}".format(type(path)))

Lib/inspect.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2034,7 +2034,7 @@ def wrap_value(s):
20342034
except NameError:
20352035
raise RuntimeError()
20362036

2037-
if isinstance(value, (str, int, float, bytes, bool, types.NoneType)):
2037+
if isinstance(value, (str, int, float, bytes, bool, type(None))):
20382038
return ast.Constant(value)
20392039
raise RuntimeError()
20402040

Lib/lib2to3/fixes/fix_types.py

+3
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@
3030
'ComplexType' : 'complex',
3131
'DictType': 'dict',
3232
'DictionaryType' : 'dict',
33+
'EllipsisType' : 'type(Ellipsis)',
3334
#'FileType' : 'io.IOBase',
3435
'FloatType': 'float',
3536
'IntType': 'int',
3637
'ListType': 'list',
3738
'LongType': 'int',
3839
'ObjectType' : 'object',
40+
'NoneType': 'type(None)',
41+
'NotImplementedType' : 'type(NotImplemented)',
3942
'SliceType' : 'slice',
4043
'StringType': 'bytes', # XXX ?
4144
'StringTypes' : '(str,)', # XXX ?

Lib/lib2to3/tests/test_fixers.py

+4
Original file line numberDiff line numberDiff line change
@@ -3276,6 +3276,10 @@ def test_basic_types_convert(self):
32763276
a = """int"""
32773277
self.check(b, a)
32783278

3279+
b = """types.NoneType"""
3280+
a = """type(None)"""
3281+
self.check(b, a)
3282+
32793283
b = "types.StringTypes"
32803284
a = "(str,)"
32813285
self.check(b, a)

Lib/pickle.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
2424
"""
2525

26-
import types
26+
from types import FunctionType
2727
from copyreg import dispatch_table
2828
from copyreg import _extension_registry, _inverted_registry, _extension_cache
2929
from itertools import islice
@@ -737,7 +737,7 @@ def save_reduce(self, func, args, state=None, listitems=None,
737737

738738
def save_none(self, obj):
739739
self.write(NONE)
740-
dispatch[types.NoneType] = save_none
740+
dispatch[type(None)] = save_none
741741

742742
def save_bool(self, obj):
743743
if self.proto >= 2:
@@ -1117,15 +1117,15 @@ def save_global(self, obj, name=None):
11171117
self.memoize(obj)
11181118

11191119
def save_type(self, obj):
1120-
if obj is types.NoneType:
1120+
if obj is type(None):
11211121
return self.save_reduce(type, (None,), obj=obj)
1122-
elif obj is types.NotImplementedType:
1122+
elif obj is type(NotImplemented):
11231123
return self.save_reduce(type, (NotImplemented,), obj=obj)
1124-
elif obj is types.EllipsisType:
1124+
elif obj is type(...):
11251125
return self.save_reduce(type, (...,), obj=obj)
11261126
return self.save_global(obj)
11271127

1128-
dispatch[types.FunctionType] = save_global
1128+
dispatch[FunctionType] = save_global
11291129
dispatch[type] = save_type
11301130

11311131

Lib/pickletools.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import pickle
1616
import re
1717
import sys
18-
import types
1918

2019
__all__ = ['dis', 'genops', 'optimize']
2120

@@ -1018,7 +1017,7 @@ def __repr__(self):
10181017

10191018
pynone = StackObject(
10201019
name="None",
1021-
obtype=types.NoneType,
1020+
obtype=type(None),
10221021
doc="The Python None object.")
10231022

10241023
pytuple = StackObject(

Lib/pprint.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ def _safe_repr(object, context, maxlevels, level, sort_dicts):
591591
return rep, (rep and not rep.startswith('<')), False
592592

593593
_builtin_scalars = frozenset({str, bytes, bytearray, int, float, complex,
594-
bool, _types.NoneType})
594+
bool, type(None)})
595595

596596
def _recursion(object):
597597
return ("<Recursion on %s with id=%s>"

Lib/pydoc_data/topics.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@
13651365
'explicitly return a\n'
13661366
'value. It supports no special operations. There is '
13671367
'exactly one null\n'
1368-
'object, named "None" (a built-in name). "types.NoneType()" '
1368+
'object, named "None" (a built-in name). "type(None)()" '
13691369
'produces the\n'
13701370
'same singleton.\n'
13711371
'\n'

Lib/random.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
from bisect import bisect as _bisect
5656
import os as _os
5757
import _random
58-
import types
5958

6059
try:
6160
# hashlib is pretty heavy to load, try lean internal module first
@@ -155,7 +154,7 @@ def seed(self, a=None, version=2):
155154
a += _sha512(a).digest()
156155
a = int.from_bytes(a, 'big')
157156

158-
elif not isinstance(a, (types.NoneType, int, float, str, bytes, bytearray)):
157+
elif not isinstance(a, (type(None), int, float, str, bytes, bytearray)):
159158
_warn('Seeding based on hashing is deprecated\n'
160159
'since Python 3.9 and will be removed in a subsequent '
161160
'version. The only \n'

Lib/runpy.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def run_path(path_name, init_globals=None, run_name=None):
261261
if type(importer).__module__ == 'imp':
262262
if type(importer).__name__ == 'NullImporter':
263263
is_NullImporter = True
264-
if isinstance(importer, types.NoneType) or is_NullImporter:
264+
if isinstance(importer, type(None)) or is_NullImporter:
265265
# Not a valid sys.path entry, so run the code directly
266266
# execfile() doesn't help as we want to allow compiled files
267267
code, fname = _get_code_from_file(run_name, path_name)

Lib/sqlite3/test/userfunctions.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
# misrepresented as being the original software.
2222
# 3. This notice may not be removed or altered from any source distribution.
2323

24-
import types
2524
import unittest
2625
import unittest.mock
2726
import sqlite3 as sqlite
@@ -50,7 +49,7 @@ def func_isint(v):
5049
def func_isfloat(v):
5150
return type(v) is float
5251
def func_isnone(v):
53-
return type(v) is types.NoneType
52+
return type(v) is type(None)
5453
def func_isblob(v):
5554
return isinstance(v, (bytes, memoryview))
5655
def func_islonglong(v):
@@ -108,7 +107,7 @@ def __init__(self):
108107
self.val = None
109108

110109
def step(self, whichType, val):
111-
theType = {"str": str, "int": int, "float": float, "None": types.NoneType,
110+
theType = {"str": str, "int": int, "float": float, "None": type(None),
112111
"blob": bytes}
113112
self.val = int(theType[whichType] is type(val))
114113

@@ -120,7 +119,7 @@ def __init__(self):
120119
self.val = 0
121120

122121
def step(self, whichType, *vals):
123-
theType = {"str": str, "int": int, "float": float, "None": types.NoneType,
122+
theType = {"str": str, "int": int, "float": float, "None": type(None),
124123
"blob": bytes}
125124
for val in vals:
126125
self.val += int(theType[whichType] is type(val))
@@ -212,7 +211,7 @@ def CheckFuncReturnNull(self):
212211
cur = self.con.cursor()
213212
cur.execute("select returnnull()")
214213
val = cur.fetchone()[0]
215-
self.assertEqual(type(val), types.NoneType)
214+
self.assertEqual(type(val), type(None))
216215
self.assertEqual(val, None)
217216

218217
def CheckFuncReturnBlob(self):

Lib/test/test_dataclasses.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import inspect
99
import builtins
1010
import unittest
11-
import types
1211
from unittest.mock import Mock
1312
from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional
1413
from typing import get_type_hints
@@ -2027,7 +2026,7 @@ class C:
20272026
def test_docstring_one_field_with_default_none(self):
20282027
@dataclass
20292028
class C:
2030-
x: Union[int, types.NoneType] = None
2029+
x: Union[int, type(None)] = None
20312030

20322031
self.assertDocStrEqual(C.__doc__, "C(x:Optional[int]=None)")
20332032

@@ -2956,7 +2955,7 @@ def test_text_annotations(self):
29562955
self.assertEqual(
29572956
get_type_hints(dataclass_textanno.Bar.__init__),
29582957
{'foo': dataclass_textanno.Foo,
2959-
'return': types.NoneType})
2958+
'return': type(None)})
29602959

29612960

29622961
class TestMakeDataclass(unittest.TestCase):

Lib/test/test_descr.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -3260,7 +3260,7 @@ class Int(int): __slots__ = []
32603260
cant(2, bool)
32613261
o = object()
32623262
cant(o, type(1))
3263-
cant(o, types.NoneType)
3263+
cant(o, type(None))
32643264
del o
32653265
class G(object):
32663266
__slots__ = ["a", "b"]
@@ -4000,35 +4000,35 @@ class D(C):
40004000

40014001
def test_unsubclassable_types(self):
40024002
with self.assertRaises(TypeError):
4003-
class X(types.NoneType):
4003+
class X(type(None)):
40044004
pass
40054005
with self.assertRaises(TypeError):
4006-
class X(object, types.NoneType):
4006+
class X(object, type(None)):
40074007
pass
40084008
with self.assertRaises(TypeError):
4009-
class X(types.NoneType, object):
4009+
class X(type(None), object):
40104010
pass
40114011
class O(object):
40124012
pass
40134013
with self.assertRaises(TypeError):
4014-
class X(O, types.NoneType):
4014+
class X(O, type(None)):
40154015
pass
40164016
with self.assertRaises(TypeError):
4017-
class X(types.NoneType, O):
4017+
class X(type(None), O):
40184018
pass
40194019

40204020
class X(object):
40214021
pass
40224022
with self.assertRaises(TypeError):
4023-
X.__bases__ = types.NoneType,
4023+
X.__bases__ = type(None),
40244024
with self.assertRaises(TypeError):
4025-
X.__bases__ = object, types.NoneType
4025+
X.__bases__ = object, type(None)
40264026
with self.assertRaises(TypeError):
4027-
X.__bases__ = types.NoneType, object
4027+
X.__bases__ = type(None), object
40284028
with self.assertRaises(TypeError):
4029-
X.__bases__ = O, types.NoneType
4029+
X.__bases__ = O, type(None)
40304030
with self.assertRaises(TypeError):
4031-
X.__bases__ = types.NoneType, O
4031+
X.__bases__ = type(None), O
40324032

40334033
def test_mutable_bases_with_failing_mro(self):
40344034
# Testing mutable bases with failing mro...

0 commit comments

Comments
 (0)