From 19ba6eca15631a1a7bf6b418ca7d45360afac1c8 Mon Sep 17 00:00:00 2001 From: shadeMe <shadeMe@users.noreply.github.com> Date: Wed, 17 Aug 2022 16:53:58 +0200 Subject: [PATCH] 'strings': Raise error when non-string/-int types are passed to functions that don't accept them --- spacy/errors.py | 3 +++ spacy/strings.pyx | 9 +++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/spacy/errors.py b/spacy/errors.py index fd412a4da..67d841633 100644 --- a/spacy/errors.py +++ b/spacy/errors.py @@ -939,6 +939,9 @@ class Errors(metaclass=ErrorsWithCodes): "`{arg2}`={arg2_values} but these arguments are conflicting.") E1043 = ("Expected None or a value in range [{range_start}, {range_end}] for entity linker threshold, but got " "{value}.") + + # New errors added in v4.x + E1400 = ("Expected 'str' or 'int', but got '{key_type}'") # Deprecated model shortcuts, only used in errors and warnings diff --git a/spacy/strings.pyx b/spacy/strings.pyx index f7ab259b5..a77210745 100644 --- a/spacy/strings.pyx +++ b/spacy/strings.pyx @@ -42,8 +42,7 @@ def get_string_id(key): # whose comparison operators can incur a significant overhead). return str_hash else: - # TODO: Raise an error instead - return key + raise KeyError(Errors.E1400.format(key_type=type(key))) cpdef hash_t hash_string(str string) except 0: @@ -96,8 +95,7 @@ cdef class StringStore: else: utf8str = <Utf8Str*>self._map.get(str_hash) else: - # TODO: Raise an error instead - utf8str = <Utf8Str*>self._map.get(string_or_id) + raise KeyError(Errors.E1400.format(key_type=type(key))) if utf8str is NULL: raise KeyError(Errors.E018.format(hash_value=string_or_id)) @@ -164,8 +162,7 @@ cdef class StringStore: elif _try_coerce_to_hash(string_or_id, &str_hash): pass else: - # TODO: Raise an error instead - return self._map.get(string_or_id) is not NULL + raise KeyError(Errors.E1400.format(key_type=type(string_or_id))) if str_hash < len(SYMBOLS_BY_INT): return True