From 2c0d1870f0c8e73440d85c06735a24947803dec7 Mon Sep 17 00:00:00 2001 From: Tony Meyer Date: Wed, 18 Dec 2024 13:12:55 +1300 Subject: [PATCH] Remove the ops.main.main deprecation warning, as per Charm-Tech discussion. --- ops/main.py | 60 ++---------------------------------- test/test_main_invocation.py | 12 -------- 2 files changed, 2 insertions(+), 70 deletions(-) diff --git a/ops/main.py b/ops/main.py index 24978e19c..728d8e88d 100644 --- a/ops/main.py +++ b/ops/main.py @@ -14,10 +14,7 @@ """Support legacy ops.main.main() import.""" -import inspect -import os -import warnings -from typing import Any, Optional, Type, Union +from typing import Optional, Type import ops.charm @@ -32,14 +29,6 @@ ) -def _top_frame(): - frame = inspect.currentframe() - while frame: - if frame.f_back is None: - return frame - frame = frame.f_back - - def main(charm_class: Type[ops.charm.CharmBase], use_juju_for_storage: Optional[bool] = None): """Legacy entrypoint to set up the charm and dispatch the observed event. @@ -48,49 +37,4 @@ def main(charm_class: Type[ops.charm.CharmBase], use_juju_for_storage: Optional[ See `ops.main() <#ops-main-entry-point>`_ for details. """ - # Normally, we would do warnings.warn() with a DeprecationWarning, but at - # this point in the charm execution, the framework has not been set up, so - # we haven't had a chance to direct warnings where we want them to go. That - # means that they'll end up going to stderr, and with actions that means - # they'll end up being displayed to the user. - # This means that we need to delay emitting the warning until the framework - # has been set up, so we wrap the charm and do it on instantiation. However, - # this means that a regular warning call won't provide the correct filename - # and line number. - # Note also that this will be logged with every event. Our assumption is - # that this will be noticeable enough during integration testing that it - # will get fixed before going into production. - frame = _top_frame() - assert frame is not None - - class DeprecatedMainCharmBase(charm_class): - def __init__(self, *args: Any, **kwargs: Any): - super().__init__(*args, **kwargs) - - _original_format = warnings.formatwarning - - def custom_warning_formatter( - message: Union[str, Warning], - category: Type[Warning], - *args: Any, - **kwargs: Any, - ) -> str: - """Like the default formatter, but patch in the filename and line number.""" - return ( - f'{frame.f_code.co_filename}:{frame.f_lineno}: ' - f'{category.__name__}: {message}' - ) - - try: - warnings.formatwarning = custom_warning_formatter - warnings.warn( - 'Calling `ops.main()` is deprecated, call `ops.main()` instead', - DeprecationWarning, - stacklevel=2, - ) - finally: - warnings.formatwarning = _original_format - - return _main.main( - charm_class=DeprecatedMainCharmBase, use_juju_for_storage=use_juju_for_storage - ) + return _main.main(charm_class=charm_class, use_juju_for_storage=use_juju_for_storage) diff --git a/test/test_main_invocation.py b/test/test_main_invocation.py index 4105b3c17..3796d99a2 100644 --- a/test/test_main_invocation.py +++ b/test/test_main_invocation.py @@ -52,9 +52,6 @@ def test_top_level_import(charm_env: None): def test_top_level_import_legacy_call(charm_env: None): import ops - with pytest.deprecated_call(): - ops.main.main(ops.CharmBase) - with pytest.raises(TypeError): ops.main.main() # type: ignore @@ -71,9 +68,6 @@ def test_submodule_import(charm_env: None): def test_submodule_import_legacy_call(charm_env: None): import ops.main - with pytest.deprecated_call(): - ops.main.main(ops.CharmBase) - with pytest.raises(TypeError): ops.main.main() # type: ignore @@ -90,9 +84,6 @@ def test_import_from_top_level_module(charm_env: None): def test_import_from_top_level_module_legacy_call(charm_env: None): from ops import main - with pytest.deprecated_call(): - main.main(ops.CharmBase) - with pytest.raises(TypeError): main.main() # type: ignore @@ -100,8 +91,5 @@ def test_import_from_top_level_module_legacy_call(charm_env: None): def test_legacy_import_from_submodule(charm_env: None): from ops.main import main - with pytest.deprecated_call(): - main(ops.CharmBase) - with pytest.raises(TypeError): main() # type: ignore