diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-01-06 01:14:26 -0500 |
|---|---|---|
| committer | mike bayer <mike_mp@zzzcomputing.com> | 2019-01-06 17:34:50 +0000 |
| commit | 1e1a38e7801f410f244e4bbb44ec795ae152e04e (patch) | |
| tree | 28e725c5c8188bd0cfd133d1e268dbca9b524978 /lib/sqlalchemy/util | |
| parent | 404e69426b05a82d905cbb3ad33adafccddb00dd (diff) | |
| download | sqlalchemy-1e1a38e7801f410f244e4bbb44ec795ae152e04e.tar.gz | |
Run black -l 79 against all source files
This is a straight reformat run using black as is, with no edits
applied at all.
The black run will format code consistently, however in
some cases that are prevalent in SQLAlchemy code it produces
too-long lines. The too-long lines will be resolved in the
following commit that will resolve all remaining flake8 issues
including shadowed builtins, long lines, import order, unused
imports, duplicate imports, and docstring issues.
Change-Id: I7eda77fed3d8e73df84b3651fd6cfcfe858d4dc9
Diffstat (limited to 'lib/sqlalchemy/util')
| -rw-r--r-- | lib/sqlalchemy/util/__init__.py | 170 | ||||
| -rw-r--r-- | lib/sqlalchemy/util/_collections.py | 122 | ||||
| -rw-r--r-- | lib/sqlalchemy/util/compat.py | 100 | ||||
| -rw-r--r-- | lib/sqlalchemy/util/deprecations.py | 35 | ||||
| -rw-r--r-- | lib/sqlalchemy/util/langhelpers.py | 423 | ||||
| -rw-r--r-- | lib/sqlalchemy/util/queue.py | 2 | ||||
| -rw-r--r-- | lib/sqlalchemy/util/topological.py | 12 |
7 files changed, 546 insertions, 318 deletions
diff --git a/lib/sqlalchemy/util/__init__.py b/lib/sqlalchemy/util/__init__.py index d8c28d6af..103225e2a 100644 --- a/lib/sqlalchemy/util/__init__.py +++ b/lib/sqlalchemy/util/__init__.py @@ -5,42 +5,146 @@ # This module is part of SQLAlchemy and is released under # the MIT License: http://www.opensource.org/licenses/mit-license.php -from .compat import callable, cmp, reduce, \ - threading, py3k, py33, py36, py2k, jython, pypy, cpython, win32, \ - pickle, dottedgetter, parse_qsl, namedtuple, next, reraise, \ - raise_from_cause, text_type, safe_kwarg, string_types, int_types, \ - binary_type, nested, \ - quote_plus, with_metaclass, print_, itertools_filterfalse, u, ue, b,\ - unquote_plus, unquote, b64decode, b64encode, byte_buffer, itertools_filter,\ - iterbytes, StringIO, inspect_getargspec, zip_longest +from .compat import ( + callable, + cmp, + reduce, + threading, + py3k, + py33, + py36, + py2k, + jython, + pypy, + cpython, + win32, + pickle, + dottedgetter, + parse_qsl, + namedtuple, + next, + reraise, + raise_from_cause, + text_type, + safe_kwarg, + string_types, + int_types, + binary_type, + nested, + quote_plus, + with_metaclass, + print_, + itertools_filterfalse, + u, + ue, + b, + unquote_plus, + unquote, + b64decode, + b64encode, + byte_buffer, + itertools_filter, + iterbytes, + StringIO, + inspect_getargspec, + zip_longest, +) -from ._collections import KeyedTuple, ImmutableContainer, immutabledict, \ - Properties, OrderedProperties, ImmutableProperties, OrderedDict, \ - OrderedSet, IdentitySet, OrderedIdentitySet, column_set, \ - column_dict, ordered_column_set, populate_column_dict, unique_list, \ - UniqueAppender, PopulateDict, EMPTY_SET, to_list, to_set, \ - to_column_set, update_copy, flatten_iterator, has_intersection, \ - LRUCache, ScopedRegistry, ThreadLocalRegistry, WeakSequence, \ - coerce_generator_arg, lightweight_named_tuple, collections_abc, \ - has_dupes +from ._collections import ( + KeyedTuple, + ImmutableContainer, + immutabledict, + Properties, + OrderedProperties, + ImmutableProperties, + OrderedDict, + OrderedSet, + IdentitySet, + OrderedIdentitySet, + column_set, + column_dict, + ordered_column_set, + populate_column_dict, + unique_list, + UniqueAppender, + PopulateDict, + EMPTY_SET, + to_list, + to_set, + to_column_set, + update_copy, + flatten_iterator, + has_intersection, + LRUCache, + ScopedRegistry, + ThreadLocalRegistry, + WeakSequence, + coerce_generator_arg, + lightweight_named_tuple, + collections_abc, + has_dupes, +) -from .langhelpers import iterate_attributes, class_hierarchy, \ - portable_instancemethod, unbound_method_to_callable, \ - getargspec_init, format_argspec_init, format_argspec_plus, \ - get_func_kwargs, get_cls_kwargs, decorator, as_interface, \ - memoized_property, memoized_instancemethod, md5_hex, \ - group_expirable_memoized_property, dependencies, decode_slice, \ - monkeypatch_proxied_specials, asbool, bool_or_str, coerce_kw_type,\ - duck_type_collection, assert_arg_type, symbol, dictlike_iteritems,\ - classproperty, set_creation_order, warn_exception, warn, NoneType,\ - constructor_copy, methods_equivalent, chop_traceback, asint,\ - generic_repr, counter, PluginLoader, hybridproperty, hybridmethod, \ - safe_reraise, quoted_token_parser,\ - get_callable_argspec, only_once, attrsetter, ellipses_string, \ - warn_limited, map_bits, MemoizedSlots, EnsureKWArgType, wrap_callable +from .langhelpers import ( + iterate_attributes, + class_hierarchy, + portable_instancemethod, + unbound_method_to_callable, + getargspec_init, + format_argspec_init, + format_argspec_plus, + get_func_kwargs, + get_cls_kwargs, + decorator, + as_interface, + memoized_property, + memoized_instancemethod, + md5_hex, + group_expirable_memoized_property, + dependencies, + decode_slice, + monkeypatch_proxied_specials, + asbool, + bool_or_str, + coerce_kw_type, + duck_type_collection, + assert_arg_type, + symbol, + dictlike_iteritems, + classproperty, + set_creation_order, + warn_exception, + warn, + NoneType, + constructor_copy, + methods_equivalent, + chop_traceback, + asint, + generic_repr, + counter, + PluginLoader, + hybridproperty, + hybridmethod, + safe_reraise, + quoted_token_parser, + get_callable_argspec, + only_once, + attrsetter, + ellipses_string, + warn_limited, + map_bits, + MemoizedSlots, + EnsureKWArgType, + wrap_callable, +) -from .deprecations import warn_deprecated, warn_pending_deprecation, \ - deprecated, pending_deprecation, inject_docstring_text +from .deprecations import ( + warn_deprecated, + warn_pending_deprecation, + deprecated, + pending_deprecation, + inject_docstring_text, +) # things that used to be not always available, # but are now as of current support Python versions diff --git a/lib/sqlalchemy/util/_collections.py b/lib/sqlalchemy/util/_collections.py index 43440134a..67be0e6bf 100644 --- a/lib/sqlalchemy/util/_collections.py +++ b/lib/sqlalchemy/util/_collections.py @@ -10,8 +10,13 @@ from __future__ import absolute_import import weakref import operator -from .compat import threading, itertools_filterfalse, string_types, \ - binary_types, collections_abc +from .compat import ( + threading, + itertools_filterfalse, + string_types, + binary_types, + collections_abc, +) from . import py2k import types @@ -77,7 +82,7 @@ class KeyedTuple(AbstractKeyedTuple): t.__dict__.update(zip(labels, vals)) else: labels = [] - t.__dict__['_labels'] = labels + t.__dict__["_labels"] = labels return t @property @@ -139,8 +144,7 @@ class ImmutableContainer(object): class immutabledict(ImmutableContainer, dict): - clear = pop = popitem = setdefault = \ - update = ImmutableContainer._immutable + clear = pop = popitem = setdefault = update = ImmutableContainer._immutable def __new__(cls, *args): new = dict.__new__(cls) @@ -151,7 +155,7 @@ class immutabledict(ImmutableContainer, dict): pass def __reduce__(self): - return immutabledict, (dict(self), ) + return immutabledict, (dict(self),) def union(self, d): if not d: @@ -173,10 +177,10 @@ class immutabledict(ImmutableContainer, dict): class Properties(object): """Provide a __getattr__/__setattr__ interface over a dict.""" - __slots__ = '_data', + __slots__ = ("_data",) def __init__(self, data): - object.__setattr__(self, '_data', data) + object.__setattr__(self, "_data", data) def __len__(self): return len(self._data) @@ -185,7 +189,9 @@ class Properties(object): return iter(list(self._data.values())) def __dir__(self): - return dir(super(Properties, self)) + [str(k) for k in self._data.keys()] + return dir(super(Properties, self)) + [ + str(k) for k in self._data.keys() + ] def __add__(self, other): return list(self) + list(other) @@ -203,10 +209,10 @@ class Properties(object): self._data[key] = obj def __getstate__(self): - return {'_data': self._data} + return {"_data": self._data} def __setstate__(self, state): - object.__setattr__(self, '_data', state['_data']) + object.__setattr__(self, "_data", state["_data"]) def __getattr__(self, key): try: @@ -266,7 +272,7 @@ class ImmutableProperties(ImmutableContainer, Properties): class OrderedDict(dict): """A dict that returns keys/values/items in the order they were added.""" - __slots__ = '_list', + __slots__ = ("_list",) def __reduce__(self): return OrderedDict, (self.items(),) @@ -294,7 +300,7 @@ class OrderedDict(dict): def update(self, ____sequence=None, **kwargs): if ____sequence is not None: - if hasattr(____sequence, 'keys'): + if hasattr(____sequence, "keys"): for key in ____sequence.keys(): self.__setitem__(key, ____sequence[key]) else: @@ -323,6 +329,7 @@ class OrderedDict(dict): return [(key, self[key]) for key in self._list] if py2k: + def itervalues(self): return iter(self.values()) @@ -402,7 +409,7 @@ class OrderedSet(set): return self.union(other) def __repr__(self): - return '%s(%r)' % (self.__class__.__name__, self._list) + return "%s(%r)" % (self.__class__.__name__, self._list) __str__ = __repr__ @@ -502,13 +509,13 @@ class IdentitySet(object): pair = self._members.popitem() return pair[1] except KeyError: - raise KeyError('pop from an empty set') + raise KeyError("pop from an empty set") def clear(self): self._members.clear() def __cmp__(self, other): - raise TypeError('cannot compare sets using cmp()') + raise TypeError("cannot compare sets using cmp()") def __eq__(self, other): if isinstance(other, IdentitySet): @@ -527,8 +534,9 @@ class IdentitySet(object): if len(self) > len(other): return False - for m in itertools_filterfalse(other._members.__contains__, - iter(self._members.keys())): + for m in itertools_filterfalse( + other._members.__contains__, iter(self._members.keys()) + ): return False return True @@ -548,8 +556,9 @@ class IdentitySet(object): if len(self) < len(other): return False - for m in itertools_filterfalse(self._members.__contains__, - iter(other._members.keys())): + for m in itertools_filterfalse( + self._members.__contains__, iter(other._members.keys()) + ): return False return True @@ -635,7 +644,8 @@ class IdentitySet(object): members = self._member_id_tuples() other = _iter_id(iterable) result._members.update( - self._working_set(members).symmetric_difference(other)) + self._working_set(members).symmetric_difference(other) + ) return result def _member_id_tuples(self): @@ -667,10 +677,10 @@ class IdentitySet(object): return iter(self._members.values()) def __hash__(self): - raise TypeError('set objects are unhashable') + raise TypeError("set objects are unhashable") def __repr__(self): - return '%s(%r)' % (type(self).__name__, list(self._members.values())) + return "%s(%r)" % (type(self).__name__, list(self._members.values())) class WeakSequence(object): @@ -689,8 +699,9 @@ class WeakSequence(object): return len(self._storage) def __iter__(self): - return (obj for obj in - (ref() for ref in self._storage) if obj is not None) + return ( + obj for obj in (ref() for ref in self._storage) if obj is not None + ) def __getitem__(self, index): try: @@ -732,6 +743,7 @@ class PopulateDict(dict): self[key] = val = self.creator(key) return val + # Define collections that are capable of storing # ColumnElement objects as hashable keys/elements. # At this point, these are mostly historical, things @@ -745,20 +757,21 @@ populate_column_dict = PopulateDict _getters = PopulateDict(operator.itemgetter) _property_getters = PopulateDict( - lambda idx: property(operator.itemgetter(idx))) + lambda idx: property(operator.itemgetter(idx)) +) def unique_list(seq, hashfunc=None): seen = set() seen_add = seen.add if not hashfunc: - return [x for x in seq - if x not in seen - and not seen_add(x)] + return [x for x in seq if x not in seen and not seen_add(x)] else: - return [x for x in seq - if hashfunc(x) not in seen - and not seen_add(hashfunc(x))] + return [ + x + for x in seq + if hashfunc(x) not in seen and not seen_add(hashfunc(x)) + ] class UniqueAppender(object): @@ -773,9 +786,9 @@ class UniqueAppender(object): self._unique = {} if via: self._data_appender = getattr(data, via) - elif hasattr(data, 'append'): + elif hasattr(data, "append"): self._data_appender = data.append - elif hasattr(data, 'add'): + elif hasattr(data, "add"): self._data_appender = data.add def append(self, item): @@ -798,8 +811,9 @@ def coerce_generator_arg(arg): def to_list(x, default=None): if x is None: return default - if not isinstance(x, collections_abc.Iterable) or \ - isinstance(x, string_types + binary_types): + if not isinstance(x, collections_abc.Iterable) or isinstance( + x, string_types + binary_types + ): return [x] elif isinstance(x, list): return x @@ -815,9 +829,7 @@ def has_intersection(set_, iterable): """ # TODO: optimize, write in C, etc. - return bool( - set_.intersection([i for i in iterable if i.__hash__]) - ) + return bool(set_.intersection([i for i in iterable if i.__hash__])) def to_set(x): @@ -854,7 +866,7 @@ def flatten_iterator(x): """ for elem in x: - if not isinstance(elem, str) and hasattr(elem, '__iter__'): + if not isinstance(elem, str) and hasattr(elem, "__iter__"): for y in flatten_iterator(elem): yield y else: @@ -871,9 +883,9 @@ class LRUCache(dict): """ - __slots__ = 'capacity', 'threshold', 'size_alert', '_counter', '_mutex' + __slots__ = "capacity", "threshold", "size_alert", "_counter", "_mutex" - def __init__(self, capacity=100, threshold=.5, size_alert=None): + def __init__(self, capacity=100, threshold=0.5, size_alert=None): self.capacity = capacity self.threshold = threshold self.size_alert = size_alert @@ -929,10 +941,10 @@ class LRUCache(dict): if size_alert: size_alert = False self.size_alert(self) - by_counter = sorted(dict.values(self), - key=operator.itemgetter(2), - reverse=True) - for item in by_counter[self.capacity:]: + by_counter = sorted( + dict.values(self), key=operator.itemgetter(2), reverse=True + ) + for item in by_counter[self.capacity :]: try: del self[item[0]] except KeyError: @@ -946,17 +958,22 @@ _lw_tuples = LRUCache(100) def lightweight_named_tuple(name, fields): - hash_ = (name, ) + tuple(fields) + hash_ = (name,) + tuple(fields) tp_cls = _lw_tuples.get(hash_) if tp_cls: return tp_cls tp_cls = type( - name, (_LW,), - dict([ - (field, _property_getters[idx]) - for idx, field in enumerate(fields) if field is not None - ] + [('__slots__', ())]) + name, + (_LW,), + dict( + [ + (field, _property_getters[idx]) + for idx, field in enumerate(fields) + if field is not None + ] + + [("__slots__", ())] + ), ) tp_cls._real_fields = fields @@ -1077,6 +1094,7 @@ def has_dupes(sequence, target): return True return False + # .index version. the two __contains__ calls as well # as .index() and isinstance() slow this down. # def has_dupes(sequence, target): diff --git a/lib/sqlalchemy/util/compat.py b/lib/sqlalchemy/util/compat.py index b01471edf..553624b49 100644 --- a/lib/sqlalchemy/util/compat.py +++ b/lib/sqlalchemy/util/compat.py @@ -20,9 +20,9 @@ py32 = sys.version_info >= (3, 2) py3k = sys.version_info >= (3, 0) py2k = sys.version_info < (3, 0) py265 = sys.version_info >= (2, 6, 5) -jython = sys.platform.startswith('java') -pypy = hasattr(sys, 'pypy_version_info') -win32 = sys.platform.startswith('win') +jython = sys.platform.startswith("java") +pypy = hasattr(sys, "pypy_version_info") +win32 = sys.platform.startswith("win") cpython = not pypy and not jython # TODO: something better for this ? contextmanager = contextlib.contextmanager @@ -30,8 +30,9 @@ dottedgetter = operator.attrgetter namedtuple = collections.namedtuple next = next -ArgSpec = collections.namedtuple("ArgSpec", - ["args", "varargs", "keywords", "defaults"]) +ArgSpec = collections.namedtuple( + "ArgSpec", ["args", "varargs", "keywords", "defaults"] +) try: import threading @@ -58,40 +59,43 @@ if py3k: from io import BytesIO as byte_buffer from io import StringIO from itertools import zip_longest - from urllib.parse import (quote_plus, unquote_plus, parse_qsl, quote, unquote) - - string_types = str, - binary_types = bytes, + from urllib.parse import ( + quote_plus, + unquote_plus, + parse_qsl, + quote, + unquote, + ) + + string_types = (str,) + binary_types = (bytes,) binary_type = bytes text_type = str - int_types = int, + int_types = (int,) iterbytes = iter itertools_filterfalse = itertools.filterfalse itertools_filter = filter itertools_imap = map - exec_ = getattr(builtins, 'exec') - import_ = getattr(builtins, '__import__') + exec_ = getattr(builtins, "exec") + import_ = getattr(builtins, "__import__") print_ = getattr(builtins, "print") def b(s): return s.encode("latin-1") def b64decode(x): - return base64.b64decode(x.encode('ascii')) - + return base64.b64decode(x.encode("ascii")) def b64encode(x): - return base64.b64encode(x).decode('ascii') + return base64.b64encode(x).decode("ascii") def cmp(a, b): return (a > b) - (a < b) def inspect_getargspec(func): - return ArgSpec( - *inspect_getfullargspec(func)[0:4] - ) + return ArgSpec(*inspect_getfullargspec(func)[0:4]) def reraise(tp, value, tb=None, cause=None): if cause is not None: @@ -110,8 +114,11 @@ if py3k: if py32: callable = callable else: + def callable(fn): - return hasattr(fn, '__call__') + return hasattr(fn, "__call__") + + else: import base64 import ConfigParser as configparser @@ -129,8 +136,8 @@ else: except ImportError: import pickle - string_types = basestring, - binary_types = bytes, + string_types = (basestring,) + binary_types = (bytes,) binary_type = str text_type = unicode int_types = int, long @@ -153,9 +160,9 @@ else: def exec_(func_text, globals_, lcl=None): if lcl is None: - exec('exec func_text in globals_') + exec("exec func_text in globals_") else: - exec('exec func_text in globals_, lcl') + exec("exec func_text in globals_, lcl") def iterbytes(buf): return (ord(byte) for byte in buf) @@ -186,24 +193,32 @@ else: # not as nice as that of Py3K, but at least preserves # the code line where the issue occurred - exec("def reraise(tp, value, tb=None, cause=None):\n" - " if cause is not None:\n" - " assert cause is not value, 'Same cause emitted'\n" - " raise tp, value, tb\n") + exec( + "def reraise(tp, value, tb=None, cause=None):\n" + " if cause is not None:\n" + " assert cause is not value, 'Same cause emitted'\n" + " raise tp, value, tb\n" + ) if py35: from inspect import formatannotation def inspect_formatargspec( - args, varargs=None, varkw=None, defaults=None, - kwonlyargs=(), kwonlydefaults={}, annotations={}, - formatarg=str, - formatvarargs=lambda name: '*' + name, - formatvarkw=lambda name: '**' + name, - formatvalue=lambda value: '=' + repr(value), - formatreturns=lambda text: ' -> ' + text, - formatannotation=formatannotation): + args, + varargs=None, + varkw=None, + defaults=None, + kwonlyargs=(), + kwonlydefaults={}, + annotations={}, + formatarg=str, + formatvarargs=lambda name: "*" + name, + formatvarkw=lambda name: "**" + name, + formatvalue=lambda value: "=" + repr(value), + formatreturns=lambda text: " -> " + text, + formatannotation=formatannotation, + ): """Copy formatargspec from python 3.7 standard library. Python 3 has deprecated formatargspec and requested that Signature @@ -221,7 +236,7 @@ if py35: def formatargandannotation(arg): result = formatarg(arg) if arg in annotations: - result += ': ' + formatannotation(annotations[arg]) + result += ": " + formatannotation(annotations[arg]) return result specs = [] @@ -237,7 +252,7 @@ if py35: specs.append(formatvarargs(formatargandannotation(varargs))) else: if kwonlyargs: - specs.append('*') + specs.append("*") if kwonlyargs: for kwonlyarg in kwonlyargs: @@ -249,10 +264,12 @@ if py35: if varkw is not None: specs.append(formatvarkw(formatargandannotation(varkw))) - result = '(' + ', '.join(specs) + ')' - if 'return' in annotations: - result += formatreturns(formatannotation(annotations['return'])) + result = "(" + ", ".join(specs) + ")" + if "return" in annotations: + result += formatreturns(formatannotation(annotations["return"])) return result + + else: from inspect import formatargspec as inspect_formatargspec @@ -330,4 +347,5 @@ def with_metaclass(meta, *bases): if this_bases is None: return type.__new__(cls, name, (), d) return meta(name, bases, d) - return metaclass('temporary_class', None, {}) + + return metaclass("temporary_class", None, {}) diff --git a/lib/sqlalchemy/util/deprecations.py b/lib/sqlalchemy/util/deprecations.py index 9000cc795..e6612f075 100644 --- a/lib/sqlalchemy/util/deprecations.py +++ b/lib/sqlalchemy/util/deprecations.py @@ -40,8 +40,7 @@ def deprecated(version, message=None, add_deprecation_to_docstring=True): """ if add_deprecation_to_docstring: - header = ".. deprecated:: %s %s" % \ - (version, (message or '')) + header = ".. deprecated:: %s %s" % (version, (message or "")) else: header = None @@ -50,13 +49,18 @@ def deprecated(version, message=None, add_deprecation_to_docstring=True): def decorate(fn): return _decorate_with_warning( - fn, exc.SADeprecationWarning, - message % dict(func=fn.__name__), header) + fn, + exc.SADeprecationWarning, + message % dict(func=fn.__name__), + header, + ) + return decorate -def pending_deprecation(version, message=None, - add_deprecation_to_docstring=True): +def pending_deprecation( + version, message=None, add_deprecation_to_docstring=True +): """Decorates a function and issues a pending deprecation warning on use. :param version: @@ -74,8 +78,7 @@ def pending_deprecation(version, message=None, """ if add_deprecation_to_docstring: - header = ".. deprecated:: %s (pending) %s" % \ - (version, (message or '')) + header = ".. deprecated:: %s (pending) %s" % (version, (message or "")) else: header = None @@ -84,8 +87,12 @@ def pending_deprecation(version, message=None, def decorate(fn): return _decorate_with_warning( - fn, exc.SAPendingDeprecationWarning, - message % dict(func=fn.__name__), header) + fn, + exc.SAPendingDeprecationWarning, + message % dict(func=fn.__name__), + header, + ) + return decorate @@ -95,7 +102,8 @@ def _sanitize_restructured_text(text): if type_ in ("func", "meth"): name += "()" return name - return re.sub(r'\:(\w+)\:`~?\.?(.+?)`', repl, text) + + return re.sub(r"\:(\w+)\:`~?\.?(.+?)`", repl, text) def _decorate_with_warning(func, wtype, message, docstring_header=None): @@ -108,7 +116,7 @@ def _decorate_with_warning(func, wtype, message, docstring_header=None): warnings.warn(message, wtype, stacklevel=3) return fn(*args, **kwargs) - doc = func.__doc__ is not None and func.__doc__ or '' + doc = func.__doc__ is not None and func.__doc__ or "" if docstring_header is not None: docstring_header %= dict(func=func.__name__) @@ -118,6 +126,7 @@ def _decorate_with_warning(func, wtype, message, docstring_header=None): decorated.__doc__ = doc return decorated + import textwrap @@ -135,7 +144,7 @@ def _dedent_docstring(text): def inject_docstring_text(doctext, injecttext, pos): doctext = _dedent_docstring(doctext or "") - lines = doctext.split('\n') + lines = doctext.split("\n") injectlines = textwrap.dedent(injecttext).split("\n") if injectlines[0]: injectlines.insert(0, "") diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py index 7e387f4f2..6a286998b 100644 --- a/lib/sqlalchemy/util/langhelpers.py +++ b/lib/sqlalchemy/util/langhelpers.py @@ -25,7 +25,7 @@ from . import _collections def md5_hex(x): if compat.py3k: - x = x.encode('utf-8') + x = x.encode("utf-8") m = hashlib.md5() m.update(x) return m.hexdigest() @@ -49,7 +49,7 @@ class safe_reraise(object): """ - __slots__ = ('warn_only', '_exc_info') + __slots__ = ("warn_only", "_exc_info") def __init__(self, warn_only=False): self.warn_only = warn_only @@ -61,7 +61,7 @@ class safe_reraise(object): # see #2703 for notes if type_ is None: exc_type, exc_value, exc_tb = self._exc_info - self._exc_info = None # remove potential circular references + self._exc_info = None # remove potential circular references if not self.warn_only: compat.reraise(exc_type, exc_value, exc_tb) else: @@ -71,8 +71,9 @@ class safe_reraise(object): warn( "An exception has occurred during handling of a " "previous exception. The previous exception " - "is:\n %s %s\n" % (self._exc_info[0], self._exc_info[1])) - self._exc_info = None # remove potential circular references + "is:\n %s %s\n" % (self._exc_info[0], self._exc_info[1]) + ) + self._exc_info = None # remove potential circular references compat.reraise(type_, value, traceback) @@ -84,7 +85,7 @@ def decode_slice(slc): """ ret = [] for x in slc.start, slc.stop, slc.step: - if hasattr(x, '__index__'): + if hasattr(x, "__index__"): x = x.__index__() ret.append(x) return tuple(ret) @@ -93,9 +94,10 @@ def decode_slice(slc): def _unique_symbols(used, *bases): used = set(used) for base in bases: - pool = itertools.chain((base,), - compat.itertools_imap(lambda i: base + str(i), - range(1000))) + pool = itertools.chain( + (base,), + compat.itertools_imap(lambda i: base + str(i), range(1000)), + ) for sym in pool: if sym not in used: used.add(sym) @@ -122,21 +124,25 @@ def decorator(target): raise Exception("not a decoratable function") spec = compat.inspect_getfullargspec(fn) names = tuple(spec[0]) + spec[1:3] + (fn.__name__,) - targ_name, fn_name = _unique_symbols(names, 'target', 'fn') + targ_name, fn_name = _unique_symbols(names, "target", "fn") metadata = dict(target=targ_name, fn=fn_name) metadata.update(format_argspec_plus(spec, grouped=False)) - metadata['name'] = fn.__name__ - code = """\ + metadata["name"] = fn.__name__ + code = ( + """\ def %(name)s(%(args)s): return %(target)s(%(fn)s, %(apply_kw)s) -""" % metadata - decorated = _exec_code_in_env(code, - {targ_name: target, fn_name: fn}, - fn.__name__) - decorated.__defaults__ = getattr(fn, 'im_func', fn).__defaults__ +""" + % metadata + ) + decorated = _exec_code_in_env( + code, {targ_name: target, fn_name: fn}, fn.__name__ + ) + decorated.__defaults__ = getattr(fn, "im_func", fn).__defaults__ decorated.__wrapped__ = fn return update_wrapper(decorated, fn) + return update_wrapper(decorate, target) @@ -155,31 +161,38 @@ def public_factory(target, location): if isinstance(target, type): fn = target.__init__ callable_ = target - doc = "Construct a new :class:`.%s` object. \n\n"\ - "This constructor is mirrored as a public API function; "\ - "see :func:`~%s` "\ - "for a full usage and argument description." % ( - target.__name__, location, ) + doc = ( + "Construct a new :class:`.%s` object. \n\n" + "This constructor is mirrored as a public API function; " + "see :func:`~%s` " + "for a full usage and argument description." + % (target.__name__, location) + ) else: fn = callable_ = target - doc = "This function is mirrored; see :func:`~%s` "\ + doc = ( + "This function is mirrored; see :func:`~%s` " "for a description of arguments." % location + ) location_name = location.split(".")[-1] spec = compat.inspect_getfullargspec(fn) del spec[0][0] metadata = format_argspec_plus(spec, grouped=False) - metadata['name'] = location_name - code = """\ + metadata["name"] = location_name + code = ( + """\ def %(name)s(%(args)s): return cls(%(apply_kw)s) -""" % metadata - env = {'cls': callable_, 'symbol': symbol} +""" + % metadata + ) + env = {"cls": callable_, "symbol": symbol} exec(code, env) decorated = env[location_name] decorated.__doc__ = fn.__doc__ decorated.__module__ = "sqlalchemy" + location.rsplit(".", 1)[0] - if compat.py2k or hasattr(fn, '__func__'): + if compat.py2k or hasattr(fn, "__func__"): fn.__func__.__doc__ = doc else: fn.__doc__ = doc @@ -187,7 +200,6 @@ def %(name)s(%(args)s): class PluginLoader(object): - def __init__(self, group, auto_fn=None): self.group = group self.impls = {} @@ -211,14 +223,13 @@ class PluginLoader(object): except ImportError: pass else: - for impl in pkg_resources.iter_entry_points( - self.group, name): + for impl in pkg_resources.iter_entry_points(self.group, name): self.impls[name] = impl.load return impl.load() raise exc.NoSuchModuleError( - "Can't load plugin: %s:%s" % - (self.group, name)) + "Can't load plugin: %s:%s" % (self.group, name) + ) def register(self, name, modulepath, objname): def load(): @@ -226,6 +237,7 @@ class PluginLoader(object): for token in modulepath.split(".")[1:]: mod = getattr(mod, token) return getattr(mod, objname) + self.impls[name] = load @@ -245,10 +257,13 @@ def get_cls_kwargs(cls, _set=None): if toplevel: _set = set() - ctr = cls.__dict__.get('__init__', False) + ctr = cls.__dict__.get("__init__", False) - has_init = ctr and isinstance(ctr, types.FunctionType) and \ - isinstance(ctr.__code__, types.CodeType) + has_init = ( + ctr + and isinstance(ctr, types.FunctionType) + and isinstance(ctr.__code__, types.CodeType) + ) if has_init: names, has_kw = inspect_func_args(ctr) @@ -262,7 +277,7 @@ def get_cls_kwargs(cls, _set=None): if get_cls_kwargs(c, _set) is None: break - _set.discard('self') + _set.discard("self") return _set @@ -278,7 +293,9 @@ try: has_kw = bool(co.co_flags & CO_VARKEYWORDS) return args, has_kw + except ImportError: + def inspect_func_args(fn): names, _, has_kw, _ = compat.inspect_getargspec(fn) return names, bool(has_kw) @@ -309,23 +326,26 @@ def get_callable_argspec(fn, no_self=False, _is_init=False): elif inspect.isfunction(fn): if _is_init and no_self: spec = compat.inspect_getargspec(fn) - return compat.ArgSpec(spec.args[1:], spec.varargs, - spec.keywords, spec.defaults) + return compat.ArgSpec( + spec.args[1:], spec.varargs, spec.keywords, spec.defaults + ) else: return compat.inspect_getargspec(fn) elif inspect.ismethod(fn): if no_self and (_is_init or fn.__self__): spec = compat.inspect_getargspec(fn.__func__) - return compat.ArgSpec(spec.args[1:], spec.varargs, - spec.keywords, spec.defaults) + return compat.ArgSpec( + spec.args[1:], spec.varargs, spec.keywords, spec.defaults + ) else: return compat.inspect_getargspec(fn.__func__) elif inspect.isclass(fn): return get_callable_argspec( - fn.__init__, no_self=no_self, _is_init=True) - elif hasattr(fn, '__func__'): + fn.__init__, no_self=no_self, _is_init=True + ) + elif hasattr(fn, "__func__"): return compat.inspect_getargspec(fn.__func__) - elif hasattr(fn, '__call__'): + elif hasattr(fn, "__call__"): if inspect.ismethod(fn.__call__): return get_callable_argspec(fn.__call__, no_self=no_self) else: @@ -375,13 +395,14 @@ def format_argspec_plus(fn, grouped=True): if spec[0]: self_arg = spec[0][0] elif spec[1]: - self_arg = '%s[0]' % spec[1] + self_arg = "%s[0]" % spec[1] else: self_arg = None if compat.py3k: apply_pos = compat.inspect_formatargspec( - spec[0], spec[1], spec[2], None, spec[4]) + spec[0], spec[1], spec[2], None, spec[4] + ) num_defaults = 0 if spec[3]: num_defaults += len(spec[3]) @@ -396,19 +417,31 @@ def format_argspec_plus(fn, grouped=True): name_args = spec[0] if num_defaults: - defaulted_vals = name_args[0 - num_defaults:] + defaulted_vals = name_args[0 - num_defaults :] else: defaulted_vals = () apply_kw = compat.inspect_formatargspec( - name_args, spec[1], spec[2], defaulted_vals, - formatvalue=lambda x: '=' + x) + name_args, + spec[1], + spec[2], + defaulted_vals, + formatvalue=lambda x: "=" + x, + ) if grouped: - return dict(args=args, self_arg=self_arg, - apply_pos=apply_pos, apply_kw=apply_kw) + return dict( + args=args, + self_arg=self_arg, + apply_pos=apply_pos, + apply_kw=apply_kw, + ) else: - return dict(args=args[1:-1], self_arg=self_arg, - apply_pos=apply_pos[1:-1], apply_kw=apply_kw[1:-1]) + return dict( + args=args[1:-1], + self_arg=self_arg, + apply_pos=apply_pos[1:-1], + apply_kw=apply_kw[1:-1], + ) def format_argspec_init(method, grouped=True): @@ -422,14 +455,17 @@ def format_argspec_init(method, grouped=True): """ if method is object.__init__: - args = grouped and '(self)' or 'self' + args = grouped and "(self)" or "self" else: try: return format_argspec_plus(method, grouped=grouped) except TypeError: - args = (grouped and '(self, *args, **kwargs)' - or 'self, *args, **kwargs') - return dict(self_arg='self', args=args, apply_pos=args, apply_kw=args) + args = ( + grouped + and "(self, *args, **kwargs)" + or "self, *args, **kwargs" + ) + return dict(self_arg="self", args=args, apply_pos=args, apply_kw=args) def getargspec_init(method): @@ -445,9 +481,9 @@ def getargspec_init(method): return compat.inspect_getargspec(method) except TypeError: if method is object.__init__: - return (['self'], None, None, None) + return (["self"], None, None, None) else: - return (['self'], 'args', 'kwargs', None) + return (["self"], "args", "kwargs", None) def unbound_method_to_callable(func_or_cls): @@ -479,8 +515,9 @@ def generic_repr(obj, additional_kw=(), to_inspect=None, omit_kwarg=()): vargs = None for i, insp in enumerate(to_inspect): try: - (_args, _vargs, vkw, defaults) = \ - compat.inspect_getargspec(insp.__init__) + (_args, _vargs, vkw, defaults) = compat.inspect_getargspec( + insp.__init__ + ) except TypeError: continue else: @@ -493,16 +530,17 @@ def generic_repr(obj, additional_kw=(), to_inspect=None, omit_kwarg=()): else: pos_args.extend(_args[1:]) else: - kw_args.update([ - (arg, missing) for arg in _args[1:-default_len] - ]) + kw_args.update( + [(arg, missing) for arg in _args[1:-default_len]] + ) if default_len: - kw_args.update([ - (arg, default) - for arg, default - in zip(_args[-default_len:], defaults) - ]) + kw_args.update( + [ + (arg, default) + for arg, default in zip(_args[-default_len:], defaults) + ] + ) output = [] output.extend(repr(getattr(obj, arg, None)) for arg in pos_args) @@ -516,7 +554,7 @@ def generic_repr(obj, additional_kw=(), to_inspect=None, omit_kwarg=()): try: val = getattr(obj, arg, missing) if val is not missing and val != defval: - output.append('%s=%r' % (arg, val)) + output.append("%s=%r" % (arg, val)) except Exception: pass @@ -525,7 +563,7 @@ def generic_repr(obj, additional_kw=(), to_inspect=None, omit_kwarg=()): try: val = getattr(obj, arg, missing) if val is not missing and val != defval: - output.append('%s=%r' % (arg, val)) + output.append("%s=%r" % (arg, val)) except Exception: pass @@ -538,16 +576,19 @@ class portable_instancemethod(object): """ - __slots__ = 'target', 'name', 'kwargs', '__weakref__' + __slots__ = "target", "name", "kwargs", "__weakref__" def __getstate__(self): - return {'target': self.target, 'name': self.name, - 'kwargs': self.kwargs} + return { + "target": self.target, + "name": self.name, + "kwargs": self.kwargs, + } def __setstate__(self, state): - self.target = state['target'] - self.name = state['name'] - self.kwargs = state.get('kwargs', ()) + self.target = state["target"] + self.name = state["name"] + self.kwargs = state.get("kwargs", ()) def __init__(self, meth, kwargs=()): self.target = meth.__self__ @@ -583,8 +624,11 @@ def class_hierarchy(cls): if compat.py2k: if isinstance(c, types.ClassType): continue - bases = (_ for _ in c.__bases__ - if _ not in hier and not isinstance(_, types.ClassType)) + bases = ( + _ + for _ in c.__bases__ + if _ not in hier and not isinstance(_, types.ClassType) + ) else: bases = (_ for _ in c.__bases__ if _ not in hier) @@ -593,11 +637,12 @@ def class_hierarchy(cls): hier.add(b) if compat.py3k: - if c.__module__ == 'builtins' or not hasattr(c, '__subclasses__'): + if c.__module__ == "builtins" or not hasattr(c, "__subclasses__"): continue else: - if c.__module__ == '__builtin__' or not hasattr( - c, '__subclasses__'): + if c.__module__ == "__builtin__" or not hasattr( + c, "__subclasses__" + ): continue for s in [_ for _ in c.__subclasses__() if _ not in hier]: @@ -622,26 +667,45 @@ def iterate_attributes(cls): break -def monkeypatch_proxied_specials(into_cls, from_cls, skip=None, only=None, - name='self.proxy', from_instance=None): +def monkeypatch_proxied_specials( + into_cls, + from_cls, + skip=None, + only=None, + name="self.proxy", + from_instance=None, +): """Automates delegation of __specials__ for a proxying type.""" if only: dunders = only else: if skip is None: - skip = ('__slots__', '__del__', '__getattribute__', - '__metaclass__', '__getstate__', '__setstate__') - dunders = [m for m in dir(from_cls) - if (m.startswith('__') and m.endswith('__') and - not hasattr(into_cls, m) and m not in skip)] + skip = ( + "__slots__", + "__del__", + "__getattribute__", + "__metaclass__", + "__getstate__", + "__setstate__", + ) + dunders = [ + m + for m in dir(from_cls) + if ( + m.startswith("__") + and m.endswith("__") + and not hasattr(into_cls, m) + and m not in skip + ) + ] for method in dunders: try: fn = getattr(from_cls, method) - if not hasattr(fn, '__call__'): + if not hasattr(fn, "__call__"): continue - fn = getattr(fn, 'im_func', fn) + fn = getattr(fn, "im_func", fn) except AttributeError: continue try: @@ -649,11 +713,13 @@ def monkeypatch_proxied_specials(into_cls, from_cls, skip=None, only=None, fn_args = compat.inspect_formatargspec(spec[0]) d_args = compat.inspect_formatargspec(spec[0][1:]) except TypeError: - fn_args = '(self, *args, **kw)' - d_args = '(*args, **kw)' + fn_args = "(self, *args, **kw)" + d_args = "(*args, **kw)" - py = ("def %(method)s%(fn_args)s: " - "return %(name)s.%(method)s%(d_args)s" % locals()) + py = ( + "def %(method)s%(fn_args)s: " + "return %(name)s.%(method)s%(d_args)s" % locals() + ) env = from_instance is not None and {name: from_instance} or {} compat.exec_(py, env) @@ -667,8 +733,9 @@ def monkeypatch_proxied_specials(into_cls, from_cls, skip=None, only=None, def methods_equivalent(meth1, meth2): """Return True if the two methods are the same implementation.""" - return getattr(meth1, '__func__', meth1) is getattr( - meth2, '__func__', meth2) + return getattr(meth1, "__func__", meth1) is getattr( + meth2, "__func__", meth2 + ) def as_interface(obj, cls=None, methods=None, required=None): @@ -705,12 +772,12 @@ def as_interface(obj, cls=None, methods=None, required=None): """ if not cls and not methods: - raise TypeError('a class or collection of method names are required') + raise TypeError("a class or collection of method names are required") if isinstance(cls, type) and isinstance(obj, cls): return obj - interface = set(methods or [m for m in dir(cls) if not m.startswith('_')]) + interface = set(methods or [m for m in dir(cls) if not m.startswith("_")]) implemented = set(dir(obj)) complies = operator.ge @@ -727,15 +794,17 @@ def as_interface(obj, cls=None, methods=None, required=None): # No dict duck typing here. if not isinstance(obj, dict): - qualifier = complies is operator.gt and 'any of' or 'all of' - raise TypeError("%r does not implement %s: %s" % ( - obj, qualifier, ', '.join(interface))) + qualifier = complies is operator.gt and "any of" or "all of" + raise TypeError( + "%r does not implement %s: %s" + % (obj, qualifier, ", ".join(interface)) + ) class AnonymousInterface(object): """A callable-holding shell.""" if cls: - AnonymousInterface.__name__ = 'Anonymous' + cls.__name__ + AnonymousInterface.__name__ = "Anonymous" + cls.__name__ found = set() for method, impl in dictlike_iteritems(obj): @@ -749,8 +818,10 @@ def as_interface(obj, cls=None, methods=None, required=None): if complies(found, required): return AnonymousInterface - raise TypeError("dictionary does not contain required keys %s" % - ', '.join(required - found)) + raise TypeError( + "dictionary does not contain required keys %s" + % ", ".join(required - found) + ) class memoized_property(object): @@ -791,6 +862,7 @@ def memoized_instancemethod(fn): memo.__doc__ = fn.__doc__ self.__dict__[fn.__name__] = memo return result + return update_wrapper(oneshot, fn) @@ -831,14 +903,14 @@ class MemoizedSlots(object): raise AttributeError(key) def __getattr__(self, key): - if key.startswith('_memoized'): + if key.startswith("_memoized"): raise AttributeError(key) - elif hasattr(self, '_memoized_attr_%s' % key): - value = getattr(self, '_memoized_attr_%s' % key)() + elif hasattr(self, "_memoized_attr_%s" % key): + value = getattr(self, "_memoized_attr_%s" % key)() setattr(self, key, value) return value - elif hasattr(self, '_memoized_method_%s' % key): - fn = getattr(self, '_memoized_method_%s' % key) + elif hasattr(self, "_memoized_method_%s" % key): + fn = getattr(self, "_memoized_method_%s" % key) def oneshot(*args, **kw): result = fn(*args, **kw) @@ -847,6 +919,7 @@ class MemoizedSlots(object): memo.__doc__ = fn.__doc__ setattr(self, key, memo) return result + oneshot.__doc__ = fn.__doc__ return oneshot else: @@ -859,12 +932,14 @@ def dependency_for(modulename, add_to_all=False): # unfortunately importlib doesn't work that great either tokens = modulename.split(".") mod = compat.import_( - ".".join(tokens[0:-1]), globals(), locals(), [tokens[-1]]) + ".".join(tokens[0:-1]), globals(), locals(), [tokens[-1]] + ) mod = getattr(mod, tokens[-1]) setattr(mod, obj.__name__, obj) if add_to_all and hasattr(mod, "__all__"): mod.__all__.append(obj.__name__) return obj + return decorate @@ -891,10 +966,7 @@ class dependencies(object): for dep in deps: tokens = dep.split(".") self.import_deps.append( - dependencies._importlater( - ".".join(tokens[0:-1]), - tokens[-1] - ) + dependencies._importlater(".".join(tokens[0:-1]), tokens[-1]) ) def __call__(self, fn): @@ -902,7 +974,7 @@ class dependencies(object): spec = compat.inspect_getfullargspec(fn) spec_zero = list(spec[0]) - hasself = spec_zero[0] in ('self', 'cls') + hasself = spec_zero[0] in ("self", "cls") for i in range(len(import_deps)): spec[0][i + (1 if hasself else 0)] = "import_deps[%r]" % i @@ -915,13 +987,13 @@ class dependencies(object): outer_spec = format_argspec_plus(spec, grouped=False) - code = 'lambda %(args)s: fn(%(apply_kw)s)' % { - "args": outer_spec['args'], - "apply_kw": inner_spec['apply_kw'] + code = "lambda %(args)s: fn(%(apply_kw)s)" % { + "args": outer_spec["args"], + "apply_kw": inner_spec["apply_kw"], } decorated = eval(code, locals()) - decorated.__defaults__ = getattr(fn, 'im_func', fn).__defaults__ + decorated.__defaults__ = getattr(fn, "im_func", fn).__defaults__ return update_wrapper(decorated, fn) @classmethod @@ -961,26 +1033,27 @@ class dependencies(object): raise ImportError( "importlater.resolve_all() hasn't " "been called (this is %s %s)" - % (self._il_path, self._il_addtl)) + % (self._il_path, self._il_addtl) + ) return getattr(self._initial_import, self._il_addtl) def _resolve(self): dependencies._unresolved.discard(self) self._initial_import = compat.import_( - self._il_path, globals(), locals(), - [self._il_addtl]) + self._il_path, globals(), locals(), [self._il_addtl] + ) def __getattr__(self, key): - if key == 'module': - raise ImportError("Could not resolve module %s" - % self._full_path) + if key == "module": + raise ImportError( + "Could not resolve module %s" % self._full_path + ) try: attr = getattr(self.module, key) except AttributeError: raise AttributeError( - "Module %s has no attribute '%s'" % - (self._full_path, key) + "Module %s has no attribute '%s'" % (self._full_path, key) ) self.__dict__[key] = attr return attr @@ -990,9 +1063,9 @@ class dependencies(object): def asbool(obj): if isinstance(obj, compat.string_types): obj = obj.strip().lower() - if obj in ['true', 'yes', 'on', 'y', 't', '1']: + if obj in ["true", "yes", "on", "y", "t", "1"]: return True - elif obj in ['false', 'no', 'off', 'n', 'f', '0']: + elif obj in ["false", "no", "off", "n", "f", "0"]: return False else: raise ValueError("String is not true/false: %r" % obj) @@ -1004,11 +1077,13 @@ def bool_or_str(*text): boolean, or one of a set of "alternate" string values. """ + def bool_or_value(obj): if obj in text: return obj else: return asbool(obj) + return bool_or_value @@ -1026,9 +1101,11 @@ def coerce_kw_type(kw, key, type_, flexi_bool=True): when coercing to boolean. """ - if key in kw and ( - not isinstance(type_, type) or not isinstance(kw[key], type_) - ) and kw[key] is not None: + if ( + key in kw + and (not isinstance(type_, type) or not isinstance(kw[key], type_)) + and kw[key] is not None + ): if type_ is bool and flexi_bool: kw[key] = asbool(kw[key]) else: @@ -1044,8 +1121,8 @@ def constructor_copy(obj, cls, *args, **kw): names = get_cls_kwargs(cls) kw.update( - (k, obj.__dict__[k]) for k in names.difference(kw) - if k in obj.__dict__) + (k, obj.__dict__[k]) for k in names.difference(kw) if k in obj.__dict__ + ) return cls(*args, **kw) @@ -1072,10 +1149,11 @@ def duck_type_collection(specimen, default=None): property is present, return that preferentially. """ - if hasattr(specimen, '__emulates__'): + if hasattr(specimen, "__emulates__"): # canonicalize set vs sets.Set to a standard: the builtin set - if (specimen.__emulates__ is not None and - issubclass(specimen.__emulates__, set)): + if specimen.__emulates__ is not None and issubclass( + specimen.__emulates__, set + ): return set else: return specimen.__emulates__ @@ -1088,11 +1166,11 @@ def duck_type_collection(specimen, default=None): elif isa(specimen, dict): return dict - if hasattr(specimen, 'append'): + if hasattr(specimen, "append"): return list - elif hasattr(specimen, 'add'): + elif hasattr(specimen, "add"): return set - elif hasattr(specimen, 'set'): + elif hasattr(specimen, "set"): return dict else: return default @@ -1104,41 +1182,43 @@ def assert_arg_type(arg, argtype, name): else: if isinstance(argtype, tuple): raise exc.ArgumentError( - "Argument '%s' is expected to be one of type %s, got '%s'" % - (name, ' or '.join("'%s'" % a for a in argtype), type(arg))) + "Argument '%s' is expected to be one of type %s, got '%s'" + % (name, " or ".join("'%s'" % a for a in argtype), type(arg)) + ) else: raise exc.ArgumentError( - "Argument '%s' is expected to be of type '%s', got '%s'" % - (name, argtype, type(arg))) + "Argument '%s' is expected to be of type '%s', got '%s'" + % (name, argtype, type(arg)) + ) def dictlike_iteritems(dictlike): """Return a (key, value) iterator for almost any dict-like object.""" if compat.py3k: - if hasattr(dictlike, 'items'): + if hasattr(dictlike, "items"): return list(dictlike.items()) else: - if hasattr(dictlike, 'iteritems'): + if hasattr(dictlike, "iteritems"): return dictlike.iteritems() - elif hasattr(dictlike, 'items'): + elif hasattr(dictlike, "items"): return iter(dictlike.items()) - getter = getattr(dictlike, '__getitem__', getattr(dictlike, 'get', None)) + getter = getattr(dictlike, "__getitem__", getattr(dictlike, "get", None)) if getter is None: - raise TypeError( - "Object '%r' is not dict-like" % dictlike) + raise TypeError("Object '%r' is not dict-like" % dictlike) + + if hasattr(dictlike, "iterkeys"): - if hasattr(dictlike, 'iterkeys'): def iterator(): for key in dictlike.iterkeys(): yield key, getter(key) + return iterator() - elif hasattr(dictlike, 'keys'): + elif hasattr(dictlike, "keys"): return iter((key, getter(key)) for key in dictlike.keys()) else: - raise TypeError( - "Object '%r' is not dict-like" % dictlike) + raise TypeError("Object '%r' is not dict-like" % dictlike) class classproperty(property): @@ -1207,7 +1287,8 @@ class _symbol(int): def __repr__(self): return "symbol(%r)" % self.name -_symbol.__name__ = 'symbol' + +_symbol.__name__ = "symbol" class symbol(object): @@ -1231,6 +1312,7 @@ class symbol(object): ``doc`` here. """ + symbols = {} _lock = compat.threading.Lock() @@ -1292,9 +1374,11 @@ class _hash_limit_string(compat.text_type): """ + def __new__(cls, value, num, args): - interpolated = (value % args) + \ - (" (this warning may be suppressed after %d occurrences)" % num) + interpolated = (value % args) + ( + " (this warning may be suppressed after %d occurrences)" % num + ) self = super(_hash_limit_string, cls).__new__(cls, interpolated) self._hash = hash("%s_%d" % (value, hash(interpolated) % num)) return self @@ -1340,8 +1424,8 @@ def only_once(fn): return go -_SQLA_RE = re.compile(r'sqlalchemy/([a-z_]+/){0,2}[a-z_]+\.py') -_UNITTEST_RE = re.compile(r'unit(?:2|test2?/)') +_SQLA_RE = re.compile(r"sqlalchemy/([a-z_]+/){0,2}[a-z_]+\.py") +_UNITTEST_RE = re.compile(r"unit(?:2|test2?/)") def chop_traceback(tb, exclude_prefix=_UNITTEST_RE, exclude_suffix=_SQLA_RE): @@ -1363,18 +1447,17 @@ def chop_traceback(tb, exclude_prefix=_UNITTEST_RE, exclude_suffix=_SQLA_RE): start += 1 while start <= end and exclude_suffix.search(tb[end]): end -= 1 - return tb[start:end + 1] + return tb[start : end + 1] + NoneType = type(None) def attrsetter(attrname): - code = \ - "def set(obj, value):"\ - " obj.%s = value" % attrname + code = "def set(obj, value):" " obj.%s = value" % attrname env = locals().copy() exec(code, env) - return env['set'] + return env["set"] class EnsureKWArgType(type): @@ -1382,6 +1465,7 @@ class EnsureKWArgType(type): don't already. """ + def __init__(cls, clsname, bases, clsdict): fn_reg = cls.ensure_kwarg if fn_reg: @@ -1396,9 +1480,9 @@ class EnsureKWArgType(type): super(EnsureKWArgType, cls).__init__(clsname, bases, clsdict) def _wrap_w_kw(self, fn): - def wrap(*arg, **kw): return fn(*arg) + return update_wrapper(wrap, fn) @@ -1410,15 +1494,15 @@ def wrap_callable(wrapper, fn): object with __call__ method """ - if hasattr(fn, '__name__'): + if hasattr(fn, "__name__"): return update_wrapper(wrapper, fn) else: _f = wrapper _f.__name__ = fn.__class__.__name__ - if hasattr(fn, '__module__'): + if hasattr(fn, "__module__"): _f.__module__ = fn.__module__ - if hasattr(fn.__call__, '__doc__') and fn.__call__.__doc__: + if hasattr(fn.__call__, "__doc__") and fn.__call__.__doc__: _f.__doc__ = fn.__call__.__doc__ elif fn.__doc__: _f.__doc__ = fn.__doc__ @@ -1468,4 +1552,3 @@ def quoted_token_parser(value): idx += 1 return ["".join(token) for token in result] - diff --git a/lib/sqlalchemy/util/queue.py b/lib/sqlalchemy/util/queue.py index 640f70ea9..5e56e855a 100644 --- a/lib/sqlalchemy/util/queue.py +++ b/lib/sqlalchemy/util/queue.py @@ -23,7 +23,7 @@ from time import time as _time from .compat import threading -__all__ = ['Empty', 'Full', 'Queue'] +__all__ = ["Empty", "Full", "Queue"] class Empty(Exception): diff --git a/lib/sqlalchemy/util/topological.py b/lib/sqlalchemy/util/topological.py index 5f516d67e..95391c31b 100644 --- a/lib/sqlalchemy/util/topological.py +++ b/lib/sqlalchemy/util/topological.py @@ -10,7 +10,7 @@ from ..exc import CircularDependencyError from .. import util -__all__ = ['sort', 'sort_as_subsets', 'find_cycles'] +__all__ = ["sort", "sort_as_subsets", "find_cycles"] def sort_as_subsets(tuples, allitems, deterministic_order=False): @@ -33,7 +33,7 @@ def sort_as_subsets(tuples, allitems, deterministic_order=False): raise CircularDependencyError( "Circular dependency detected.", find_cycles(tuples, allitems), - _gen_edges(edges) + _gen_edges(edges), ) todo.difference_update(output) @@ -79,7 +79,7 @@ def find_cycles(tuples, allitems): top = stack[-1] for node in edges[top]: if node in stack: - cyc = stack[stack.index(node):] + cyc = stack[stack.index(node) :] todo.difference_update(cyc) output.update(cyc) @@ -93,8 +93,4 @@ def find_cycles(tuples, allitems): def _gen_edges(edges): - return set([ - (right, left) - for left in edges - for right in edges[left] - ]) + return set([(right, left) for left in edges for right in edges[left]]) |
