diff options
| author | RamonWill <ramonwilliams@hotmail.co.uk> | 2020-08-12 14:50:20 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-08-17 10:34:22 -0400 |
| commit | 7ab5e4e4dd8acfe28b8e9ab44d2e821b984f0d02 (patch) | |
| tree | bcbec8f54f57dd68e592d9fed9249bcb5bdea237 /lib/sqlalchemy | |
| parent | 677f9bd309b825bd3e253954e38ba642f92fef29 (diff) | |
| download | sqlalchemy-7ab5e4e4dd8acfe28b8e9ab44d2e821b984f0d02.tar.gz | |
error message for Lookup
Thr proposed change will provide the user with the target Enum Class name as well as up to four possible enum values when a LookupError is raised in the Enum Class.
A user requested that the enum name and possible values are included to the LookupError message to make debugging easier. The criteria included using ellipses for Enums containing more than four values and using ellipses for enum values that were greater than a certain number of characters (for this resolution the limit is 11 characters).
This pull request is:
- [ ] A documentation / typographical error fix
- Good to go, no issue or tests are needed
- [X ] A short code fix
- please include the issue number, and create an issue if none exists, which
must include a complete example of the issue. one line code fixes without an
issue and demonstration will not be accepted.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests. one line code fixes without tests will not be accepted.
- [ ] A new feature implementation
- please include the issue number, and create an issue if none exists, which must
include a complete example of how the feature would look.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests.
**Have a nice day!**
Fixes: #4733
Closes: #5490
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5490
Pull-request-sha: 55e76f2ae796b59b7de157cfaae5235dffa359cb
Change-Id: I4541f9efed1c05401587a413e9e748d46938bcd1
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/sql/sqltypes.py | 17 | ||||
| -rw-r--r-- | lib/sqlalchemy/util/langhelpers.py | 14 |
2 files changed, 29 insertions, 2 deletions
diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index 64663a6b0..af5829216 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -38,6 +38,7 @@ from .. import inspection from .. import processors from .. import util from ..util import compat +from ..util import langhelpers from ..util import pickle @@ -1555,7 +1556,13 @@ class Enum(Emulated, String, SchemaType): else: util.raise_( LookupError( - '"%s" is not among the defined enum values' % elem + "'%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, ) @@ -1579,7 +1586,13 @@ class Enum(Emulated, String, SchemaType): except KeyError as err: util.raise_( LookupError( - '"%s" is not among the defined enum values' % elem + "'%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, ) diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py index cec54542a..e8abf3130 100644 --- a/lib/sqlalchemy/util/langhelpers.py +++ b/lib/sqlalchemy/util/langhelpers.py @@ -1735,3 +1735,17 @@ def inject_param_text(doctext, inject_params): lines.append(line) return "\n".join(lines) + + +def repr_tuple_names(names): + """ Trims a list of strings from the middle and return a string of up to + four elements. Strings greater than 11 characters will be truncated""" + if len(names) == 0: + return None + flag = len(names) <= 4 + names = names[0:4] if flag else names[0:3] + names[-1:] + res = ["%s.." % name[:11] if len(name) > 11 else name for name in names] + if flag: + return ", ".join(res) + else: + return "%s, ..., %s" % (", ".join(res[0:3]), res[-1]) |
