scopes.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """Scopes in which a W&B Automation can be triggered."""
  2. from __future__ import annotations
  3. from typing import Annotated, Literal, Union
  4. from pydantic import BeforeValidator, Field
  5. from typing_extensions import TypeAlias, get_args
  6. from wandb._pydantic import GQLBase
  7. from ._generated import (
  8. ArtifactPortfolioScopeFields,
  9. ArtifactSequenceScopeFields,
  10. ProjectScopeFields,
  11. )
  12. from ._validators import LenientStrEnum, parse_scope
  13. # NOTE: Re-defined publicly with a more readable name for easier access
  14. class ScopeType(LenientStrEnum):
  15. """The kind of scope that triggers an automation."""
  16. PROJECT = "PROJECT"
  17. ARTIFACT_COLLECTION = "ARTIFACT_COLLECTION"
  18. class _BaseScope(GQLBase):
  19. scope_type: Annotated[ScopeType, Field(frozen=True)]
  20. class _ArtifactSequenceScope(_BaseScope, ArtifactSequenceScopeFields):
  21. """An automation scope defined by a specific `ArtifactSequence`."""
  22. scope_type: Literal[ScopeType.ARTIFACT_COLLECTION] = ScopeType.ARTIFACT_COLLECTION
  23. class _ArtifactPortfolioScope(_BaseScope, ArtifactPortfolioScopeFields):
  24. """Automation scope defined by an `ArtifactPortfolio` (e.g. a registry collection)."""
  25. scope_type: Literal[ScopeType.ARTIFACT_COLLECTION] = ScopeType.ARTIFACT_COLLECTION
  26. # for type annotations
  27. ArtifactCollectionScope = Annotated[
  28. Union[_ArtifactSequenceScope, _ArtifactPortfolioScope],
  29. BeforeValidator(parse_scope),
  30. Field(discriminator="typename__"),
  31. ]
  32. """An automation scope defined by a specific `ArtifactCollection`."""
  33. # for runtime type checks
  34. ArtifactCollectionScopeTypes: tuple[type, ...] = get_args(
  35. ArtifactCollectionScope.__origin__ # type: ignore[attr-defined]
  36. )
  37. class ProjectScope(_BaseScope, ProjectScopeFields):
  38. """An automation scope defined by a specific `Project`."""
  39. scope_type: Literal[ScopeType.PROJECT] = ScopeType.PROJECT
  40. # for type annotations
  41. AutomationScope: TypeAlias = Annotated[
  42. Union[_ArtifactSequenceScope, _ArtifactPortfolioScope, ProjectScope],
  43. BeforeValidator(parse_scope),
  44. Field(discriminator="typename__"),
  45. ]
  46. # for runtime type checks
  47. AutomationScopeTypes: tuple[type, ...] = get_args(AutomationScope.__origin__) # type: ignore[attr-defined]
  48. __all__ = [
  49. "ScopeType",
  50. "ArtifactCollectionScope",
  51. "ProjectScope",
  52. ]