metadata.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from __future__ import annotations
  18. import warnings
  19. from typing import TYPE_CHECKING, Any
  20. import attrs
  21. from airflow.datasets import DatasetAlias, extract_event_key
  22. if TYPE_CHECKING:
  23. from airflow.datasets import Dataset
  24. @attrs.define(init=False)
  25. class Metadata:
  26. """Metadata to attach to a DatasetEvent."""
  27. uri: str
  28. extra: dict[str, Any]
  29. alias_name: str | None = None
  30. def __init__(
  31. self, target: str | Dataset, extra: dict[str, Any], alias: DatasetAlias | str | None = None
  32. ) -> None:
  33. if isinstance(target, str):
  34. warnings.warn(
  35. (
  36. "Accessing outlet_events using string is deprecated and will be removed in Airflow 3. "
  37. "Please use the Dataset or DatasetAlias object (renamed as Asset and AssetAlias in Airflow 3) directly"
  38. ),
  39. DeprecationWarning,
  40. stacklevel=2,
  41. )
  42. self.uri = extract_event_key(target)
  43. self.extra = extra
  44. if isinstance(alias, DatasetAlias):
  45. self.alias_name = alias.name
  46. else:
  47. warnings.warn(
  48. (
  49. "Emitting dataset events using string is deprecated and will be removed in Airflow 3. "
  50. "Please use the Dataset object (renamed as Asset in Airflow 3) directly"
  51. ),
  52. DeprecationWarning,
  53. stacklevel=2,
  54. )
  55. self.alias_name = alias