Coverage for /usr/local/lib/python3.12/site-packages/prefect/settings/models/cloud.py: 89%

28 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-12-05 13:38 +0000

1import re 1a

2from typing import ClassVar, Optional 1a

3 

4from pydantic import Field, model_validator 1a

5from pydantic_settings import SettingsConfigDict 1a

6from typing_extensions import Self 1a

7 

8from prefect.settings.base import ( 1a

9 PrefectBaseSettings, 

10 build_settings_config, 

11) 

12 

13 

14def default_cloud_ui_url(settings: "CloudSettings") -> Optional[str]: 1a

15 value = settings.ui_url 1a

16 if value is not None: 16 ↛ 17line 16 didn't jump to line 17 because the condition on line 16 was never true1a

17 return value 

18 

19 # Otherwise, infer a value from the API URL 

20 ui_url = api_url = settings.api_url 1a

21 

22 if re.match(r"^https://api[\.\w]*.prefect.[^\.]+/", api_url): 22 ↛ 25line 22 didn't jump to line 25 because the condition on line 22 was always true1a

23 ui_url = ui_url.replace("https://api", "https://app", 1) 1a

24 

25 if ui_url.endswith("/api"): 25 ↛ 28line 25 didn't jump to line 28 because the condition on line 25 was always true1a

26 ui_url = ui_url[:-4] 1a

27 

28 return ui_url 1a

29 

30 

31class CloudSettings(PrefectBaseSettings): 1a

32 """ 

33 Settings for interacting with Prefect Cloud 

34 """ 

35 

36 model_config: ClassVar[SettingsConfigDict] = build_settings_config(("cloud",)) 1a

37 

38 api_url: str = Field( 1a

39 default="https://api.prefect.cloud/api", 

40 description="API URL for Prefect Cloud. Used for authentication with Prefect Cloud.", 

41 ) 

42 

43 enable_orchestration_telemetry: bool = Field( 1a

44 default=True, 

45 description="Whether or not to enable orchestration telemetry.", 

46 ) 

47 

48 max_log_size: int = Field( 1a

49 default=25_000, 

50 description="Maximum size in characters for a single log when sending logs to Prefect Cloud.", 

51 ) 

52 

53 ui_url: Optional[str] = Field( 1a

54 default=None, 

55 description="The URL of the Prefect Cloud UI. If not set, the client will attempt to infer it.", 

56 ) 

57 

58 @model_validator(mode="after") 1a

59 def post_hoc_settings(self) -> Self: 1a

60 """refactor on resolution of https://github.com/pydantic/pydantic/issues/9789 

61 

62 we should not be modifying __pydantic_fields_set__ directly, but until we can 

63 define dependencies between defaults in a first-class way, we need clean up 

64 post-hoc default assignments to keep set/unset fields correct after instantiation. 

65 """ 

66 if self.ui_url is None: 1a

67 self.ui_url = default_cloud_ui_url(self) 1a

68 self.__pydantic_fields_set__.remove("ui_url") 1a

69 return self 1a