fix!: drop model.Container from ops.testing for mypy#2339
Draft
james-garner-canonical wants to merge 3 commits intocanonical:mainfrom
Draft
fix!: drop model.Container from ops.testing for mypy#2339james-garner-canonical wants to merge 3 commits intocanonical:mainfrom
james-garner-canonical wants to merge 3 commits intocanonical:mainfrom
Conversation
This should fix mypy thinking that ops.testing.Container is ops.model.Container rather than scenario.Container.
Collaborator
|
Excellent, thanks for this. Let's discuss in daily when Tony and Dima are both back (I've added it to the top of the doc). |
Contributor
|
my 2c: let's bite the bullet: merge, call it out in release notes, but don't bump the major version. |
dimaqq
reviewed
Feb 24, 2026
| _compatibility_names = [ | ||
| 'CharmBase', | ||
| 'CharmMeta', | ||
| 'Container', # If Scenario has been installed, then this will be scenario.Container. |
Contributor
There was a problem hiding this comment.
Do we have containers in Harness, at all?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR drops the
model.Containerre-export from theops.testingmodule, becauseops.testing.Containeris incorrectly inferred to bemodel.Containerbymypy, which impacts charmers who type check their state-transition tests withmypy.The issue
The
ops.testingmodule currently re-exports a number of names from elsewhere inopsfor backwards compatibility, as these names were accidentally exposed viaops.testingwhen Harness was implemented there. TheContainername is one of these, but is also exported fromscenarioif it is installed. While Pyright (the type checker used byops) correctly infers the type ofops.testing.Containerasscenario.Containerin this case, Mypy does not -- instead it infers the type asops.model.Container, even in users state-transition tests, which causes type checking errors.This was reported on matrix. It looks like this has always been the case with Mypy, and I guess it just flew under the radar because many charms don't type check their tests.
The fix
We'd like to encourage type checking tests, so this PR fixes this problem by dropping the
ops.model.Containerre-export fromops.testing.This is technically a breaking change, but (1) charms shouldn't be using
ops.testingat runtime, so it would be a testing time only breakage; and (2) only legacy Harness tests' use ofops.testing.Containerwould be broken -- and such tests should really have been usingops.Containerinstead. We could preserve the runtime behaviour of re-exportingops.model.Containerifscenarioisn't installed, but I suspect it's not worth doing so, and it would be nice to instead move towards dropping these re-exported names entirely in future.Testing the fix
To verify that this fix works as intended, I've updated the
httpbin-demoexample charm to addmypyto its existing linting. I've verified that this linting fails with the reported error without the update toops/testing.pyfrom this PR. To keep this check current, I've updatedtool.uv.sourcesfor the charm to pullopsfrom the local files.Potential CI changes
We could add
mypyto the linting for our other example charms, the K8s and machine tutorial charms. However, this wouldn't reflect what we actually want charmers to do when following the tutorial, so we shouldn't do so on disc. Instead, it would probably be cleaner to add a custom CI job like below. If we did so, we could use the same approach for thehttpbin-democharm.