Skip to content

Commit 48daf5a

Browse files
committed
convert NotificationsConfig to dict
1 parent 9ec5dfa commit 48daf5a

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

jupyter_scheduler/models.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
from enum import Enum
3-
from typing import Dict, List, Optional, Type, Union
3+
from typing import Any, Dict, List, Optional, Type, Union
44

55
from pydantic import BaseModel, root_validator, validator
66

@@ -36,35 +36,38 @@ class NotificationsConfig(BaseModel):
3636
include_output (bool): A flag indicating whether a output should be included in the notification. Default is False.
3737
"""
3838

39-
send_to: List[str]
40-
events: List[NotificationEvent]
39+
send_to: List[str] = []
40+
events: List[NotificationEvent] = []
4141
include_output: bool = False
4242

43+
def to_dict(self):
44+
return self.dict(exclude_none=True)
45+
4346
@validator("send_to")
44-
def validate_send_to(cls, send_to):
45-
if len(send_to) > 100:
47+
def validate_send_to(cls, v):
48+
if len(v) > 100:
4649
raise ValueError("Too many 'Send to' addressee identifiers. Maximum allowed is 100.")
47-
return send_to
50+
return v
4851

4952
@validator("send_to", each_item=True)
50-
def validate_send_to_items(cls, send_to_item):
51-
if len(send_to_item) > 100:
53+
def validate_send_to_items(cls, v):
54+
if len(v) > 100:
5255
raise ValueError(
5356
"Each 'Send to' addressee identifier should be at most 100 characters long."
5457
)
55-
return send_to_item
58+
return v
5659

5760
@validator("events")
58-
def validate_events(cls, send_to):
59-
if len(send_to) > 100:
61+
def validate_events(cls, v):
62+
if len(v) > 100:
6063
raise ValueError("Too many notification events. Maximum allowed is 100.")
61-
return send_to
64+
return v
6265

6366
@validator("events", each_item=True)
64-
def validate_events_items(cls, events_item):
65-
if len(events_item.value) > 100:
67+
def validate_events_items(cls, v):
68+
if len(v.value) > 100:
6669
raise ValueError("Each notification event should be at most 100 characters long.")
67-
return events_item
70+
return v
6871

6972

7073
class RuntimeEnvironment(BaseModel):
@@ -82,8 +85,8 @@ class RuntimeEnvironment(BaseModel):
8285
compute_types: Optional[List[str]]
8386
default_compute_type: Optional[str] # Should be a member of the compute_types list
8487
utc_only: Optional[bool]
85-
notifications_enabled: bool = False
86-
notification_events: List[Type[NotificationEvent]] = []
88+
notifications_enabled: bool = True
89+
notification_events: List[Type[NotificationEvent]] = [e for e in NotificationEvent]
8790

8891
def __str__(self):
8992
return self.json()
@@ -152,6 +155,12 @@ def compute_input_filename(cls, values) -> Dict:
152155

153156
return values
154157

158+
@validator("notifications_config", pre=True, always=True)
159+
def convert_notifications_config(cls, v):
160+
if isinstance(v, NotificationsConfig):
161+
return v.to_dict()
162+
return v
163+
155164

156165
class JobFile(BaseModel):
157166
"""This model is used to describe the display value,

0 commit comments

Comments
 (0)