From 91f6d74f2fdeef55b5e60081d34bddfce4def093 Mon Sep 17 00:00:00 2001 From: annatisch Date: Fri, 29 Oct 2021 18:23:27 -0700 Subject: [PATCH] Improved some error behaviour (#21499) --- .../ai/language/questionanswering/_patch.py | 5 ++-- .../tests/test_query_knowledgebase.py | 13 ++++++++++ .../tests/test_query_text.py | 26 +++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/sdk/cognitivelanguage/azure-ai-language-questionanswering/azure/ai/language/questionanswering/_patch.py b/sdk/cognitivelanguage/azure-ai-language-questionanswering/azure/ai/language/questionanswering/_patch.py index 8ac829ddd279..405303abc494 100644 --- a/sdk/cognitivelanguage/azure-ai-language-questionanswering/azure/ai/language/questionanswering/_patch.py +++ b/sdk/cognitivelanguage/azure-ai-language-questionanswering/azure/ai/language/questionanswering/_patch.py @@ -37,10 +37,9 @@ def _get_positional_body(*args, **kwargs): """Verify args and kwargs are valid, and then return the positional body, if users passed it in.""" if len(args) > 1: raise TypeError("There can only be one positional argument, which is the POST body of this request.") - if args and "options" in kwargs: + if "options" in kwargs: raise TypeError( - "You have already supplied the request body as a positional parameter, " - "you can not supply it as a keyword argument as well." + "The 'options' parameter is positional only." ) return args[0] if args else None diff --git a/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/test_query_knowledgebase.py b/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/test_query_knowledgebase.py index ed32bdddc3e5..e697aae4c797 100644 --- a/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/test_query_knowledgebase.py +++ b/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/test_query_knowledgebase.py @@ -355,6 +355,19 @@ def test_query_knowledgebase_overload_positional_and_kwarg(self): client.get_answers("positional_one", "positional_two") with pytest.raises(TypeError): client.get_answers("positional_options_bag", options="options bag by name") + with pytest.raises(TypeError): + client.get_answers( + options={'qnaId': 15}, + project_name="hello", + deployment_name='test' + ) + with pytest.raises(TypeError): + client.get_answers( + {'qnaId': 15}, + question='Why?', + project_name="hello", + deployment_name='test' + ) def test_query_knowledgebase_question_or_qna_id(self): with QuestionAnsweringClient("http://fake.com", AzureKeyCredential("123")) as client: diff --git a/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/test_query_text.py b/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/test_query_text.py index 8c77a64c3610..321b5444e085 100644 --- a/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/test_query_text.py +++ b/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/test_query_text.py @@ -187,3 +187,29 @@ def test_query_text_overload_positional_and_kwarg(self): client.get_answers_from_text("positional_one", "positional_two") with pytest.raises(TypeError): client.get_answers_from_text("positional_options_bag", options="options bag by name") + + params = AnswersFromTextOptions( + question="What is the meaning of life?", + text_documents=[ + TextDocument( + text="foo", + id="doc1" + ), + TextDocument( + text="bar", + id="doc2" + ) + ], + language="en" + ) + with pytest.raises(TypeError): + client.get_answers_from_text(options=params) + + with pytest.raises(TypeError): + client.get_answers_from_text( + question="why?", + text_documents=["foo", "bar"], + options=params) + + with pytest.raises(TypeError): + client.get_answers_from_text(params, question="Why?") \ No newline at end of file