From 9381c52b800a5eb85ab8549887987656c96efb16 Mon Sep 17 00:00:00 2001 From: Avasam Date: Sun, 21 Apr 2024 15:27:49 -0400 Subject: [PATCH] Remove more redundant internal compatibility aliases --- boltons/deprutils.py | 3 +-- boltons/funcutils.py | 16 +++++++--------- boltons/mathutils.py | 15 +++------------ boltons/timeutils.py | 7 +++---- boltons/urlutils.py | 14 ++++++-------- 5 files changed, 20 insertions(+), 35 deletions(-) diff --git a/boltons/deprutils.py b/boltons/deprutils.py index ac5d294c..6c56736f 100644 --- a/boltons/deprutils.py +++ b/boltons/deprutils.py @@ -30,10 +30,9 @@ import sys +from types import ModuleType from warnings import warn -ModuleType = type(sys) - # todo: only warn once diff --git a/boltons/funcutils.py b/boltons/funcutils.py index 5130f809..0bdc28e9 100644 --- a/boltons/funcutils.py +++ b/boltons/funcutils.py @@ -40,10 +40,11 @@ import functools import itertools from inspect import formatannotation -from types import MethodType, FunctionType - -make_method = lambda desc, obj, obj_type: MethodType(desc, obj) +from types import FunctionType, MethodType +# For legacy compatibility. +# boltons used to offer an implementation of total_ordering for Python <2.7 +from functools import total_ordering as total_ordering try: from .typeutils import make_sentinel @@ -257,7 +258,7 @@ def __partialmethod__(self): return functools.partialmethod(self.func, *self.args, **self.keywords) def __get__(self, obj, obj_type): - return make_method(self, obj, obj_type) + return MethodType(self, obj) @@ -293,13 +294,13 @@ def __get__(self, obj, obj_type): name = self.__name__ if obj is None: - return make_method(self, obj, obj_type) + return MethodType(self, obj) try: # since this is a data descriptor, this block # is probably only hit once (per object) return obj.__dict__[name] except KeyError: - obj.__dict__[name] = ret = make_method(self, obj, obj_type) + obj.__dict__[name] = ret = MethodType(self, obj) return ret @@ -981,9 +982,6 @@ def _indent(text, margin, newline='\n', key=bool): return newline.join(indented_lines) -from functools import total_ordering - - def noop(*args, **kwargs): """ Simple function that should be used when no effect is desired. diff --git a/boltons/mathutils.py b/boltons/mathutils.py index a325d19f..1367a541 100644 --- a/boltons/mathutils.py +++ b/boltons/mathutils.py @@ -121,15 +121,6 @@ def floor(x, options=None): return options[i - 1] -try: - _int_types = (int, long) - bytes = str -except NameError: - # py3 has no long - _int_types = (int,) - unicode = str - - class Bits: ''' An immutable bit-string or bit-array object. @@ -147,12 +138,12 @@ class Bits: __slots__ = ('val', 'len') def __init__(self, val=0, len_=None): - if type(val) not in _int_types: + if type(val) is not int: if type(val) is list: val = ''.join(['1' if e else '0' for e in val]) if type(val) is bytes: val = val.decode('ascii') - if type(val) is unicode: + if type(val) is str: if len_ is None: len_ = len(val) if val.startswith('0x'): @@ -164,7 +155,7 @@ def __init__(self, val=0, len_=None): val = int(val, 2) else: val = 0 - if type(val) not in _int_types: + if type(val) is not int: raise TypeError(f'initialized with bad type: {type(val).__name__}') if val < 0: raise ValueError('Bits cannot represent negative values') diff --git a/boltons/timeutils.py b/boltons/timeutils.py index b104b01c..8fcbb73a 100644 --- a/boltons/timeutils.py +++ b/boltons/timeutils.py @@ -57,10 +57,9 @@ from datetime import tzinfo, timedelta, date, datetime, timezone -def total_seconds(td): - # For legacy compatibility. - # boltons used to offer an implementation of total_seconds for Python <2.7 - return timedelta.total_seconds(td) +# For legacy compatibility. +# boltons used to offer an implementation of total_seconds for Python <2.7 +total_seconds = timedelta.total_seconds def dt_to_timestamp(dt): diff --git a/boltons/urlutils.py b/boltons/urlutils.py index d509f983..45d3e73e 100644 --- a/boltons/urlutils.py +++ b/boltons/urlutils.py @@ -55,8 +55,6 @@ import string from unicodedata import normalize -unicode = str - # The unreserved URI characters (per RFC 3986 Section 2.3) _UNRESERVED_CHARS = frozenset('~-._0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz') @@ -124,9 +122,9 @@ class URLParseError(ValueError): def to_unicode(obj): try: - return unicode(obj) + return str(obj) except UnicodeDecodeError: - return unicode(obj, encoding=DEFAULT_ENCODING) + return str(obj, encoding=DEFAULT_ENCODING) # regex from gruber via tornado @@ -174,7 +172,7 @@ def find_all_links(text, with_text=False, default_scheme='https', schemes=()): _add = ret.append def _add_text(t): - if ret and isinstance(ret[-1], unicode): + if ret and isinstance(ret[-1], str): ret[-1] += t else: _add(t) @@ -311,7 +309,7 @@ def unquote_to_bytes(string): # Is it a string-like object? string.split return b'' - if isinstance(string, unicode): + if isinstance(string, str): string = string.encode('utf-8') bits = string.split(b'%') if len(bits) == 1: @@ -741,7 +739,7 @@ def get_authority(self, full_quote=False, with_userinfo=False): # TODO: 0 port? if self.port and self.port != self.default_port: _add(':') - _add(unicode(self.port)) + _add(str(self.port)) return ''.join(parts) def to_text(self, full_quote=False): @@ -903,7 +901,7 @@ def parse_url(url_text): >>> sorted(res.keys()) # res is a basic dictionary ['_netloc_sep', 'authority', 'family', 'fragment', 'host', 'password', 'path', 'port', 'query', 'scheme', 'username'] """ - url_text = unicode(url_text) + url_text = str(url_text) # raise TypeError('parse_url expected text, not %r' % url_str) um = _URL_RE.match(url_text) try: