Skip to content

Commit 3e571ca

Browse files
code-health: remove built-in types wrappers
The built-in basestring abstract type was removed in Python 3 [1]. `str` must be used for strings and `bytes` for byte objects. `long` type was unified with `int` in Python 3 [2]. 1. https://docs.python.org/3/whatsnew/3.0.html?highlight=basestring 2. https://peps.python.org/pep-0237/ Part of #212
1 parent 4f413c1 commit 3e571ca

File tree

6 files changed

+28
-63
lines changed

6 files changed

+28
-63
lines changed

Diff for: tarantool/connection.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
check_key,
8686
greeting_decode,
8787
version_id,
88-
string_types,
8988
ENCODING_DEFAULT,
9089
)
9190

@@ -640,7 +639,7 @@ def replace(self, space_name, values):
640639
641640
:rtype: `Response` instance
642641
'''
643-
if isinstance(space_name, string_types):
642+
if isinstance(space_name, str):
644643
space_name = self.schema.get_space(space_name).sid
645644
request = RequestReplace(self, space_name, values)
646645
return self._send_request(request)
@@ -697,7 +696,7 @@ class JoinState:
697696
def _ops_process(self, space, update_ops):
698697
new_ops = []
699698
for op in update_ops:
700-
if isinstance(op[1], string_types):
699+
if isinstance(op[1], str):
701700
op = list(op)
702701
op[1] = self.schema.get_field(space, op[1])['id']
703702
new_ops.append(op)
@@ -733,7 +732,7 @@ def insert(self, space_name, values):
733732
734733
:rtype: `Response` instance
735734
'''
736-
if isinstance(space_name, string_types):
735+
if isinstance(space_name, str):
737736
space_name = self.schema.get_space(space_name).sid
738737
request = RequestInsert(self, space_name, values)
739738
return self._send_request(request)
@@ -754,9 +753,9 @@ def delete(self, space_name, key, **kwargs):
754753
index_name = kwargs.get("index", 0)
755754

756755
key = check_key(key)
757-
if isinstance(space_name, string_types):
756+
if isinstance(space_name, str):
758757
space_name = self.schema.get_space(space_name).sid
759-
if isinstance(index_name, string_types):
758+
if isinstance(index_name, str):
760759
index_name = self.schema.get_index(space_name, index_name).iid
761760
request = RequestDelete(self, space_name, index_name, key)
762761
return self._send_request(request)
@@ -826,9 +825,9 @@ def upsert(self, space_name, tuple_value, op_list, **kwargs):
826825
'''
827826
index_name = kwargs.get("index", 0)
828827

829-
if isinstance(space_name, string_types):
828+
if isinstance(space_name, str):
830829
space_name = self.schema.get_space(space_name).sid
831-
if isinstance(index_name, string_types):
830+
if isinstance(index_name, str):
832831
index_name = self.schema.get_index(space_name, index_name).iid
833832
op_list = self._ops_process(space_name, op_list)
834833
request = RequestUpsert(self, space_name, index_name, tuple_value,
@@ -902,9 +901,9 @@ def update(self, space_name, key, op_list, **kwargs):
902901
index_name = kwargs.get("index", 0)
903902

904903
key = check_key(key)
905-
if isinstance(space_name, string_types):
904+
if isinstance(space_name, str):
906905
space_name = self.schema.get_space(space_name).sid
907-
if isinstance(index_name, string_types):
906+
if isinstance(index_name, str):
908907
index_name = self.schema.get_index(space_name, index_name).iid
909908
op_list = self._ops_process(space_name, op_list)
910909
request = RequestUpdate(self, space_name, index_name, key, op_list)
@@ -985,9 +984,9 @@ def select(self, space_name, key=None, **kwargs):
985984
# tuples)
986985
key = check_key(key, select=True)
987986

988-
if isinstance(space_name, string_types):
987+
if isinstance(space_name, str):
989988
space_name = self.schema.get_space(space_name).sid
990-
if isinstance(index_name, string_types):
989+
if isinstance(index_name, str):
991990
index_name = self.schema.get_index(space_name, index_name).iid
992991
request = RequestSelect(self, space_name, index_name, key, offset,
993992
limit, iterator_type)

Diff for: tarantool/mesh_connection.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@
3333
RequestCall
3434
)
3535

36-
try:
37-
string_types = basestring
38-
except NameError:
39-
string_types = str
40-
4136
default_addr_opts = {
4237
'transport': DEFAULT_TRANSPORT,
4338
'ssl_key_file': DEFAULT_SSL_KEY_FILE,
@@ -55,7 +50,7 @@ def parse_error(uri, msg):
5550

5651
if not uri:
5752
return parse_error(uri, 'should not be None or empty string')
58-
if not isinstance(uri, string_types):
53+
if not isinstance(uri, str):
5954
return parse_error(uri, 'should be of a string type')
6055
if uri.count(':') != 1:
6156
return parse_error(uri, 'does not match host:port scheme')
@@ -117,7 +112,7 @@ def format_error(address, err):
117112
if 'host' not in result or result['host'] is None:
118113
return format_error(result,
119114
'host is mandatory for an inet result')
120-
if not isinstance(result['host'], string_types):
115+
if not isinstance(result['host'], str):
121116
return format_error(result,
122117
'host must be a string for an inet result')
123118

@@ -131,7 +126,7 @@ def format_error(address, err):
131126

132127
# Looks okay.
133128
return result, None
134-
elif isinstance(result['port'], string_types):
129+
elif isinstance(result['port'], str):
135130
# Looks like a unix address.
136131

137132
# Expect no host.
@@ -140,7 +135,7 @@ def format_error(address, err):
140135
result, 'host must be unset or None for a unix result')
141136

142137
# Validate port.
143-
if not isinstance(result['port'], string_types):
138+
if not isinstance(result['port'], str):
144139
return format_error(result,
145140
'port must be a string for a unix result')
146141

Diff for: tarantool/request.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
from tarantool.response import Response, ResponseExecute
5757
from tarantool.utils import (
5858
strxor,
59-
binary_types
6059
)
6160

6261
from tarantool.msgpack_ext.packer import default as packer_default
@@ -187,7 +186,7 @@ def sha1(values):
187186
sha = hashlib.sha1()
188187
for i in values:
189188
if i is not None:
190-
if isinstance(i, binary_types):
189+
if isinstance(i, bytes):
191190
sha.update(i)
192191
else:
193192
sha.update(i.encode())

Diff for: tarantool/schema.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
It is a Tarantool schema description.
66
'''
77

8-
from tarantool.utils import (
9-
string_types,
10-
integer_types,
11-
)
128
from tarantool.error import (
139
Error,
1410
SchemaError,
@@ -151,7 +147,7 @@ def fetch_space(self, space):
151147
)
152148
elif len(space_row) == 0 or not len(space_row[0]):
153149
# We can't find space with this name or id
154-
temp_name = 'name' if isinstance(space, string_types) else 'id'
150+
temp_name = 'name' if isinstance(space, str) else 'id'
155151
errmsg = "There's no space with {1} '{0}'".format(space, temp_name)
156152
raise SchemaError(errmsg)
157153

@@ -161,7 +157,7 @@ def fetch_space(self, space):
161157

162158
def fetch_space_from(self, space):
163159
_index = None
164-
if isinstance(space, string_types):
160+
if isinstance(space, str):
165161
_index = const.INDEX_SPACE_NAME
166162
else:
167163
_index = const.INDEX_SPACE_PRIMARY
@@ -212,7 +208,7 @@ def fetch_index(self, space_object, index):
212208
)
213209
elif len(index_row) == 0 or not len(index_row[0]):
214210
# We can't find index with this name or id
215-
temp_name = 'name' if isinstance(index, string_types) else 'id'
211+
temp_name = 'name' if isinstance(index, str) else 'id'
216212
errmsg = ("There's no index with {2} '{0}'"
217213
" in space '{1}'").format(index, space_object.name,
218214
temp_name)
@@ -229,7 +225,7 @@ def fetch_index_all(self):
229225

230226
def fetch_index_from(self, space, index):
231227
_index = None
232-
if isinstance(index, string_types):
228+
if isinstance(index, str):
233229
_index = const.INDEX_INDEX_NAME
234230
else:
235231
_index = const.INDEX_INDEX_PRIMARY
@@ -269,7 +265,7 @@ def get_field(self, space, field):
269265
try:
270266
return _space.format[field]
271267
except:
272-
tp = 'name' if isinstance(field, string_types) else 'id'
268+
tp = 'name' if isinstance(field, str) else 'id'
273269
errmsg = "There's no field with {2} '{0}' in space '{1}'".format(
274270
field, _space.name, tp
275271
)

Diff for: tarantool/utils.py

+5-29
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,14 @@
22
import sys
33
import uuid
44

5-
# Compatibility layer for Python2/Python3
6-
if sys.version_info.major == 2:
7-
string_types = (basestring, )
8-
integer_types = (int, long)
9-
supported_types = integer_types + string_types + (float,)
5+
supported_types = (int, str, bytes, float,)
106

11-
ENCODING_DEFAULT = None
7+
ENCODING_DEFAULT = "utf-8"
128

13-
if sys.version_info.minor < 6:
14-
binary_types = (str, )
15-
else:
16-
binary_types = (bytes, )
17-
from base64 import decodestring as base64_decode
9+
from base64 import decodebytes as base64_decode
1810

19-
def strxor(rhs, lhs):
20-
return "".join(chr(ord(x) ^ ord(y)) for x, y in zip(rhs, lhs))
21-
22-
elif sys.version_info.major == 3:
23-
binary_types = (bytes, )
24-
string_types = (str, )
25-
integer_types = (int, )
26-
supported_types = integer_types + string_types + binary_types + (float,)
27-
28-
ENCODING_DEFAULT = "utf-8"
29-
30-
from base64 import decodebytes as base64_decode
31-
32-
def strxor(rhs, lhs):
33-
return bytes([x ^ y for x, y in zip(rhs, lhs)])
34-
35-
else:
36-
pass # unreachable
11+
def strxor(rhs, lhs):
12+
return bytes([x ^ y for x, y in zip(rhs, lhs)])
3713

3814
def check_key(*args, **kwargs):
3915
if 'first' not in kwargs:

Diff for: test/suites/test_dml.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def test_07_call_16(self):
183183
ans = con.call('fiber.time64')
184184
self.assertEqual(len(ans), 1)
185185
self.assertEqual(len(ans[0]), 1)
186-
self.assertIsInstance(ans[0][0], tarantool.utils.integer_types)
186+
self.assertIsInstance(ans[0][0], int)
187187
ans = con.call('uuid.str')
188188
self.assertEqual(len(ans), 1)
189189
self.assertEqual(len(ans[0]), 1)
@@ -207,7 +207,7 @@ def test_07_call_17(self):
207207
self.assertIsInstance(ans[0], float)
208208
ans = con.call('fiber.time64')
209209
self.assertEqual(len(ans), 1)
210-
self.assertIsInstance(ans[0], tarantool.utils.integer_types)
210+
self.assertIsInstance(ans[0], int)
211211
ans = con.call('uuid.str')
212212
self.assertEqual(len(ans), 1)
213213
self.assertIsInstance(ans[0], str)

0 commit comments

Comments
 (0)