8000 Replaced `type(NotImplemented)` and `type(None)` · python/cpython@2791a23 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2791a23

Browse files
author
Bas van Beek
committed
Replaced type(NotImplemented) and type(None)
`type(NotImplemented)` and `type(None)` have, respectively, been replaced with `types.NotImplementedType` and `types.NoneType`
1 parent 9fc1381 commit 2791a23

28 files changed

+73
-67
lines changed

Lib/ast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ def __new__(cls, *args, **kwargs):
572572
Num: (int, float, complex),
573573
Str: (str,),
574574
Bytes: (bytes,),
575-
NameConstant: (type(None), bool),
575+
NameConstant: (types.NoneType, bool),
576576
Ellipsis: (types.EllipsisType,),
577577
}
578578
_const_types_not = {
@@ -581,7 +581,7 @@ def __new__(cls, *args, **kwargs):
581581

582582
_const_node_type_names = {
583583
bool: 'NameConstant', # should be before int
584-
type(None): 'NameConstant',
584+
types.NoneType: 'NameConstant',
585585
int: 'Num',
586586
float: 'Num',
587587
complex: 'Num',

Lib/copy.py

Lines changed: 4 additions & 4 deletions
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 (type(None), int, float, bool, complex, str, tuple,
109+
for t in (types.NoneType, int, float, bool, complex, str, tuple,
110110
bytes, frozenset, type, range, slice, property,
111111
types.BuiltinFunctionType, types.EllipsisType,
112-
type(NotImplemented), types.FunctionType, weakref.ref):
112+
types.NotImplementedType, 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[type(None)] = _deepcopy_atomic
184+
d[types.NoneType] = _deepcopy_atomic
185185
d[types.EllipsisType] = _deepcopy_atomic
186-
d[type(NotImplemented)] = _deepcopy_atomic
186+
d[types.NotImplementedType] = _deepcopy_atomic
187187
d[int] = _deepcopy_atomic
188188
d[float] = _deepcopy_atomic
189189
d[bool] = _deepcopy_atomic

Lib/distutils/unixccompiler.py

Lines changed: 2 additions & 2 deletions
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
16+
import os, sys, re, types
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, type(None))):
160+
if not isinstance(output_dir, (str, types.NoneType)):
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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import _thread as thread
1616
import threading
1717
import warnings
18+
import types
1819

1920
from idlelib import autocomplete # AutoComplete, fetch_encodings
2021
from idlelib import calltip # Calltip
@@ -558,7 +559,7 @@ def runcode(self, code):
558559
except SystemExit as e:
559560
if e.args: # SystemExit called with an argument.
560561
ob = e.args[0]
561-
if not isinstance(ob, (type(None), int)):
562+
if not isinstance(ob, (types.NoneType, int)):
562563
print('SystemExit: ' + str(ob), file=sys.stderr)
563564
# Return to the interactive prompt.
564565
except:

Lib/imp.py

Lines changed: 1 addition & 1 deletion
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, (type(< F987 span class="pl-c1 x">None), list)):
267+
elif not isinstance(path, (types.NoneType, list)):
268268
# Backwards-compatibility
269269
raise RuntimeError("'path' must be None or a list, "
270270
"not {}".format(type(path)))

Lib/inspect.py

Lines changed: 1 addition & 1 deletion
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, type(None))):
2037+
if isinstance(value, (str, int, float, bytes, bool, types.NoneType)):
20382038
return ast.Constant(value)
20392039
raise RuntimeError()
20402040

Lib/lib2to3/fixes/fix_types.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
'ListType': 'list',
3737
'LongType': 'int',
3838
'ObjectType' : 'object',
39-
'NoneType': 'type(None)',
40-
'NotImplementedType' : 'type(NotImplemented)',
4139
'SliceType' : 'slice',
4240
'StringType': 'bytes', # XXX ?
4341
'StringTypes' : '(str,)', # XXX ?

Lib/lib2to3/tests/test_fixers.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3276,10 +3276,6 @@ 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-
32833279
b = "types.StringTypes"
32843280
a = "(str,)"
32853281
self.check(b, a)

Lib/pickle.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
2424
"""
2525

26-
from types import FunctionType, EllipsisType
26+
import types
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[type(None)] = save_none
740+
dispatch[types.NoneType] = 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 type(None):
1120+
if obj is types.NoneType:
11211121
return self.save_reduce(type, (None,), obj=obj)
1122-
elif obj is type(NotImplemented):
1122+
elif obj is types.NotImplementedType:
11231123
return self.save_reduce(type, (NotImplemented,), obj=obj)
1124-
elif obj is EllipsisType:
1124+
elif obj is types.EllipsisType:
11251125
return self.save_reduce(type, (...,), obj=obj)
11261126
return self.save_global(obj)
11271127

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

11311131

Lib/pickletools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import pickle
1616
import re
1717
import sys
18+
import types
1819

1920
__all__ = ['dis', 'genops', 'optimize']
2021

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

10181019
pynone = StackObject(
10191020
name="None",
1020-
obtype=type(None),
1021+
obtype=types.NoneType,
10211022
doc="The Python None object.")
10221023

10231024
pytuple = StackObject(

Lib/pprint.py

Lines changed: 1 addition & 1 deletion
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, type(None)})
594+
bool, _types.NoneType})
595595

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

Lib/pydoc_data/topics.py

Lines changed: 1 addition & 1 deletion
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). "type(None)()" '
1368+
'object, named "None" (a built-in name). "types.NoneType()" '
13691369
'produces the\n'
13701370
'same singleton.\n'
13711371
'\n'

Lib/random.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from bisect import bisect as _bisect
5656
import os as _os
5757
import _random
58+
import types
5859

5960
try:
6061
# hashlib is pretty heavy to load, try lean internal module first
@@ -154,7 +155,7 @@ def seed(self, a=None, version=2):
154155
a += _sha512(a).digest()
155156
a = int.from_bytes(a, 'big')
156157

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

Lib/runpy.py

Lines changed: 1 addition & 1 deletion
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, type(None)) or is_NullImporter:
264+
if isinstance(importer, types.NoneType) 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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
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
2425
import unittest
2526
import unittest.mock
2627
import sqlite3 as sqlite
@@ -49,7 +50,7 @@ def func_isint(v):
4950
def func_isfloat(v):
5051
return type(v) is float
5152
def func_isnone(v):
52-
return type(v) is type(None)
53+
return type(v) is types.NoneType
5354
def func_isblob(v):
5455
return isinstance(v, (bytes, memoryview))
5556
def func_islonglong(v):
@@ -107,7 +108,7 @@ def __init__(self):
107108
self.val = None
108109

109110
def step(self, whichType, val):
110-
theType = {"str": str, "int": int, "float": float, "None": type(None),
111+
theType = {"str": str, "int": int, "float": float, "None": types.NoneType,
111112
"blob": bytes}
112113
self.val = int(theType[whichType] is type(val))
113114

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

121122
def step(self, whichType, *vals):
122-
theType = {"str": str, "int": int, "float": float, "None": type(None),
123+
theType = {"str": str, "int": int, "float": float, "None": types.NoneType,
123124
"blob": bytes}
124125
for val in vals:
125126
self.val += int(theType[whichType] is type(val))
@@ -211,7 +212,7 @@ def CheckFuncReturnNull(self):
211212
cur = self.con.cursor()
212213
cur.execute("select returnnull()")
213214
val = cur.fetchone()[0]
214-
self.assertEqual(type(val), type(None))
215+
self.assertEqual(type(val), types.NoneType)
215216
self.assertEqual(val, None)
216217

217218
def CheckFuncReturnBlob(self):

Lib/test/test_dataclasses.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import inspect
99
import builtins
1010
import unittest
11+
import types
1112
from unittest.mock import Mock
1213
from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional
1314
from typing import get_type_hints
@@ -2026,7 +2027,7 @@ class C:
20262027
def test_docstring_one_field_with_default_none(self):
20272028
@dataclass
20282029
class C:
2029-
x: Union[int, type(None)] = None
2030+
x: Union[int, types.NoneType] = None
20302031

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

@@ -2955,7 +2956,7 @@ def test_text_annotations(self):
29552956
self.assertEqual(
29562957
get_type_hints(dataclass_textanno.Bar.__init__),
29572958
{'foo': dataclass_textanno.Foo,
2958-
'return': type(None)})
2959+
'return': types.NoneType})
29592960

29602961

29612962
class TestMakeDataclass(unittest.TestCase):

Lib/test/test_descr.py

Lines changed: 11 additions & 11 deletions
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, type(None))
3263+
cant(o, types.NoneType)
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(type(None)):
4003+
class X(types.NoneType):
40044004
pass
40054005
with self.assertRaises(TypeError):
4006-
class X(object, type(None)):
4006+
class X(object, types.NoneType):
40074007
pass
40084008
with self.assertRaises(TypeError):
4009-
class X(type(None), object):
4009+
class X(types.NoneType, object):
40104010
pass
40114011
class O(object):
40124012
pass
40134013
with self.assertRaises(TypeError):
4014-
class X(O, type(None)):
4014+
class X(O, types.NoneType):
40154015
pass
40164016
with self.assertRaises(TypeError):
4017-
class X(type(None), O):
4017+
class X(types.NoneType, O):
40184018
pass
40194019

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

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

Lib/test/test_getargs2.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import math
33
import string
44
import sys
5+
import types
56
from test import support
67
from test.support import import_helper
78
# Skip this test if the _testcapi module isn't available.
@@ -567,11 +568,11 @@ def test_args(self):
567568

568569
ret = get_args()
569570
self.assertIn(ret, ((), None))
570-
self.assertIn(type(ret), (tuple, type(None)))
571+
self.assertIn(type(ret), (tuple, types.NoneType))
571572

572573
ret = get_args(*())
573574
self.assertIn(ret, ((), None))
574-
self.assertIn(type(ret), (tuple, type(None)))
575+
self.assertIn(type(ret), (tuple, types.NoneType))
575576

576577
def test_tuple(self):
577578
from _testcapi import getargs_tuple
@@ -605,11 +606,11 @@ def test_kwargs(self):
605606

606607
ret = get_kwargs()
607608
self.assertIn(ret, ({}, None))
608-
self.assertIn(type(ret), (dict, type(None)))
609+
self.assertIn(type(ret), (dict, types.NoneType))
609610

610611
ret = get_kwargs(**{})
611612
self.assertIn(ret, ({}, None))
612-
self.assertIn(type(ret), (dict, type(None)))
613+
self.assertIn(type(ret), (dict, types.NoneType))
613614

614615
def test_positional_args(self):
615616
# using all positional args

Lib/test/test_nntplib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os.path
1010
import re
1111
import threading
12+
import types
1213

1314
from test import support
1415
from test.support import socket_helper
@@ -126,7 +127,7 @@ def _check_art_dict(self, art_dict):
126127
"references", ":bytes", ":lines"}
127128
)
128129
for v in art_dict.values():
129-
self.assertIsInstance(v, (str, type(None)))
130+
self.assertIsInstance(v, (str, types.NoneType))
130131

131132
def test_xover(self):
132133
resp, count, first, last, name = self.server.group(self.GROUP_NAME)

0 commit comments

Comments
 (0)
0