potiuk commented on code in PR #26153: URL: https://github.com/apache/airflow/pull/26153#discussion_r962781850
########## airflow/utils/deprecation_tools.py: ########## @@ -0,0 +1,46 @@ +# 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. + +import functools +import importlib +import sys +import warnings +from types import ModuleType +from typing import Dict + + +def getattr_with_deprecation(imports: Dict[str, str], module: str, name: str): + target_class_full_name = imports.get(name) + if not target_class_full_name: + raise AttributeError(f"The module `{module!r}` has no attribute `{name!r}`") + warnings.warn( + f"The `{module}.{name}` class is deprecated. Please use `{target_class_full_name!r}`.", + DeprecationWarning, + stacklevel=2, + ) + new_module, new_class_name = target_class_full_name.rsplit('.', 1) + return getattr(importlib.import_module(new_module), new_class_name) + + +def add_deprecated_classes(module_imports: Dict[str, Dict[str, str]], module: str): + for submodule_name, imports in module_imports.items(): + module_type = ModuleType(f"{module}.{submodule_name}") Review Comment: Tried it, and it does not work, module loader does not use `__getattr__`, it uses much more complex machinery to find modules. By default (with defalt modulespec and loader) it expects the Package object (which should be added to `sys.modules[package])' to return `__path__` attribiute (list of paths to search). I even went as far as creating this dynamically created package but then I have no `__paths__` to provide it to :). I think we would have to hack and change the loader implementation to not search in `__path__` but implement ModuleSpec based on https://peps.python.org/pep-0451 I believe. This would also eventually enable complete removal of sub-packages in contrib, but I somehow feel it's not worth it as it's a bit too magical IMHO and we do not need the whole machinery to reload the modules which is behind it). The nice thing about this solution is that it's fairly straightforward to get how it works and it's pretty much based on standards (PEP 562 and the fact that all modules should land in sys.modules). -- 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]
