Skip to content

Commit f6a4b80

Browse files
Better errors for has_annotation and Matcher (explosion#10830)
* Show input argument instead of None * catch invalid attr early * moved error message from code to errors.py * Update spacy/errors.py Co-authored-by: Adriane Boyd <adrianeboyd@gmail.com> * Update spacy/errors.py * update E153 and E154 Co-authored-by: Adriane Boyd <adrianeboyd@gmail.com>
1 parent 83ed1f3 commit f6a4b80

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

spacy/errors.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,10 @@ class Errors(metaclass=ErrorsWithCodes):
449449
"same, but found '{nlp}' and '{vocab}' respectively.")
450450
E152 = ("The attribute {attr} is not supported for token patterns. "
451451
"Please use the option `validate=True` with the Matcher, PhraseMatcher, "
452-
"or EntityRuler for more details.")
452+
"EntityRuler or AttributeRuler for more details.")
453453
E153 = ("The value type {vtype} is not supported for token patterns. "
454454
"Please use the option validate=True with Matcher, PhraseMatcher, "
455-
"or EntityRuler for more details.")
455+
"EntityRuler or AttributeRuler for more details.")
456456
E154 = ("One of the attributes or values is not supported for token "
457457
"patterns. Please use the option `validate=True` with the Matcher, "
458458
"PhraseMatcher, or EntityRuler for more details.")
@@ -918,6 +918,7 @@ class Errors(metaclass=ErrorsWithCodes):
918918
E1034 = ("Node index {i} out of bounds ({length})")
919919
E1035 = ("Token index {i} out of bounds ({length})")
920920
E1036 = ("Cannot index into NoneNode")
921+
E1037 = ("Invalid attribute value '{attr}'.")
921922

922923

923924
# Deprecated model shortcuts, only used in errors and warnings

spacy/matcher/matcher.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ def _preprocess_pattern(token_specs, vocab, extensions_table, extra_predicates):
786786
def _get_attr_values(spec, string_store):
787787
attr_values = []
788788
for attr, value in spec.items():
789+
input_attr = attr
789790
if isinstance(attr, str):
790791
attr = attr.upper()
791792
if attr == '_':
@@ -814,7 +815,7 @@ def _get_attr_values(spec, string_store):
814815
attr_values.append((attr, value))
815816
else:
816817
# should be caught in validation
817-
raise ValueError(Errors.E152.format(attr=attr))
818+
raise ValueError(Errors.E152.format(attr=input_attr))
818819
return attr_values
819820

820821

spacy/tokens/doc.pyx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ cdef class Doc:
414414
"""
415415

416416
# empty docs are always annotated
417+
input_attr = attr
417418
if self.length == 0:
418419
return True
419420
cdef int i
@@ -423,6 +424,10 @@ cdef class Doc:
423424
elif attr == "IS_SENT_END" or attr == self.vocab.strings["IS_SENT_END"]:
424425
attr = SENT_START
425426
attr = intify_attr(attr)
427+
if attr is None:
428+
raise ValueError(
429+
Errors.E1037.format(attr=input_attr)
430+
)
426431
# adjust attributes
427432
if attr == HEAD:
428433
# HEAD does not have an unset state, so rely on DEP

0 commit comments

Comments
 (0)