Skip to content

Commit 6196ac2

Browse files
drbhWauplin
andcommitted
Add type to job owner (#3291)
* fix: add type to job owner * fix: add avatarUrl prop too * fix: bump cli test payload * Do not break if new fields are added --------- Co-authored-by: Lucain Pouget <[email protected]>
1 parent 4f6975f commit 6196ac2

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/huggingface_hub/_jobs_api.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,12 @@ class JobStatus:
4747
stage: JobStage
4848
message: Optional[str]
4949

50-
def __init__(self, **kwargs) -> None:
51-
self.stage = kwargs["stage"]
52-
self.message = kwargs.get("message")
53-
5450

5551
@dataclass
5652
class JobOwner:
5753
id: str
5854
name: str
55+
type: str
5956

6057

6158
@dataclass
@@ -89,7 +86,7 @@ class JobInfo:
8986
Status of the Job, e.g. `JobStatus(stage="RUNNING", message=None)`
9087
See [`JobStage`] for possible stage values.
9188
status: (`JobOwner` or `None`):
92-
Owner of the Job, e.g. `JobOwner(id="5e9ecfc04957053f60648a3e", name="lhoestq")`
89+
Owner of the Job, e.g. `JobOwner(id="5e9ecfc04957053f60648a3e", name="lhoestq", type="user")`
9390
9491
Example:
9592
@@ -100,7 +97,7 @@ class JobInfo:
10097
... command=["python", "-c", "print('Hello from the cloud!')"]
10198
... )
10299
>>> job
103-
JobInfo(id='687fb701029421ae5549d998', created_at=datetime.datetime(2025, 7, 22, 16, 6, 25, 79000, tzinfo=datetime.timezone.utc), docker_image='python:3.12', space_id=None, command=['python', '-c', "print('Hello from the cloud!')"], arguments=[], environment={}, secrets={}, flavor='cpu-basic', status=JobStatus(stage='RUNNING', message=None), owner=JobOwner(id='5e9ecfc04957053f60648a3e', name='lhoestq'), endpoint='https://huggingface.co', url='https://huggingface.co/jobs/lhoestq/687fb701029421ae5549d998')
100+
JobInfo(id='687fb701029421ae5549d998', created_at=datetime.datetime(2025, 7, 22, 16, 6, 25, 79000, tzinfo=datetime.timezone.utc), docker_image='python:3.12', space_id=None, command=['python', '-c', "print('Hello from the cloud!')"], arguments=[], environment={}, secrets={}, flavor='cpu-basic', status=JobStatus(stage='RUNNING', message=None), owner=JobOwner(id='5e9ecfc04957053f60648a3e', name='lhoestq', type='user'), endpoint='https://huggingface.co', url='https://huggingface.co/jobs/lhoestq/687fb701029421ae5549d998')
104101
>>> job.id
105102
'687fb701029421ae5549d998'
106103
>>> job.url
@@ -119,8 +116,8 @@ class JobInfo:
119116
environment: Optional[Dict[str, Any]]
120117
secrets: Optional[Dict[str, Any]]
121118
flavor: Optional[SpaceHardware]
122-
status: Optional[JobStatus]
123-
owner: Optional[JobOwner]
119+
status: JobStatus
120+
owner: JobOwner
124121

125122
# Inferred fields
126123
endpoint: str
@@ -132,13 +129,15 @@ def __init__(self, **kwargs) -> None:
132129
self.created_at = parse_datetime(created_at) if created_at else None
133130
self.docker_image = kwargs.get("dockerImage") or kwargs.get("docker_image")
134131
self.space_id = kwargs.get("spaceId") or kwargs.get("space_id")
135-
self.owner = JobOwner(**(kwargs["owner"] if isinstance(kwargs.get("owner"), dict) else {}))
132+
owner = kwargs.get("owner", {})
133+
self.owner = JobOwner(id=owner["id"], name=owner["name"], type=owner["type"])
136134
self.command = kwargs.get("command")
137135
self.arguments = kwargs.get("arguments")
138136
self.environment = kwargs.get("environment")
139137
self.secrets = kwargs.get("secrets")
140138
self.flavor = kwargs.get("flavor")
141-
self.status = JobStatus(**(kwargs["status"] if isinstance(kwargs.get("status"), dict) else {}))
139+
status = kwargs.get("status", {})
140+
self.status = JobStatus(stage=status["stage"], message=status.get("message"))
142141

143142
# Inferred fields
144143
self.endpoint = kwargs.get("endpoint", constants.ENDPOINT)

tests/test_cli.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,15 @@ def setUp(self) -> None:
851851
@patch(
852852
"requests.Session.post",
853853
return_value=DummyResponse(
854-
{"id": "my-job-id", "owner": {"id": "userid", "name": "my-username"}, "status": {"stage": "RUNNING"}}
854+
{
855+
"id": "my-job-id",
856+
"owner": {
857+
"id": "userid",
858+
"name": "my-username",
859+
"type": "user",
860+
},
861+
"status": {"stage": "RUNNING"},
862+
}
855863
),
856864
)
857865
@patch("huggingface_hub.hf_api.HfApi.whoami", return_value={"name": "my-username"})

0 commit comments

Comments
 (0)