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): def process_message_arguments(obj, format=None, message=None, **kw):
for key, value in kw.items(): for key, value in kw.items():
if not isinstance(value, six.integer_types): if not isinstance(value, int):
try: try:
kw[key] = unicode(value) kw[key] = unicode(value)
except UnicodeError: except UnicodeError:

View File

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

View File

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

View File

@@ -86,7 +86,7 @@ def json_serialize(obj):
return [json_serialize(o) for o in obj] return [json_serialize(o) for o in obj]
if isinstance(obj, dict): if isinstance(obj, dict):
return {k: json_serialize(v) for (k, v) in obj.items()} 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 return obj
if isinstance(obj, str): if isinstance(obj, str):
return obj.decode('utf-8') return obj.decode('utf-8')

View File

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

View File

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

View File

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

View File

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