pydantic.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. # This is an util module that makes Pydantic use optional. While we are using Pydantic in the airflow core
  18. # codebase, we don't want to make it a hard dependency for all the users of the core codebase, because
  19. # it is only used in the serialization and deserialization of the models for Internal API and for nothing
  20. # else, and since Pydantic is a very popular library, we don't want to force the users of the core codebase
  21. # to install specific Pydantic version - especially that a lot of libraries out there still depend on
  22. # Pydantic 1 and our internal API uses Pydantic 2+
  23. from __future__ import annotations
  24. from importlib import metadata
  25. from packaging import version
  26. def is_pydantic_2_installed() -> bool:
  27. try:
  28. return version.parse(metadata.version("pydantic")).major == 2
  29. except ImportError:
  30. return False
  31. if is_pydantic_2_installed():
  32. from pydantic import BaseModel, ConfigDict, PlainSerializer, PlainValidator, ValidationInfo
  33. else:
  34. class BaseModel: # type: ignore[no-redef] # noqa: D101
  35. def __init__(self, *args, **kwargs):
  36. pass
  37. class ConfigDict: # type: ignore[no-redef] # noqa: D101
  38. def __init__(self, *args, **kwargs):
  39. pass
  40. class PlainSerializer: # type: ignore[no-redef] # noqa: D101
  41. def __init__(self, *args, **kwargs):
  42. pass
  43. class PlainValidator: # type: ignore[no-redef] # noqa: D101
  44. def __init__(self, *args, **kwargs):
  45. pass
  46. class ValidationInfo: # type: ignore[no-redef] # noqa: D101
  47. def __init__(self, *args, **kwargs):
  48. pass