Skip to content

Commit 40dd460

Browse files
authored
Merge branch 'main' into issue1564
2 parents e6a92df + 7feb2a3 commit 40dd460

5 files changed

Lines changed: 74 additions & 113 deletions

File tree

openml/_api_calls.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
from .__version__ import __version__
2626
from .exceptions import (
27+
OpenMLAuthenticationError,
2728
OpenMLHashException,
28-
OpenMLNotAuthorizedError,
2929
OpenMLServerError,
3030
OpenMLServerException,
3131
OpenMLServerNoResult,
@@ -518,11 +518,7 @@ def __parse_server_exception(
518518
400, # run/42 delete
519519
460, # task/42 delete
520520
]:
521-
msg = (
522-
f"The API call {url} requires authentication via an API key.\nPlease configure "
523-
"OpenML-Python to use your API as described in this example:"
524-
"\nhttps://openml.github.io/openml-python/latest/examples/Basics/introduction_tutorial/#authentication"
525-
)
526-
return OpenMLNotAuthorizedError(message=msg)
521+
msg = f"The API call {url} requires authentication via an API key."
522+
return OpenMLAuthenticationError(message=msg)
527523

528524
return OpenMLServerException(code=code, message=full_message, url=url)

openml/exceptions.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,28 @@ class OpenMLNotAuthorizedError(OpenMLServerError):
6363
"""Indicates an authenticated user is not authorized to execute the requested action."""
6464

6565

66+
class OpenMLAuthenticationError(OpenMLServerError):
67+
"""Exception raised when API authentication fails.
68+
69+
This typically occurs when:
70+
- No API key is configured
71+
- The API key is invalid or expired
72+
- The API key format is incorrect
73+
74+
This is different from authorization (OpenMLNotAuthorizedError), which occurs
75+
when a valid API key lacks permissions for the requested operation.
76+
"""
77+
78+
def __init__(self, message: str):
79+
help_text = (
80+
"\n\nTo fix this:\n"
81+
"1. Get your API key from https://www.openml.org/\n"
82+
" (you'll need to register for a free account if you don't have one)\n"
83+
"2. Configure your API key by following the authentication guide:\n"
84+
" https://openml.github.io/openml-python/latest/examples/Basics/introduction_tutorial/#authentication"
85+
)
86+
super().__init__(message + help_text)
87+
88+
6689
class ObjectNotPublishedError(PyOpenMLError):
6790
"""Indicates an object has not been published yet."""

openml/tasks/functions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ def get_task(
426426
# Including class labels as part of task meta data handles
427427
# the case where data download was initially disabled
428428
if isinstance(task, (OpenMLClassificationTask, OpenMLLearningCurveTask)):
429+
assert task.target_name is not None, (
430+
"Supervised tasks must define a target feature before retrieving class labels."
431+
)
429432
task.class_labels = dataset.retrieve_class_labels(task.target_name)
430433
# Clustering tasks do not have class labels
431434
# and do not offer download_split
@@ -599,6 +602,7 @@ def create_task(
599602
)
600603

601604
return task_cls(
605+
task_id=None,
602606
task_type_id=task_type,
603607
task_type="None", # TODO: refactor to get task type string from ID.
604608
data_set_id=dataset_id,

0 commit comments

Comments
 (0)