From ece27a0424d3cd503909c06d1b0864059292eef1 Mon Sep 17 00:00:00 2001 From: Raphael Mitsch Date: Mon, 2 May 2022 13:38:46 +0200 Subject: [PATCH] Refactor error messages to remove hardcoded strings (#10729) * Use custom error msg instead of hardcoded string: replaced remaining hardcoded error message strings. * Use custom error msg instead of hardcoded string: fixing faulty Errors import. --- spacy/errors.py | 10 +++++++++- spacy/ml/parser_model.pyx | 5 +++-- spacy/pipeline/_edit_tree_internals/edit_trees.pyx | 8 ++++---- spacy/pipeline/_parser_internals/arc_eager.pyx | 2 +- spacy/tokens/graph.pyx | 8 +++++--- spacy/tokens/span.pyx | 8 ++++---- 6 files changed, 26 insertions(+), 15 deletions(-) diff --git a/spacy/errors.py b/spacy/errors.py index abf710d7c..b01afcb80 100644 --- a/spacy/errors.py +++ b/spacy/errors.py @@ -905,7 +905,15 @@ class Errors(metaclass=ErrorsWithCodes): E1026 = ("Edit tree has an invalid format:\n{errors}") E1027 = ("AlignmentArray only supports slicing with a step of 1.") E1028 = ("AlignmentArray only supports indexing using an int or a slice.") - + E1029 = ("Edit tree cannot be applied to form.") + E1030 = ("Edit tree identifier out of range.") + E1031 = ("Could not find gold transition - see logs above.") + E1032 = ("`{var}` should not be {forbidden}, but received {value}.") + E1033 = ("Dimension {name} invalid -- only nO, nF, nP") + E1034 = ("Node index {i} out of bounds ({length})") + E1035 = ("Token index {i} out of bounds ({length})") + E1036 = ("Cannot index into NoneNode") + # Deprecated model shortcuts, only used in errors and warnings OLD_MODEL_SHORTCUTS = { diff --git a/spacy/ml/parser_model.pyx b/spacy/ml/parser_model.pyx index da937ca4f..4e854178d 100644 --- a/spacy/ml/parser_model.pyx +++ b/spacy/ml/parser_model.pyx @@ -11,6 +11,7 @@ import numpy.random from thinc.api import Model, CupyOps, NumpyOps from .. import util +from ..errors import Errors from ..typedefs cimport weight_t, class_t, hash_t from ..pipeline._parser_internals.stateclass cimport StateClass @@ -411,7 +412,7 @@ cdef class precompute_hiddens: elif name == "nO": return self.nO else: - raise ValueError(f"Dimension {name} invalid -- only nO, nF, nP") + raise ValueError(Errors.E1033.format(name=name)) def set_dim(self, name, value): if name == "nF": @@ -421,7 +422,7 @@ cdef class precompute_hiddens: elif name == "nO": self.nO = value else: - raise ValueError(f"Dimension {name} invalid -- only nO, nF, nP") + raise ValueError(Errors.E1033.format(name=name)) def __call__(self, X, bint is_train): if is_train: diff --git a/spacy/pipeline/_edit_tree_internals/edit_trees.pyx b/spacy/pipeline/_edit_tree_internals/edit_trees.pyx index 02907b67a..9d18c0334 100644 --- a/spacy/pipeline/_edit_tree_internals/edit_trees.pyx +++ b/spacy/pipeline/_edit_tree_internals/edit_trees.pyx @@ -132,7 +132,7 @@ cdef class EditTrees: could not be applied to the form. """ if tree_id >= self.trees.size(): - raise IndexError("Edit tree identifier out of range") + raise IndexError(Errors.E1030) lemma_pieces = [] try: @@ -154,7 +154,7 @@ cdef class EditTrees: match_node = tree.inner.match_node if match_node.prefix_len + match_node.suffix_len > len(form_part): - raise ValueError("Edit tree cannot be applied to form") + raise ValueError(Errors.E1029) suffix_start = len(form_part) - match_node.suffix_len @@ -169,7 +169,7 @@ cdef class EditTrees: if form_part == self.strings[tree.inner.subst_node.orig]: lemma_pieces.append(self.strings[tree.inner.subst_node.subst]) else: - raise ValueError("Edit tree cannot be applied to form") + raise ValueError(Errors.E1029) cpdef unicode tree_to_str(self, uint32_t tree_id): """Return the tree as a string. The tree tree string is formatted @@ -187,7 +187,7 @@ cdef class EditTrees: """ if tree_id >= self.trees.size(): - raise IndexError("Edit tree identifier out of range") + raise IndexError(Errors.E1030) cdef EditTreeC tree = self.trees[tree_id] cdef SubstNodeC subst_node diff --git a/spacy/pipeline/_parser_internals/arc_eager.pyx b/spacy/pipeline/_parser_internals/arc_eager.pyx index f1165592e..d60f1c3e6 100644 --- a/spacy/pipeline/_parser_internals/arc_eager.pyx +++ b/spacy/pipeline/_parser_internals/arc_eager.pyx @@ -824,7 +824,7 @@ cdef class ArcEager(TransitionSystem): for i in range(self.n_moves): print(self.get_class_name(i), is_valid[i], costs[i]) print("Gold sent starts?", is_sent_start(&gold_state, state.B(0)), is_sent_start(&gold_state, state.B(1))) - raise ValueError("Could not find gold transition - see logs above.") + raise ValueError(Errors.E1031) def get_oracle_sequence_from_state(self, StateClass state, ArcEagerGold gold, _debug=None): cdef int i diff --git a/spacy/tokens/graph.pyx b/spacy/tokens/graph.pyx index 9d64f924e..adc4d23c8 100644 --- a/spacy/tokens/graph.pyx +++ b/spacy/tokens/graph.pyx @@ -9,6 +9,8 @@ cimport cython import weakref from preshed.maps cimport map_get_unless_missing from murmurhash.mrmr cimport hash64 + +from .. import Errors from ..typedefs cimport hash_t from ..strings import get_string_id from ..structs cimport EdgeC, GraphC @@ -68,7 +70,7 @@ cdef class Node: """ cdef int length = graph.c.nodes.size() if i >= length or -i >= length: - raise IndexError(f"Node index {i} out of bounds ({length})") + raise IndexError(Errors.E1034.format(i=i, length=length)) if i < 0: i += length self.graph = graph @@ -88,7 +90,7 @@ cdef class Node: """Get a token index from the node's set of tokens.""" length = self.graph.c.nodes[self.i].size() if i >= length or -i >= length: - raise IndexError(f"Token index {i} out of bounds ({length})") + raise IndexError(Errors.E1035.format(i=i, length=length)) if i < 0: i += length return self.graph.c.nodes[self.i][i] @@ -306,7 +308,7 @@ cdef class NoneNode(Node): self.i = -1 def __getitem__(self, int i): - raise IndexError("Cannot index into NoneNode.") + raise IndexError(Errors.E1036) def __len__(self): return 0 diff --git a/spacy/tokens/span.pyx b/spacy/tokens/span.pyx index 4b0c724e5..305d7caf4 100644 --- a/spacy/tokens/span.pyx +++ b/spacy/tokens/span.pyx @@ -730,7 +730,7 @@ cdef class Span: def __set__(self, int start): if start < 0: - raise IndexError("TODO") + raise IndexError(Errors.E1032.format(var="start", forbidden="< 0", value=start)) self.c.start = start property end: @@ -739,7 +739,7 @@ cdef class Span: def __set__(self, int end): if end < 0: - raise IndexError("TODO") + raise IndexError(Errors.E1032.format(var="end", forbidden="< 0", value=end)) self.c.end = end property start_char: @@ -748,7 +748,7 @@ cdef class Span: def __set__(self, int start_char): if start_char < 0: - raise IndexError("TODO") + raise IndexError(Errors.E1032.format(var="start_char", forbidden="< 0", value=start_char)) self.c.start_char = start_char property end_char: @@ -757,7 +757,7 @@ cdef class Span: def __set__(self, int end_char): if end_char < 0: - raise IndexError("TODO") + raise IndexError(Errors.E1032.format(var="end_char", forbidden="< 0", value=end_char)) self.c.end_char = end_char property label: