Skip to content

Improve typing UX of ops.testing.State.get_relation #2243

@james-garner-canonical

Description

@james-garner-canonical

ops.testing.State.get_relation(relation: int) is typed as returning an ops.testing.RelationBase, because it could return any of Relation, PeerRelation, and SubordinateRelation.

When type checking tests, code like this is often needed:

retrieved_relation = state.get_relation(relation.id)
assert isinstance(retrieved_relation, Relation)

Which isn't terrible, but as @dimaqq noted, could be better:

Aside: I wish this was more ergonomic for our users.

Like if they could conceptually write:

rel = state.get_relation<Relation>(rel.id)
# or
rel = state.get_relation(rel.id, kind=Relation)  # runtime error on mismatch

Perhaps more Pythonically:

rel = state.get_regular_relation(rid)
per = state.get_peer_relation(pid)
sub = state.get_subordinate_relation(sid)

Metadata

Metadata

Assignees

No one assigned

    Labels

    26.04An item we hope to do in the 26.04 cyclerainy daySmall items done in ~10% of each week's timetestsRelated to tests or testing

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions