From 1d0248d8bf3cefeed44994cdc93bd2cb7b57fbaa Mon Sep 17 00:00:00 2001 From: svlandeg Date: Mon, 21 Aug 2023 15:52:26 +0200 Subject: [PATCH] warn when an unsupported/unknown key is given to the dependency matcher --- spacy/errors.py | 1 + spacy/matcher/dependencymatcher.pyx | 8 ++++++++ spacy/tests/matcher/test_dependency_matcher.py | 5 +++++ 3 files changed, 14 insertions(+) diff --git a/spacy/errors.py b/spacy/errors.py index 14ec669a308..dac07f80409 100644 --- a/spacy/errors.py +++ b/spacy/errors.py @@ -219,6 +219,7 @@ class Warnings(metaclass=ErrorsWithCodes): W125 = ("The StaticVectors key_attr is no longer used. To set a custom " "key attribute for vectors, configure it through Vectors(attr=) or " "'spacy init vectors --attr'") + W126 = ("These keys are unsupported: {unsupported}") class Errors(metaclass=ErrorsWithCodes): diff --git a/spacy/matcher/dependencymatcher.pyx b/spacy/matcher/dependencymatcher.pyx index 348e000ffb2..1f66d99b222 100644 --- a/spacy/matcher/dependencymatcher.pyx +++ b/spacy/matcher/dependencymatcher.pyx @@ -129,6 +129,7 @@ cdef class DependencyMatcher: else: required_keys = {"RIGHT_ID", "RIGHT_ATTRS", "REL_OP", "LEFT_ID"} relation_keys = set(relation.keys()) + # Identify required keys that have not been specified missing = required_keys - relation_keys if missing: missing_txt = ", ".join(list(missing)) @@ -136,6 +137,13 @@ cdef class DependencyMatcher: required=required_keys, missing=missing_txt )) + # Identify additional, unsupported keys + unsupported = relation_keys - required_keys + if unsupported: + unsupported_txt = ", ".join(list(unsupported)) + warnings.warn(Warnings.W126.format( + unsupported=unsupported_txt + )) if ( relation["RIGHT_ID"] in visited_nodes or relation["LEFT_ID"] not in visited_nodes diff --git a/spacy/tests/matcher/test_dependency_matcher.py b/spacy/tests/matcher/test_dependency_matcher.py index 44b3bb26b0e..be33f90cf44 100644 --- a/spacy/tests/matcher/test_dependency_matcher.py +++ b/spacy/tests/matcher/test_dependency_matcher.py @@ -216,6 +216,11 @@ def test_dependency_matcher_pattern_validation(en_vocab): pattern2 = copy.deepcopy(pattern) pattern2[1]["RIGHT_ID"] = "fox" matcher.add("FOUNDED", [pattern2]) + # invalid key + with pytest.warns(UserWarning): + pattern2 = copy.deepcopy(pattern) + pattern2[1]["FOO"] = "BAR" + matcher.add("FOUNDED", [pattern2]) def test_dependency_matcher_callback(en_vocab, doc):