Py3: Replace six.integer_types with int

In Python 3, six.integer_types is (int,). In most places, the alias can
be simply replaced with int. In other places, it was possible to
simplify the code by unpacking the tuple.

See: https://pagure.io/freeipa/issue/7715
Signed-off-by: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
This commit is contained in:
Christian Heimes
2018-09-26 12:33:30 +02:00
parent 964a9bdcec
commit ea396528b7
8 changed files with 21 additions and 25 deletions

View File

@@ -50,7 +50,7 @@ def add_message(version, result, message):
def process_message_arguments(obj, format=None, message=None, **kw):
for key, value in kw.items():
if not isinstance(value, six.integer_types):
if not isinstance(value, int):
try:
kw[key] = unicode(value)
except UnicodeError:

View File

@@ -132,7 +132,7 @@ from ipapython.dnsutil import DNSName
def _is_null(value):
if value:
return False
elif isinstance(value, six.integer_types + (float, decimal.Decimal)):
elif isinstance(value, (int, float, decimal.Decimal)):
# 0 is not NULL
return False
else:
@@ -597,7 +597,7 @@ class Param(ReadOnly):
value = self.__kw[key]
if callable(value) and hasattr(value, '__name__'):
value = value.__name__
elif isinstance(value, six.integer_types):
elif isinstance(value, int):
value = str(value)
elif isinstance(value, (tuple, set, frozenset)):
value = apirepr(list(value))
@@ -1072,7 +1072,7 @@ class Number(Param):
"""
if type(value) in self.allowed_types:
return value
if type(value) in (unicode, float) + six.integer_types:
if type(value) in (unicode, float, int):
try:
return self.type(value)
except ValueError:
@@ -1089,12 +1089,12 @@ class Int(Number):
"""
type = int
allowed_types = six.integer_types
allowed_types = (int,)
type_error = _('must be an integer')
kwargs = Param.kwargs + (
('minvalue', six.integer_types, int(MININT)),
('maxvalue', six.integer_types, int(MAXINT)),
('minvalue', int, int(MININT)),
('maxvalue', int, int(MAXINT)),
)
@staticmethod
@@ -1138,7 +1138,7 @@ class Int(Number):
"""
Check min constraint.
"""
assert type(value) in six.integer_types
assert isinstance(value, int)
if value < self.minvalue:
return _('must be at least %(minvalue)d') % dict(
minvalue=self.minvalue,
@@ -1150,7 +1150,7 @@ class Int(Number):
"""
Check max constraint.
"""
assert type(value) in six.integer_types
assert isinstance(value, int)
if value > self.maxvalue:
return _('can be at most %(maxvalue)d') % dict(
maxvalue=self.maxvalue,
@@ -1563,7 +1563,7 @@ class Str(Data):
"""
if type(value) in self.allowed_types:
return value
if type(value) in (float, decimal.Decimal) + six.integer_types:
if type(value) in (int, float, decimal.Decimal):
return self.type(value)
if type(value) in (tuple, list):
raise ConversionError(name=self.name,
@@ -1721,7 +1721,7 @@ class IntEnum(Enum):
"""
type = int
allowed_types = six.integer_types
allowed_types = (int,)
type_error = Int.type_error
def _convert_scalar(self, value, index=None):

View File

@@ -171,7 +171,7 @@ def xml_wrap(value, version):
if type(value) is Decimal:
# transfer Decimal as a string
return unicode(value)
if isinstance(value, six.integer_types) and (value < MININT or value > MAXINT):
if isinstance(value, int) and (value < MININT or value > MAXINT):
return unicode(value)
if isinstance(value, DN):
return str(value)
@@ -200,7 +200,7 @@ def xml_wrap(value, version):
return base64.b64encode(
value.public_bytes(x509_Encoding.DER)).decode('ascii')
assert type(value) in (unicode, float, bool, type(None)) + six.integer_types
assert type(value) in (unicode, float, int, bool, type(None))
return value
@@ -320,6 +320,7 @@ class _JSONPrimer(dict):
self.update({
unicode: _identity,
bool: _identity,
int: _identity,
type(None): _identity,
float: _identity,
Decimal: unicode,
@@ -334,9 +335,6 @@ class _JSONPrimer(dict):
crypto_x509.Certificate: self._enc_certificate,
crypto_x509.CertificateSigningRequest: self._enc_certificate,
})
# int, long
for t in six.integer_types:
self[t] = _identity
def __missing__(self, typ):
# walk MRO to find best match

View File

@@ -86,7 +86,7 @@ def json_serialize(obj):
return [json_serialize(o) for o in obj]
if isinstance(obj, dict):
return {k: json_serialize(v) for (k, v) in obj.items()}
if isinstance(obj, (bool, float, unicode, type(None), six.integer_types)):
if isinstance(obj, (int, bool, float, unicode, type(None))):
return obj
if isinstance(obj, str):
return obj.decode('utf-8')

View File

@@ -22,7 +22,6 @@ import datetime
import email.utils
from calendar import timegm
import six
# pylint: disable=import-error
from six.moves.urllib.parse import urlparse
# pylint: enable=import-error
@@ -392,7 +391,7 @@ class Cookie:
self._timestamp = None
elif isinstance(value, datetime.datetime):
self._timestamp = value
elif isinstance(value, (six.integer_types, float)):
elif isinstance(value, (int, float)):
self._timestamp = datetime.datetime.utcfromtimestamp(value)
elif isinstance(value, str):
self._timestamp = Cookie.parse_datetime(value)
@@ -418,7 +417,7 @@ class Cookie:
self._expires = None
elif isinstance(value, datetime.datetime):
self._expires = value
elif isinstance(value, (six.integer_types, float)):
elif isinstance(value, (int, float)):
self._expires = datetime.datetime.utcfromtimestamp(value)
elif isinstance(value, str):
self._expires = Cookie.parse_datetime(value)

View File

@@ -860,7 +860,7 @@ class RDN:
return len(self._avas)
def __getitem__(self, key):
if isinstance(key, six.integer_types):
if isinstance(key, int):
return self._get_ava(self._avas[key])
if isinstance(key, slice):
return [self._get_ava(ava) for ava in self._avas[key]]
@@ -1178,7 +1178,7 @@ class DN:
return len(self.rdns)
def __getitem__(self, key):
if isinstance(key, six.integer_types):
if isinstance(key, int):
return self._get_rdn(self.rdns[key])
if isinstance(key, slice):
cls = self.__class__

View File

@@ -909,8 +909,7 @@ class LDAPClient:
return b'TRUE'
else:
return b'FALSE'
elif isinstance(val, (unicode, six.integer_types, Decimal, DN,
Principal)):
elif isinstance(val, (unicode, int, Decimal, DN, Principal)):
return six.text_type(val).encode('utf-8')
elif isinstance(val, DNSName):
return val.to_text().encode('ascii')

View File

@@ -1243,7 +1243,7 @@ class test_Int(ClassChecker):
# Test with no kwargs:
o = self.cls('my_number')
assert o.type == int
assert o.allowed_types == six.integer_types
assert o.allowed_types == (int,)
assert isinstance(o, parameters.Int)
assert o.minvalue == int(MININT)
assert o.maxvalue == int(MAXINT)