Skip to content

Commit

Permalink
Remove the ops.main.main deprecation warning, as per Charm-Tech discu…
Browse files Browse the repository at this point in the history
…ssion.
  • Loading branch information
tonyandrewmeyer committed Dec 18, 2024
1 parent eca45fb commit 2c0d187
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 70 deletions.
60 changes: 2 additions & 58 deletions ops/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -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)
12 changes: 0 additions & 12 deletions test/test_main_invocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -90,18 +84,12 @@ 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


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

0 comments on commit 2c0d187

Please sign in to comment.