diff options
Diffstat (limited to 'lib/sqlalchemy/sql')
-rw-r--r-- | lib/sqlalchemy/sql/base.py | 27 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/coercions.py | 23 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/compiler.py | 42 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/ddl.py | 33 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/elements.py | 70 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/lambdas.py | 33 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/schema.py | 41 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/selectable.py | 26 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/sqltypes.py | 42 |
9 files changed, 130 insertions, 207 deletions
diff --git a/lib/sqlalchemy/sql/base.py b/lib/sqlalchemy/sql/base.py index 7ac253dd4..187651435 100644 --- a/lib/sqlalchemy/sql/base.py +++ b/lib/sqlalchemy/sql/base.py @@ -199,7 +199,7 @@ class _DialectArgView(collections_abc.MutableMapping): try: dialect, value_key = key.split("_", 1) except ValueError as err: - util.raise_(KeyError(key), replace_context=err) + raise KeyError(key) from err else: return dialect, value_key @@ -209,7 +209,7 @@ class _DialectArgView(collections_abc.MutableMapping): try: opt = self.obj.dialect_options[dialect] except exc.NoSuchModuleError as err: - util.raise_(KeyError(key), replace_context=err) + raise KeyError(key) from err else: return opt[value_key] @@ -217,12 +217,9 @@ class _DialectArgView(collections_abc.MutableMapping): try: dialect, value_key = self._key(key) except KeyError as err: - util.raise_( - exc.ArgumentError( - "Keys must be of the form <dialectname>_<argname>" - ), - replace_context=err, - ) + raise exc.ArgumentError( + "Keys must be of the form <dialectname>_<argname>" + ) from err else: self.obj.dialect_options[dialect][value_key] = value @@ -1203,7 +1200,7 @@ class ColumnCollection: return self._index[key] except KeyError as err: if isinstance(key, int): - util.raise_(IndexError(key), replace_context=err) + raise IndexError(key) from err else: raise @@ -1211,7 +1208,7 @@ class ColumnCollection: try: return self._index[key] except KeyError as err: - util.raise_(AttributeError(key), replace_context=err) + raise AttributeError(key) from err def __contains__(self, key): if key not in self._index: @@ -1656,10 +1653,6 @@ def _entity_namespace_key(entity, key, default=NO_ARG): else: return getattr(ns, key) except AttributeError as err: - util.raise_( - exc.InvalidRequestError( - 'Entity namespace for "%s" has no property "%s"' - % (entity, key) - ), - replace_context=err, - ) + raise exc.InvalidRequestError( + 'Entity namespace for "%s" has no property "%s"' % (entity, key) + ) from err diff --git a/lib/sqlalchemy/sql/coercions.py b/lib/sqlalchemy/sql/coercions.py index 38eed4d2a..9b8b4540c 100644 --- a/lib/sqlalchemy/sql/coercions.py +++ b/lib/sqlalchemy/sql/coercions.py @@ -280,7 +280,7 @@ class RoleImpl: if advice: msg += " " + advice - util.raise_(exc.ArgumentError(msg, code=code), replace_context=err) + raise exc.ArgumentError(msg, code=code) from err class _Deannotate: @@ -345,18 +345,15 @@ class _ColumnCoercions: def _no_text_coercion( element, argname=None, exc_cls=exc.ArgumentError, extra=None, err=None ): - util.raise_( - exc_cls( - "%(extra)sTextual SQL expression %(expr)r %(argname)sshould be " - "explicitly declared as text(%(expr)r)" - % { - "expr": util.ellipses_string(element), - "argname": "for argument %s" % (argname,) if argname else "", - "extra": "%s " % extra if extra else "", - } - ), - replace_context=err, - ) + raise exc_cls( + "%(extra)sTextual SQL expression %(expr)r %(argname)sshould be " + "explicitly declared as text(%(expr)r)" + % { + "expr": util.ellipses_string(element), + "argname": "for argument %s" % (argname,) if argname else "", + "extra": "%s " % extra if extra else "", + } + ) from err class _NoTextCoercion: diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 5f6ee5f41..a9dd6a23a 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -467,10 +467,7 @@ class Compiled: raise exc.ObjectNotExecutableError(self.statement) def visit_unsupported_compilation(self, element, err): - util.raise_( - exc.UnsupportedCompilationError(self, type(element)), - replace_context=err, - ) + raise exc.UnsupportedCompilationError(self, type(element)) from err @property def sql_compiler(self): @@ -518,10 +515,7 @@ class TypeCompiler(metaclass=util.EnsureKWArgType): return type_._compiler_dispatch(self, **kw) def visit_unsupported_compilation(self, element, err, **kw): - util.raise_( - exc.UnsupportedCompilationError(self, element), - replace_context=err, - ) + raise exc.UnsupportedCompilationError(self, element) from err # this was a Visitable, but to allow accurate detection of @@ -830,10 +824,7 @@ class SQLCompiler(Compiled): try: return self.stack[-1]["selectable"] except IndexError as ie: - util.raise_( - IndexError("Compiler does not have a stack entry"), - replace_context=ie, - ) + raise IndexError("Compiler does not have a stack entry") from ie @property def prefetch(self): @@ -943,13 +934,10 @@ class SQLCompiler(Compiled): try: orig_extracted = self.cache_key[1] except TypeError as err: - util.raise_( - exc.CompileError( - "This compiled object has no original cache key; " - "can't pass extracted_parameters to construct_params" - ), - replace_context=err, - ) + raise exc.CompileError( + "This compiled object has no original cache key; " + "can't pass extracted_parameters to construct_params" + ) from err ckbm = self._cache_key_bind_match resolved_extracted = { @@ -2176,10 +2164,7 @@ class SQLCompiler(Compiled): try: opstring = OPERATORS[operator_] except KeyError as err: - util.raise_( - exc.UnsupportedCompilationError(self, operator_), - replace_context=err, - ) + raise exc.UnsupportedCompilationError(self, operator_) from err else: return self._generate_generic_binary( binary, @@ -4393,13 +4378,10 @@ class DDLCompiler(Compiled): if column.primary_key: first_pk = True except exc.CompileError as ce: - util.raise_( - exc.CompileError( - "(in table '%s', column '%s'): %s" - % (table.description, column.name, ce.args[0]) - ), - from_=ce, - ) + raise exc.CompileError( + "(in table '%s', column '%s'): %s" + % (table.description, column.name, ce.args[0]) + ) from ce const = self.create_table_constraints( table, diff --git a/lib/sqlalchemy/sql/ddl.py b/lib/sqlalchemy/sql/ddl.py index ef0906328..f415aeaff 100644 --- a/lib/sqlalchemy/sql/ddl.py +++ b/lib/sqlalchemy/sql/ddl.py @@ -855,26 +855,19 @@ class SchemaDropper(DDLBase): ) collection = [(t, ()) for t in unsorted_tables] else: - util.raise_( - exc.CircularDependencyError( - err2.args[0], - err2.cycles, - err2.edges, - msg="Can't sort tables for DROP; an " - "unresolvable foreign key " - "dependency exists between tables: %s. Please ensure " - "that the ForeignKey and ForeignKeyConstraint objects " - "involved in the cycle have " - "names so that they can be dropped using " - "DROP CONSTRAINT." - % ( - ", ".join( - sorted([t.fullname for t in err2.cycles]) - ) - ), - ), - from_=err2, - ) + raise exc.CircularDependencyError( + err2.args[0], + err2.cycles, + err2.edges, + msg="Can't sort tables for DROP; an " + "unresolvable foreign key " + "dependency exists between tables: %s. Please ensure " + "that the ForeignKey and ForeignKeyConstraint objects " + "involved in the cycle have " + "names so that they can be dropped using " + "DROP CONSTRAINT." + % (", ".join(sorted([t.fullname for t in err2.cycles]))), + ) from err2 seq_coll = [ s diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index 028743706..37425345b 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -848,13 +848,10 @@ class ColumnElement( try: comparator_factory = self.type.comparator_factory except AttributeError as err: - util.raise_( - TypeError( - "Object %r associated with '.type' attribute " - "is not a TypeEngine class or object" % self.type - ), - replace_context=err, - ) + raise TypeError( + "Object %r associated with '.type' attribute " + "is not a TypeEngine class or object" % self.type + ) from err else: return comparator_factory(self) @@ -862,17 +859,14 @@ class ColumnElement( try: return getattr(self.comparator, key) except AttributeError as err: - util.raise_( - AttributeError( - "Neither %r object nor %r object has an attribute %r" - % ( - type(self).__name__, - type(self.comparator).__name__, - key, - ) - ), - replace_context=err, - ) + raise AttributeError( + "Neither %r object nor %r object has an attribute %r" + % ( + type(self).__name__, + type(self.comparator).__name__, + key, + ) + ) from err def operate(self, op, *other, **kwargs): return op(self.comparator, *other, **kwargs) @@ -1989,13 +1983,10 @@ class TextClause( # so that a text() construct can support unique parameters existing = new_params[bind._orig_key] except KeyError as err: - util.raise_( - exc.ArgumentError( - "This text() construct doesn't define a " - "bound parameter named %r" % bind._orig_key - ), - replace_context=err, - ) + raise exc.ArgumentError( + "This text() construct doesn't define a " + "bound parameter named %r" % bind._orig_key + ) from err else: new_params[existing._orig_key] = bind @@ -2003,13 +1994,10 @@ class TextClause( try: existing = new_params[key] except KeyError as err: - util.raise_( - exc.ArgumentError( - "This text() construct doesn't define a " - "bound parameter named %r" % key - ), - replace_context=err, - ) + raise exc.ArgumentError( + "This text() construct doesn't define a " + "bound parameter named %r" % key + ) from err else: new_params[key] = existing._with_value(value, required=False) @@ -4175,12 +4163,9 @@ class Over(ColumnElement): try: lower = int(range_[0]) except ValueError as err: - util.raise_( - exc.ArgumentError( - "Integer or None expected for range value" - ), - replace_context=err, - ) + raise exc.ArgumentError( + "Integer or None expected for range value" + ) from err else: if lower == 0: lower = RANGE_CURRENT @@ -4191,12 +4176,9 @@ class Over(ColumnElement): try: upper = int(range_[1]) except ValueError as err: - util.raise_( - exc.ArgumentError( - "Integer or None expected for range value" - ), - replace_context=err, - ) + raise exc.ArgumentError( + "Integer or None expected for range value" + ) from err else: if upper == 0: upper = RANGE_CURRENT diff --git a/lib/sqlalchemy/sql/lambdas.py b/lib/sqlalchemy/sql/lambdas.py index 2256fb5a9..da1dbedbb 100644 --- a/lib/sqlalchemy/sql/lambdas.py +++ b/lib/sqlalchemy/sql/lambdas.py @@ -901,24 +901,21 @@ class AnalyzedCode: def _raise_for_uncacheable_closure_variable( self, variable_name, fn, from_=None ): - util.raise_( - exc.InvalidRequestError( - "Closure variable named '%s' inside of lambda callable %s " - "does not refer to a cacheable SQL element, and also does not " - "appear to be serving as a SQL literal bound value based on " - "the default " - "SQL expression returned by the function. This variable " - "needs to remain outside the scope of a SQL-generating lambda " - "so that a proper cache key may be generated from the " - "lambda's state. Evaluate this variable outside of the " - "lambda, set track_on=[<elements>] to explicitly select " - "closure elements to track, or set " - "track_closure_variables=False to exclude " - "closure variables from being part of the cache key." - % (variable_name, fn.__code__), - ), - from_=from_, - ) + raise exc.InvalidRequestError( + "Closure variable named '%s' inside of lambda callable %s " + "does not refer to a cacheable SQL element, and also does not " + "appear to be serving as a SQL literal bound value based on " + "the default " + "SQL expression returned by the function. This variable " + "needs to remain outside the scope of a SQL-generating lambda " + "so that a proper cache key may be generated from the " + "lambda's state. Evaluate this variable outside of the " + "lambda, set track_on=[<elements>] to explicitly select " + "closure elements to track, or set " + "track_closure_variables=False to exclude " + "closure variables from being part of the cache key." + % (variable_name, fn.__code__), + ) from from_ def _cache_key_getter_tracked_literal(self, fn, pytracker): """Return a getter that will extend a cache key with new entries diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index cdd17f2c0..9b5005b5d 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -120,13 +120,10 @@ class SchemaItem(SchemaEventTarget, visitors.Visitable): try: spwd = item._set_parent_with_dispatch except AttributeError as err: - util.raise_( - exc.ArgumentError( - "'SchemaItem' object, such as a 'Column' or a " - "'Constraint' expected, got %r" % item - ), - replace_context=err, - ) + raise exc.ArgumentError( + "'SchemaItem' object, such as a 'Column' or a " + "'Constraint' expected, got %r" % item + ) from err else: spwd(self, **kw) @@ -1932,16 +1929,13 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause): *fk ) except TypeError as err: - util.raise_( - TypeError( - "Could not create a copy of this %r object. " - "Ensure the class includes a _constructor() " - "attribute or method which accepts the " - "standard Column constructor arguments, or " - "references the Column class itself." % self.__class__ - ), - from_=err, - ) + raise TypeError( + "Could not create a copy of this %r object. " + "Ensure the class includes a _constructor() " + "attribute or method which accepts the " + "standard Column constructor arguments, or " + "references the Column class itself." % self.__class__ + ) from err c.table = selectable c._propagate_attrs = selectable._propagate_attrs @@ -3647,14 +3641,11 @@ class ForeignKeyConstraint(ColumnCollectionConstraint): try: ColumnCollectionConstraint._set_parent(self, table) except KeyError as ke: - util.raise_( - exc.ArgumentError( - "Can't create ForeignKeyConstraint " - "on table '%s': no column " - "named '%s' is present." % (table.description, ke.args[0]) - ), - from_=ke, - ) + raise exc.ArgumentError( + "Can't create ForeignKeyConstraint " + "on table '%s': no column " + "named '%s' is present." % (table.description, ke.args[0]) + ) from ke for col, fk in zip(self.columns, self.elements): if not hasattr(fk, "parent") or fk.parent is not col: diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index a96ed479d..2f157c27e 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -3606,13 +3606,10 @@ class GenerativeSelect(DeprecatedSelectBaseGenerations, SelectBase): try: value = clause._limit_offset_value except AttributeError as err: - util.raise_( - exc.CompileError( - "This SELECT structure does not use a simple " - "integer value for %s" % attrname - ), - replace_context=err, - ) + raise exc.CompileError( + "This SELECT structure does not use a simple " + "integer value for %s" % attrname + ) from err else: return util.asint(value) @@ -5053,15 +5050,12 @@ class Select( try: cols_present = bool(columns) except TypeError as err: - util.raise_( - exc.ArgumentError( - "select() construct created in legacy mode, i.e. with " - "keyword arguments, must provide the columns argument as " - "a Python list or other iterable.", - code="c9ae", - ), - from_=err, - ) + raise exc.ArgumentError( + "select() construct created in legacy mode, i.e. with " + "keyword arguments, must provide the columns argument as " + "a Python list or other iterable.", + code="c9ae", + ) from err if cols_present: self._raw_columns = [ diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index f035284f4..574692fed 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -1325,18 +1325,15 @@ class Enum(Emulated, String, SchemaType): if not self.validate_strings and isinstance(elem, str): return elem else: - util.raise_( - LookupError( - "'%s' is not among the defined enum values. " - "Enum name: %s. Possible values: %s" - % ( - elem, - self.name, - langhelpers.repr_tuple_names(self.enums), - ) - ), - replace_context=err, - ) + raise LookupError( + "'%s' is not among the defined enum values. " + "Enum name: %s. Possible values: %s" + % ( + elem, + self.name, + langhelpers.repr_tuple_names(self.enums), + ) + ) from err class Comparator(String.Comparator): def _adapt_expression(self, op, other_comparator): @@ -1353,18 +1350,15 @@ class Enum(Emulated, String, SchemaType): try: return self._object_lookup[elem] except KeyError as err: - util.raise_( - LookupError( - "'%s' is not among the defined enum values. " - "Enum name: %s. Possible values: %s" - % ( - elem, - self.name, - langhelpers.repr_tuple_names(self.enums), - ) - ), - replace_context=err, - ) + raise LookupError( + "'%s' is not among the defined enum values. " + "Enum name: %s. Possible values: %s" + % ( + elem, + self.name, + langhelpers.repr_tuple_names(self.enums), + ) + ) from err def __repr__(self): return util.generic_repr( |