Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: narrow the type for 'app' and 'unit' in relation events #1032

Merged

Conversation

tonyandrewmeyer
Copy link
Contributor

@tonyandrewmeyer tonyandrewmeyer commented Oct 5, 2023

  • app is always provided for relation events, except for relation-broken (which may be a Juju bug), so for all of the relation event classes, make the type Application rather than Application|None.
  • unit may be provided for relation-changed, and is provided for unit level relation events (joined, departed), but is not provided for application level events (created, broken), so adjust the types in the relation event classes to be either None or Unit as applicable.
  • Adjust the tests so that we have a relation-change with and without the unit provided, and so that the app is provided for all cases except relation-broken, which has one test with and one without.

The RelationCreated and RelationBroken classes do now have an odd unit attribute that is defined as always being None, but that was the actual case before and we're just being explicit about it now. I assume dropping the attribute would be too much of a backwards compatibility issue.

Similarly, the __init__ for the various event classes still accept None for app and unit even when they are actually always expected. This is unfortunate, but is stuck for backwards compatibility. Generally, we are the one creating the event objects, not the charmer, so the inexact types in the __init__ shouldn't impact charmers.

Fixes #976

@tonyandrewmeyer tonyandrewmeyer added the feature New feature or request label Oct 5, 2023
@tonyandrewmeyer
Copy link
Contributor Author

The grafana-k8s-operator tests assume that emit can be called with just the relation. I think this is an issue with the test, and that couldn't happen in production. However, it still indicates that there are backwards compatibility issues here. So I'll need to do a more minor change that leaves the __init__ in place I suppose.

@benhoyt
Copy link
Collaborator

benhoyt commented Oct 5, 2023

The grafana-k8s-operator tests assume that emit can be called with just the relation. I think this is an issue with the test, and that couldn't happen in production. However, it still indicates that there are backwards compatibility issues here. So I'll need to do a more minor change that leaves the init in place I suppose.

Yeah, I think that assessment is probably right: it's probably too much to change the signature here, even if that test shouldn't really be doing that. So let's leave __init__ alone and focus on the typing changes.

ops/charm.py Outdated Show resolved Hide resolved
* Assume that the application will always be provided for relation events, even though there is an open Juju bug that indicates that may not always be the case at the moment.
* Minor style fixes.
* Don't adjust the types in the , as charmers may be relying on the default value in tests, and there's no valid default value other than None.
@tonyandrewmeyer tonyandrewmeyer marked this pull request as ready for review October 5, 2023 23:18
ops/charm.py Outdated Show resolved Hide resolved
test/test_main.py Outdated Show resolved Hide resolved
@benhoyt benhoyt merged commit 6428ff6 into canonical:main Oct 11, 2023
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve typing for RelationEvent.unit
2 participants