deprecation.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from __future__ import annotations
  2. from typing import TYPE_CHECKING
  3. from typing_extensions import TypeAlias
  4. import wandb
  5. from wandb.sdk.lib import telemetry
  6. if TYPE_CHECKING:
  7. from wandb.proto.wandb_telemetry_pb2 import Deprecated
  8. UNSET: DoNotSet = object()
  9. """A temporary default value for removing formerly optional parameters.
  10. This helps distinguish explicit None arguments from implicit None.
  11. Usage:
  12. def myfunction(old_param: DoNotSet = UNSET) -> ...:
  13. if old_param is not UNSET:
  14. warn_user()
  15. else:
  16. # Set to None if checked in downstream logic,
  17. # since UNSET is not None.
  18. old_param = None
  19. ...
  20. myfunction() # Does not warn
  21. myfunction(old_param=None) # Warns
  22. """
  23. DoNotSet: TypeAlias = object
  24. """The type of UNSET."""
  25. def warn_and_record_deprecation(
  26. *,
  27. feature: Deprecated,
  28. message: str,
  29. run: wandb.Run | None = None,
  30. ) -> None:
  31. """Warn the user that a feature has been deprecated and update telemetry.
  32. Args:
  33. feature: A Deprecated protobuf message with the relevant field set to True.
  34. message: The deprecation warning message to display to the user.
  35. run: The run whose telemetry to update.
  36. """
  37. with telemetry.context(run=run or wandb.run) as tel:
  38. tel.deprecated.MergeFrom(feature)
  39. wandb.termwarn(message, repeat=False)