close
Skip to content

feat: allow testing.State.get_relation to accept relation objects#2359

Merged
james-garner-canonical merged 5 commits intocanonical:mainfrom
james-garner-canonical:26-03+feat+accept-relation-in-testing-state-get-relation
Mar 13, 2026
Merged

feat: allow testing.State.get_relation to accept relation objects#2359
james-garner-canonical merged 5 commits intocanonical:mainfrom
james-garner-canonical:26-03+feat+accept-relation-in-testing-state-get-relation

Conversation

@james-garner-canonical
Copy link
Copy Markdown
Contributor

@james-garner-canonical james-garner-canonical commented Mar 3, 2026

This PR updates testing.State.get_relation to accept a relation object. The means that users can write state_out.get_relation(rel_in) instead of state_out.get_relation(rel_in.id), and if they do so, the returned type is guaranteed to match the relation type passed in (peer, subordinate, or regular) -- both statically and at runtime.

Resolves: #2243

Alternative to:


For context on why testing.State.get_relation only accepts int currently, see discussion on the 2024 PR where this was designed: canonical/ops-scenario#134

@james-garner-canonical james-garner-canonical force-pushed the 26-03+feat+accept-relation-in-testing-state-get-relation branch 2 times, most recently from 21d791c to 74c1e66 Compare March 3, 2026 04:00
@james-garner-canonical james-garner-canonical force-pushed the 26-03+feat+accept-relation-in-testing-state-get-relation branch from 74c1e66 to 4e0af23 Compare March 3, 2026 04:46
@james-garner-canonical james-garner-canonical requested review from dimaqq and tonyandrewmeyer and removed request for dimaqq March 5, 2026 01:49
@james-garner-canonical james-garner-canonical marked this pull request as ready for review March 5, 2026 01:49
Copy link
Copy Markdown
Collaborator

@tonyandrewmeyer tonyandrewmeyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm +1 on this, and now -1 on both the alternatives. Thanks for taking the time to work through the options.

I'm not sure that checking the (often empty) interface and endpoint adds value, but I'm not opposed to including that.

Please do update the PR description (or commit body) for merging.

Comment thread testing/src/scenario/state.py Outdated
Comment thread testing/src/scenario/state.py Outdated
Comment thread testing/src/scenario/state.py Outdated
Comment on lines +1764 to +1766
if isinstance(relation, RelationBase) and (
(type(state_relation), state_relation.endpoint)
!= (type(relation), relation.endpoint)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it the case that we don't care about the interface, because realistically we infer it from charm metadata, so it can't really change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think validating interfaces here might come back to haunt us in the future, as there are behind the scenes shenanigans involved. See other comment (relevant bit quoted below).

PS: I'm dropping validating the interface from this PR, because I'm worried that it may lead to unexpected issues in future. The documentation threatens that it will be auto-populated from the charm's metadata if not specified, but I think this only happens with State.from_context (where you don't specify anything manually). But if we ever did start auto-populating this during Context.run or similar it would be easy to get an unexpected mismatch here (manually create Relation without interface name, then query post-run State and get mismatch).

Comment thread testing/src/scenario/state.py Outdated
@james-garner-canonical james-garner-canonical merged commit 706b667 into canonical:main Mar 13, 2026
60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve typing UX of ops.testing.State.get_relation

3 participants