From 5834aaa5ceba8caa562842b45e91ab9acf2c4e42 Mon Sep 17 00:00:00 2001 From: Omswastik-11 Date: Mon, 29 Dec 2025 17:14:35 +0530 Subject: [PATCH] add openmlauthentication Error exception --- openml/_api_calls.py | 10 +++------- openml/exceptions.py | 23 +++++++++++++++++++++++ tests/test_openml/test_api_calls.py | 2 +- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/openml/_api_calls.py b/openml/_api_calls.py index 81296b3da..f0c0941de 100644 --- a/openml/_api_calls.py +++ b/openml/_api_calls.py @@ -23,8 +23,8 @@ from . import config from .__version__ import __version__ from .exceptions import ( + OpenMLAuthenticationError, OpenMLHashException, - OpenMLNotAuthorizedError, OpenMLServerError, OpenMLServerException, OpenMLServerNoResult, @@ -516,11 +516,7 @@ def __parse_server_exception( 400, # run/42 delete 460, # task/42 delete ]: - msg = ( - f"The API call {url} requires authentication via an API key.\nPlease configure " - "OpenML-Python to use your API as described in this example:" - "\nhttps://openml.github.io/openml-python/latest/examples/Basics/introduction_tutorial/#authentication" - ) - return OpenMLNotAuthorizedError(message=msg) + msg = f"The API call {url} requires authentication via an API key." + return OpenMLAuthenticationError(message=msg) return OpenMLServerException(code=code, message=full_message, url=url) diff --git a/openml/exceptions.py b/openml/exceptions.py index fe63b8a58..1c1343ff3 100644 --- a/openml/exceptions.py +++ b/openml/exceptions.py @@ -63,5 +63,28 @@ class OpenMLNotAuthorizedError(OpenMLServerError): """Indicates an authenticated user is not authorized to execute the requested action.""" +class OpenMLAuthenticationError(OpenMLServerError): + """Exception raised when API authentication fails. + + This typically occurs when: + - No API key is configured + - The API key is invalid or expired + - The API key format is incorrect + + This is different from authorization (OpenMLNotAuthorizedError), which occurs + when a valid API key lacks permissions for the requested operation. + """ + + def __init__(self, message: str): + help_text = ( + "\n\nTo fix this:\n" + "1. Get your API key from https://www.openml.org/\n" + " (you'll need to register for a free account if you don't have one)\n" + "2. Configure your API key by following the authentication guide:\n" + " https://openml.github.io/openml-python/latest/examples/Basics/introduction_tutorial/#authentication" + ) + super().__init__(message + help_text) + + class ObjectNotPublishedError(PyOpenMLError): """Indicates an object has not been published yet.""" diff --git a/tests/test_openml/test_api_calls.py b/tests/test_openml/test_api_calls.py index da6857b6e..d5bd3a72c 100644 --- a/tests/test_openml/test_api_calls.py +++ b/tests/test_openml/test_api_calls.py @@ -121,5 +121,5 @@ def test_authentication_endpoints_requiring_api_key_show_relevant_help_link( ) -> None: # We need to temporarily disable the API key to test the error message with openml.config.overwrite_config_context({"apikey": None}): - with pytest.raises(openml.exceptions.OpenMLNotAuthorizedError, match=API_TOKEN_HELP_LINK): + with pytest.raises(openml.exceptions.OpenMLAuthenticationError, match=API_TOKEN_HELP_LINK): openml._api_calls._perform_api_call(call=endpoint, request_method=method, data=None)