From c1dfaeef783b6e9e3e06cac46a680c177f140555 Mon Sep 17 00:00:00 2001 From: Tony Meyer Date: Thu, 10 Oct 2024 15:19:39 +1300 Subject: [PATCH 1/2] Remove compatibility code with older ops. --- testing/src/context.py | 8 +------ testing/src/mocking.py | 45 +++++++++++++----------------------- testing/src/ops_main_mock.py | 44 +++++++++++------------------------ 3 files changed, 30 insertions(+), 67 deletions(-) diff --git a/testing/src/context.py b/testing/src/context.py index 5ecf4a57f..9b26ba03d 100644 --- a/testing/src/context.py +++ b/testing/src/context.py @@ -41,14 +41,8 @@ _Event, ) -# TODO: Remove the line below after the ops compatibility code is removed. -# pyright: reportUnnecessaryTypeIgnoreComment=false - if TYPE_CHECKING: # pragma: no cover - try: - from ops._private.harness import ExecArgs # type: ignore - except ImportError: - from ops.testing import ExecArgs # type: ignore + from ops._private.harness import ExecArgs from scenario.ops_main_mock import Ops from scenario.state import ( diff --git a/testing/src/mocking.py b/testing/src/mocking.py index 7eb041cd5..b18e61733 100644 --- a/testing/src/mocking.py +++ b/testing/src/mocking.py @@ -24,12 +24,7 @@ from ops import JujuVersion, pebble -# TODO: Remove the line below after the ops compatibility code is removed. -# pyright: reportUnnecessaryTypeIgnoreComment=false -try: - from ops._private.harness import ExecArgs, _TestingPebbleClient # type: ignore -except ImportError: - from ops.testing import ExecArgs, _TestingPebbleClient # type: ignore +from ops._private.harness import ExecArgs, _TestingPebbleClient from ops.model import CloudSpec as CloudSpec_Ops from ops.model import ModelError @@ -148,7 +143,7 @@ def opened_ports(self) -> Set[Port_Ops]: def open_port( self, - protocol: "_RawPortProtocolLiteral", # pyright: ignore[reportIncompatibleMethodOverride] + protocol: "_RawPortProtocolLiteral", port: Optional[int] = None, ): # fixme: the charm will get hit with a StateValidationError @@ -218,20 +213,12 @@ def _get_secret(self, id: Optional[str] = None, label: Optional[str] = None): # in scenario, you can create Secret(id="foo"), # but ops.Secret will prepend a "secret:" prefix to that ID. # we allow getting secret by either version. - try: - secrets = [ - s - for s in self._state.secrets - if canonicalize_id(s.id, model_uuid=self._state.model.uuid) # type: ignore - == canonicalize_id(id, model_uuid=self._state.model.uuid) # type: ignore - ] - except TypeError: - # ops 2.16 - secrets = [ - s - for s in self._state.secrets - if canonicalize_id(s.id) == canonicalize_id(id) # type: ignore - ] + secrets = [ + s + for s in self._state.secrets + if canonicalize_id(s.id, model_uuid=self._state.model.uuid) + == canonicalize_id(id, model_uuid=self._state.model.uuid) + ] if not secrets: raise SecretNotFoundError(id) return secrets[0] @@ -445,7 +432,7 @@ def secret_get( secret = self._get_secret(id, label) # If both the id and label are provided, then update the label. if id is not None and label is not None: - secret._set_label(label) # type: ignore + secret._set_label(label) juju_version = self._context.juju_version if not (juju_version == "3.1.7" or juju_version >= "3.3.1"): # In this medieval Juju chapter, @@ -471,7 +458,7 @@ def secret_info_get( secret = self._get_secret(id, label) # If both the id and label are provided, then update the label. if id is not None and label is not None: - secret._set_label(label) # type: ignore + secret._set_label(label) # only "manage"=write access level can read secret info self._check_can_manage_secret(secret) @@ -860,17 +847,17 @@ def exec( ) if stdin is None: - proc_stdin = self._transform_exec_handler_output("", encoding) # type: ignore + proc_stdin = self._transform_exec_handler_output("", encoding) else: proc_stdin = None stdin = stdin.read() if hasattr(stdin, "read") else stdin # type: ignore if stdout is None: - proc_stdout = self._transform_exec_handler_output(handler.stdout, encoding) # type: ignore + proc_stdout = self._transform_exec_handler_output(handler.stdout, encoding) else: proc_stdout = None stdout.write(handler.stdout) if stderr is None: - proc_stderr = self._transform_exec_handler_output(handler.stderr, encoding) # type: ignore + proc_stderr = self._transform_exec_handler_output(handler.stderr, encoding) else: proc_stderr = None stderr.write(handler.stderr) @@ -900,9 +887,9 @@ def exec( change_id=change_id, args=args, return_code=handler.return_code, - stdin=proc_stdin, # type: ignore - stdout=proc_stdout, # type: ignore - stderr=proc_stderr, # type: ignore + stdin=proc_stdin, + stdout=proc_stdout, + stderr=proc_stderr, ), ) diff --git a/testing/src/ops_main_mock.py b/testing/src/ops_main_mock.py index 324b2f9e5..0355dc406 100644 --- a/testing/src/ops_main_mock.py +++ b/testing/src/ops_main_mock.py @@ -10,18 +10,14 @@ import ops.charm import ops.framework +import ops.jujucontext import ops.model import ops.storage from ops import CharmBase # use logger from ops._main so that juju_log will be triggered -try: - from ops._main import CHARM_STATE_FILE, _Dispatcher, _get_event_args # type: ignore - from ops._main import logger as ops_logger # type: ignore -except ImportError: - # Ops 2.16 - from ops.main import CHARM_STATE_FILE, _Dispatcher, _get_event_args # type: ignore - from ops.main import logger as ops_logger # type: ignore +from ops._main import CHARM_STATE_FILE, _Dispatcher, _get_event_args +from ops._main import logger as ops_logger from ops.charm import CharmMeta from ops.log import setup_root_logging @@ -32,8 +28,6 @@ from scenario.state import CharmType, State, _CharmSpec, _Event # pyright: reportPrivateUsage=false -# TODO: Remove the line below after the ops compatibility code is removed. -# pyright: reportUnnecessaryTypeIgnoreComment=false # TODO: Use ops.jujucontext's _JujuContext.charm_dir. @@ -88,17 +82,11 @@ def _emit_charm_event( f"invalid event (not on charm.on).", ) - try: - args, kwargs = _get_event_args(charm, event_to_emit) # type: ignore - except TypeError: - # ops 2.16+ - import ops.jujucontext # type: ignore - - args, kwargs = _get_event_args( - charm, - event_to_emit, - ops.jujucontext._JujuContext.from_dict(os.environ), # type: ignore - ) + args, kwargs = _get_event_args( + charm, + event_to_emit, + ops.jujucontext._JujuContext.from_dict(os.environ), + ) ops_logger.debug("Emitting Juju event %s.", event_name) event_to_emit.emit(*args, **kwargs) @@ -126,7 +114,7 @@ def setup_framework( ops_logger.debug( "Operator Framework %s up and running.", ops.__version__, - ) # type:ignore + ) metadata = (charm_dir / "metadata.yaml").read_text() actions_meta = charm_dir / "actions.yaml" @@ -189,16 +177,10 @@ def setup( charm_class = charm_spec.charm_type charm_dir = _get_charm_dir() - try: - dispatcher = _Dispatcher(charm_dir) # type: ignore - except TypeError: - # ops 2.16+ - import ops.jujucontext # type: ignore - - dispatcher = _Dispatcher( - charm_dir, - ops.jujucontext._JujuContext.from_dict(os.environ), # type: ignore - ) + dispatcher = _Dispatcher( + charm_dir, + ops.jujucontext._JujuContext.from_dict(os.environ), + ) dispatcher.run_any_legacy_hook() framework = setup_framework(charm_dir, state, event, context, charm_spec) From 09337cada0a99345e9701faed41541959651f322 Mon Sep 17 00:00:00 2001 From: Tony Meyer Date: Thu, 10 Oct 2024 17:53:36 +1300 Subject: [PATCH 2/2] Remove extraneous blank lines. --- testing/src/context.py | 1 - testing/src/mocking.py | 2 -- 2 files changed, 3 deletions(-) diff --git a/testing/src/context.py b/testing/src/context.py index 9b26ba03d..143a6e3fa 100644 --- a/testing/src/context.py +++ b/testing/src/context.py @@ -43,7 +43,6 @@ if TYPE_CHECKING: # pragma: no cover from ops._private.harness import ExecArgs - from scenario.ops_main_mock import Ops from scenario.state import ( AnyJson, diff --git a/testing/src/mocking.py b/testing/src/mocking.py index b18e61733..534b4cabc 100644 --- a/testing/src/mocking.py +++ b/testing/src/mocking.py @@ -23,9 +23,7 @@ ) from ops import JujuVersion, pebble - from ops._private.harness import ExecArgs, _TestingPebbleClient - from ops.model import CloudSpec as CloudSpec_Ops from ops.model import ModelError from ops.model import Port as Port_Ops