feat: deprecate testing.Context.charm_spec#2219
Conversation
|
For the metadata, it seems like a So I'm +0 on deprecating (and also opening a PR for grafana-agent-operator - maybe also charmcraft, although I think it's pinned to an old ops-scenario version, so there might be a need for a larger PR that modernises other things). I'm interested in @PietroPasotti's take here too. Essentially: do you think we should document |
tonyandrewmeyer
left a comment
There was a problem hiding this comment.
Assuming that we do deprecate, the code change looks good to me.
I think we should add a test that validates that there's a warning (pytest.warns) if you access .charm_spec. We probably don't need other tests, assuming that none of them are using .charm_spec directly (if so, we should pytest.warns there to silence the warning, but also see if that's actually a use-case).
|
I've opened PRs to migrate the two users off
I'll update this PR to use @warnings.deprecated and to mention alternatives in the docstring before requesting further review. |
Merge branch 'main' into 25-12+feat+depreate-public-charm-spec-attribute
| dependencies = [ | ||
| "ops==3.5.0.dev0", | ||
| "PyYAML>=6.0.1", | ||
| "typing_extensions>=4.9.0", # for `deprecated` decorator, replace with stdlib `warnings.deprecated` when we're Python 3.13+ |
There was a problem hiding this comment.
warnings.deprecated is Python 3.13+. Luckily, typing_extensions has backported it, and type checkers respect the backport. This introduces a testing-only runtime dependency on typing_extensions (currently it's type checking only in ops), which I think is OK.
|
Turns out it needs to be typing_extensions.deprecated instead since
|
a407ea4
into
canonical:main
…xt.charm_spec (#2527) The `ops.testing` framework currently exposes the private `_CharmSpec` class via the `Context.charm_spec` attribute (canonical/operator#2131). Charm Tech will deprecate this attribute soon (canonical/operator#2219). Charmcraft currently uses `charm_spec` in the `init-extensions` tests, so this PR preemptively modernises the test to use `State.from_context`, avoiding the need to access `Context.charm_spec`. The `init-extensions` tests run only in the Ubuntu 22.04 spread tests. While that job is currently failing, the failing tests do not appear to be the `init-extensions` tests: ``` 2025-12-12 04:44:41 Successful tasks: 50 error: unsuccessful run 2025-12-12 04:44:41 Aborted tasks: 0 2025-12-12 04:44:41 Failed tasks: 3 - google:ubuntu-22.04-64:tests/spread/smoketests/different-dir - google:ubuntu-22.04-64:tests/spread/smoketests/multi-base:basic - google:ubuntu-22.04-64:tests/spread/smoketests/multi-base:one_platform 2025-12-12 04:44:41 Failed task restore: 2 - google:ubuntu-22.04-64:tests/spread/smoketests/multi-base:basic - google:ubuntu-22.04-64:tests/spread/smoketests/multi-base:one_platform ``` <details> <summary>AFAICT the init-extensions tests pass, here's some relevant output from <a href=https://github.com/canonical/charmcraft/actions/runs/20154524656/job/57855410223?pr=2527>this run</a>.</summary> ``` 2025-12-12 03:40:14 Restoring google:ubuntu-22.04-64:tests/spread/commands/init-extensions:springboot (dec120311-758761)... 2025-12-12 03:40:28 Preparing google:ubuntu-22.04-64:tests/spread/smoketests/reactive:stable (dec120311-758761)... 2025-12-12 03:40:29 Executing google:ubuntu-22.04-64:tests/spread/smoketests/reactive:stable (dec120311-758761) (7/53)... 2025-12-12 03:40:34 Restoring google:ubuntu-22.04-64:tests/spread/commands/init-extensions:flask (dec120311-758749)... 2025-12-12 03:40:50 Preparing google:ubuntu-22.04-64:tests/spread/smoketests/reactive:two (dec120311-758749)... 2025-12-12 03:40:51 Executing google:ubuntu-22.04-64:tests/spread/smoketests/reactive:two (dec120311-758749) (8/53)... 2025-12-12 03:41:14 Restoring google:ubuntu-22.04-64:tests/spread/commands/init-extensions:expressjs (dec120311-758638)... 2025-12-12 03:41:14 Restoring google:ubuntu-22.04-64:tests/spread/commands/init-extensions:fastapi (dec120311-758711)... 2025-12-12 03:41:30 Preparing google:ubuntu-22.04-64:tests/spread/dependencies/ (dec120311-758711)... 2025-12-12 03:41:30 Preparing google:ubuntu-22.04-64:tests/spread/dependencies/ (dec120311-758638)... 2025-12-12 03:41:34 Restoring google:ubuntu-22.04-64:tests/spread/commands/init-extensions:go (dec120311-758738)... 2025-12-12 03:41:52 Preparing google:ubuntu-22.04-64:tests/spread/smoketests/multi-base:all (dec120311-758738)... 2025-12-12 03:41:55 Executing google:ubuntu-22.04-64:tests/spread/smoketests/multi-base:all (dec120311-758738) (9/53)... 2025-12-12 03:42:40 Restoring google:ubuntu-22.04-64:tests/spread/commands/init-extensions:django (dec120311-758757)... ``` </details> <!-- Describe your changes --> --- - [x] I've followed the [contribution guidelines](https://github.com/canonical/charmcraft/blob/main/CONTRIBUTING.md). - [x] I've signed the [CLA](http://www.ubuntu.com/legal/contributors/). - [x] I've successfully run `make lint && make test`. - [x] ~I've added or updated any relevant documentation.~ (N/A) - [x] ~I've updated the relevant release notes.~ (N/A)

This PR deprecates accessing
testing.Context.charm_spec, because the_CharmSpectype is private, and was never intended to be exposed publicly.I suggest we open PRs for the two known users of this attribute before the next release, or even before merging this PR, per @tonyandrewmeyer's research and suggested fixes:
Resolves #2131
Requesting review from @benhoyt, since his instinct was to avoid deprecation noise and publicly document this instead; and from @tonyandrewmeyer since he's the most familiar with this and what charms should be using instead.