'strings': Raise error when non-string/-int types are passed to functions that don't accept them

This commit is contained in:
shadeMe 2022-08-17 16:53:58 +02:00
parent d395c603c5
commit 19ba6eca15
2 changed files with 6 additions and 6 deletions

View File

@ -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

View File

@ -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