Taragolis commented on code in PR #37320: URL: https://github.com/apache/airflow/pull/37320#discussion_r1486723855
########## airflow/utils/pydantic.py: ########## @@ -0,0 +1,58 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# This is an util module that makes Pydantic use optional. While we are using Pydantic in the airflow core +# codebase, we don't want to make it a hard dependency for all the users of the core codebase, because +# it is only used in the serialization and deserialization of the models for Internal API and for nothing +# else, and since Pydantic is a very popular library, we don't want to force the users of the core codebase +# to install specific Pydantic version - especially that a lot of libraries out there still depend on +# Pydantic 1 and our internal API uses Pydantic 2+ + +from __future__ import annotations + +try: + import pydantic + from packaging.version import Version + + if Version(pydantic.__version__) < Version("2.0.0"): + raise ImportError("Pydantic version is too old. Switch to Pydantic 2+") Review Comment: I guess you would hate me if it would start to talk about `__version__` again 🤣 Maybe we should consider to switch to importlib? I've compare locally and with `pydantic` retrieve the version from the importlib suddenly take less time if compare to `pydantic.__version__`. (Python 3.9, macOS, pydantic 2.4.2) ```python import sys if sys.version_info >= (3, 9): from importlib.metadata import distribution else: from importlib_metadata import distribution try: pydantic_distribution = distribution("pydantic") pydantic_distribution.version except ImportError: pass ``` It took from 9 to 15 ms, during the run in PyCharm Profiler. Regardless is it installed or not  If compare to ```python try: import pydantic pydantic.__version__ except ImportError: pass ``` It took from 43 to 60 ms if `pydantic` installed, and 0-1 ms if not. It is also depend on the version, 1.x took longer than 2.4.x, and 2.6.1 (latest) import fast due to lazy loading into the `__init__.py`  I know this is not such a big different, if compare to Airflow loading time -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
