From 1e1a38e7801f410f244e4bbb44ec795ae152e04e Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 6 Jan 2019 01:14:26 -0500 Subject: 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 --- lib/sqlalchemy/sql/elements.py | 800 ++++++++++++++++++++++++----------------- 1 file changed, 466 insertions(+), 334 deletions(-) (limited to 'lib/sqlalchemy/sql/elements.py') diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index de3b7992a..e857f2da8 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -51,9 +51,8 @@ def collate(expression, collation): expr = _literal_as_binds(expression) return BinaryExpression( - expr, - CollationClause(collation), - operators.collate, type_=expr.type) + expr, CollationClause(collation), operators.collate, type_=expr.type + ) def between(expr, lower_bound, upper_bound, symmetric=False): @@ -130,8 +129,6 @@ def literal(value, type_=None): return BindParameter(None, value, type_=type_, unique=True) - - def outparam(key, type_=None): """Create an 'OUT' parameter for usage in functions (stored procedures), for databases which support them. @@ -142,8 +139,7 @@ def outparam(key, type_=None): attribute, which returns a dictionary containing the values. """ - return BindParameter( - key, None, type_=type_, unique=False, isoutparam=True) + return BindParameter(key, None, type_=type_, unique=False, isoutparam=True) def not_(clause): @@ -163,7 +159,8 @@ class ClauseElement(Visitable): expression. """ - __visit_name__ = 'clause' + + __visit_name__ = "clause" _annotations = {} supports_execution = False @@ -230,7 +227,7 @@ class ClauseElement(Visitable): def __getstate__(self): d = self.__dict__.copy() - d.pop('_is_clone_of', None) + d.pop("_is_clone_of", None) return d def _annotate(self, values): @@ -300,7 +297,8 @@ class ClauseElement(Visitable): kwargs.update(optionaldict[0]) elif len(optionaldict) > 1: raise exc.ArgumentError( - "params() takes zero or one positional dictionary argument") + "params() takes zero or one positional dictionary argument" + ) def visit_bindparam(bind): if bind.key in kwargs: @@ -308,7 +306,8 @@ class ClauseElement(Visitable): bind.required = False if unique: bind._convert_to_unique() - return cloned_traverse(self, {}, {'bindparam': visit_bindparam}) + + return cloned_traverse(self, {}, {"bindparam": visit_bindparam}) def compare(self, other, **kw): r"""Compare this ClauseElement to the given ClauseElement. @@ -451,7 +450,7 @@ class ClauseElement(Visitable): if util.py3k: return str(self.compile()) else: - return unicode(self.compile()).encode('ascii', 'backslashreplace') + return unicode(self.compile()).encode("ascii", "backslashreplace") def __and__(self, other): """'and' at the ClauseElement level. @@ -472,7 +471,7 @@ class ClauseElement(Visitable): return or_(self, other) def __invert__(self): - if hasattr(self, 'negation_clause'): + if hasattr(self, "negation_clause"): return self.negation_clause else: return self._negate() @@ -481,7 +480,8 @@ class ClauseElement(Visitable): return UnaryExpression( self.self_group(against=operators.inv), operator=operators.inv, - negate=None) + negate=None, + ) def __bool__(self): raise TypeError("Boolean value of this clause is not defined") @@ -493,8 +493,12 @@ class ClauseElement(Visitable): if friendly is None: return object.__repr__(self) else: - return '<%s.%s at 0x%x; %s>' % ( - self.__module__, self.__class__.__name__, id(self), friendly) + return "<%s.%s at 0x%x; %s>" % ( + self.__module__, + self.__class__.__name__, + id(self), + friendly, + ) class ColumnElement(operators.ColumnOperators, ClauseElement): @@ -571,7 +575,7 @@ class ColumnElement(operators.ColumnOperators, ClauseElement): """ - __visit_name__ = 'column_element' + __visit_name__ = "column_element" primary_key = False foreign_keys = [] @@ -646,11 +650,12 @@ class ColumnElement(operators.ColumnOperators, ClauseElement): _alt_names = () def self_group(self, against=None): - if (against in (operators.and_, operators.or_, operators._asbool) and - self.type._type_affinity - is type_api.BOOLEANTYPE._type_affinity): + if ( + against in (operators.and_, operators.or_, operators._asbool) + and self.type._type_affinity is type_api.BOOLEANTYPE._type_affinity + ): return AsBoolean(self, operators.istrue, operators.isfalse) - elif (against in (operators.any_op, operators.all_op)): + elif against in (operators.any_op, operators.all_op): return Grouping(self) else: return self @@ -675,7 +680,8 @@ class ColumnElement(operators.ColumnOperators, ClauseElement): except AttributeError: raise TypeError( "Object %r associated with '.type' attribute " - "is not a TypeEngine class or object" % self.type) + "is not a TypeEngine class or object" % self.type + ) else: return comparator_factory(self) @@ -684,10 +690,8 @@ class ColumnElement(operators.ColumnOperators, ClauseElement): return getattr(self.comparator, key) except AttributeError: raise AttributeError( - 'Neither %r object nor %r object has an attribute %r' % ( - type(self).__name__, - type(self.comparator).__name__, - key) + "Neither %r object nor %r object has an attribute %r" + % (type(self).__name__, type(self.comparator).__name__, key) ) def operate(self, op, *other, **kwargs): @@ -697,10 +701,14 @@ class ColumnElement(operators.ColumnOperators, ClauseElement): return op(other, self.comparator, **kwargs) def _bind_param(self, operator, obj, type_=None): - return BindParameter(None, obj, - _compared_to_operator=operator, - type_=type_, - _compared_to_type=self.type, unique=True) + return BindParameter( + None, + obj, + _compared_to_operator=operator, + type_=type_, + _compared_to_type=self.type, + unique=True, + ) @property def expression(self): @@ -713,17 +721,18 @@ class ColumnElement(operators.ColumnOperators, ClauseElement): @property def _select_iterable(self): - return (self, ) + return (self,) @util.memoized_property def base_columns(self): - return util.column_set(c for c in self.proxy_set - if not hasattr(c, '_proxies')) + return util.column_set( + c for c in self.proxy_set if not hasattr(c, "_proxies") + ) @util.memoized_property def proxy_set(self): s = util.column_set([self]) - if hasattr(self, '_proxies'): + if hasattr(self, "_proxies"): for c in self._proxies: s.update(c.proxy_set) return s @@ -738,11 +747,15 @@ class ColumnElement(operators.ColumnOperators, ClauseElement): """Return True if the given column element compares to this one when targeting within a result row.""" - return hasattr(other, 'name') and hasattr(self, 'name') and \ - other.name == self.name + return ( + hasattr(other, "name") + and hasattr(self, "name") + and other.name == self.name + ) def _make_proxy( - self, selectable, name=None, name_is_truncatable=False, **kw): + self, selectable, name=None, name_is_truncatable=False, **kw + ): """Create a new :class:`.ColumnElement` representing this :class:`.ColumnElement` as it appears in the select list of a descending selectable. @@ -762,13 +775,12 @@ class ColumnElement(operators.ColumnOperators, ClauseElement): key = name co = ColumnClause( _as_truncated(name) if name_is_truncatable else name, - type_=getattr(self, 'type', None), - _selectable=selectable + type_=getattr(self, "type", None), + _selectable=selectable, ) co._proxies = [self] if selectable._is_clone_of is not None: - co._is_clone_of = \ - selectable._is_clone_of.columns.get(key) + co._is_clone_of = selectable._is_clone_of.columns.get(key) selectable._columns[key] = co return co @@ -788,7 +800,7 @@ class ColumnElement(operators.ColumnOperators, ClauseElement): this one via foreign key or other criterion. """ - to_compare = (other, ) + to_compare = (other,) if equivalents and other in equivalents: to_compare = equivalents[other].union(to_compare) @@ -838,7 +850,7 @@ class ColumnElement(operators.ColumnOperators, ClauseElement): self = self._is_clone_of return _anonymous_label( - '%%(%d %s)s' % (id(self), getattr(self, 'name', 'anon')) + "%%(%d %s)s" % (id(self), getattr(self, "name", "anon")) ) @@ -862,18 +874,25 @@ class BindParameter(ColumnElement): """ - __visit_name__ = 'bindparam' + __visit_name__ = "bindparam" _is_crud = False _expanding_in_types = () - def __init__(self, key, value=NO_ARG, type_=None, - unique=False, required=NO_ARG, - quote=None, callable_=None, - expanding=False, - isoutparam=False, - _compared_to_operator=None, - _compared_to_type=None): + def __init__( + self, + key, + value=NO_ARG, + type_=None, + unique=False, + required=NO_ARG, + quote=None, + callable_=None, + expanding=False, + isoutparam=False, + _compared_to_operator=None, + _compared_to_type=None, + ): r"""Produce a "bound expression". The return value is an instance of :class:`.BindParameter`; this @@ -1093,7 +1112,7 @@ class BindParameter(ColumnElement): type_ = key.type key = key.key if required is NO_ARG: - required = (value is NO_ARG and callable_ is None) + required = value is NO_ARG and callable_ is None if value is NO_ARG: value = None @@ -1101,11 +1120,11 @@ class BindParameter(ColumnElement): key = quoted_name(key, quote) if unique: - self.key = _anonymous_label('%%(%d %s)s' % (id(self), key - or 'param')) + self.key = _anonymous_label( + "%%(%d %s)s" % (id(self), key or "param") + ) else: - self.key = key or _anonymous_label('%%(%d param)s' - % id(self)) + self.key = key or _anonymous_label("%%(%d param)s" % id(self)) # identifying key that won't change across # clones, used to identify the bind's logical @@ -1114,7 +1133,7 @@ class BindParameter(ColumnElement): # key that was passed in the first place, used to # generate new keys - self._orig_key = key or 'param' + self._orig_key = key or "param" self.unique = unique self.value = value @@ -1125,9 +1144,9 @@ class BindParameter(ColumnElement): if type_ is None: if _compared_to_type is not None: - self.type = \ - _compared_to_type.coerce_compared_value( - _compared_to_operator, value) + self.type = _compared_to_type.coerce_compared_value( + _compared_to_operator, value + ) else: self.type = type_api._resolve_value_to_type(value) elif isinstance(type_, type): @@ -1174,24 +1193,28 @@ class BindParameter(ColumnElement): def _clone(self): c = ClauseElement._clone(self) if self.unique: - c.key = _anonymous_label('%%(%d %s)s' % (id(c), c._orig_key - or 'param')) + c.key = _anonymous_label( + "%%(%d %s)s" % (id(c), c._orig_key or "param") + ) return c def _convert_to_unique(self): if not self.unique: self.unique = True self.key = _anonymous_label( - '%%(%d %s)s' % (id(self), self._orig_key or 'param')) + "%%(%d %s)s" % (id(self), self._orig_key or "param") + ) def compare(self, other, **kw): """Compare this :class:`BindParameter` to the given clause.""" - return isinstance(other, BindParameter) \ - and self.type._compare_type_affinity(other.type) \ - and self.value == other.value \ + return ( + isinstance(other, BindParameter) + and self.type._compare_type_affinity(other.type) + and self.value == other.value and self.callable == other.callable + ) def __getstate__(self): """execute a deferred value for serialization purposes.""" @@ -1200,13 +1223,16 @@ class BindParameter(ColumnElement): v = self.value if self.callable: v = self.callable() - d['callable'] = None - d['value'] = v + d["callable"] = None + d["value"] = v return d def __repr__(self): - return 'BindParameter(%r, %r, type_=%r)' % (self.key, - self.value, self.type) + return "BindParameter(%r, %r, type_=%r)" % ( + self.key, + self.value, + self.type, + ) class TypeClause(ClauseElement): @@ -1216,7 +1242,7 @@ class TypeClause(ClauseElement): """ - __visit_name__ = 'typeclause' + __visit_name__ = "typeclause" def __init__(self, type): self.type = type @@ -1242,12 +1268,12 @@ class TextClause(Executable, ClauseElement): """ - __visit_name__ = 'textclause' + __visit_name__ = "textclause" - _bind_params_regex = re.compile(r'(?