-
Notifications
You must be signed in to change notification settings - Fork 119
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
fix(typing): update to latest version of Pyright and fix errors #1105
Conversation
So annoying how quickly this tooling changes. Couple of notes: - ops/charm.py: it seems like the new version disallows overriding the type in a subclass, so just use a "pyright: ignore" comment to work around this. Maybe there's a better way. - The new Pyright no longer allows a type variable to be defined inside a function. Not sure what the rationale for this was, but they have to be moved to the top level (away from where they're used!) now. - Some of the bytes/str/bytearray stuff is annoying. Maybe there are better solutions for these types. Oh for Go's io.Reader. -
Hopefully this this fixes CI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of small suggestions and a couple of small questions, but overall looks good to me.
@@ -38,4 +38,5 @@ reportMissingModuleSource = false | |||
reportPrivateUsage = false | |||
reportUnnecessaryIsInstance = false | |||
reportUnnecessaryComparison = false | |||
disableBytesTypePromotions = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docs (I had to look up what this did!) say the default is false. Why do we need to add it in here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's odd. When I comment out that line and leave it as the default, Pyright prints a bunch of this message:
$ tox -e static
static: commands[0]> pyright
/home/ben/w/operator/ops/pebble.py
/home/ben/w/operator/ops/pebble.py:1617:21 - error: Unnecessary "cast" call; type is already "bytes" (reportUnnecessaryCast)
/home/ben/w/operator/ops/pebble.py:2993:52 - error: Argument of type "bytearray" cannot be assigned to parameter "buf" of type "bytes" in function "_next_part_boundary"
"bytearray" is incompatible with "bytes"
...
Oh wait, it looks like the default is actually true in "strict" mode: https://github.com/microsoft/pyright/blob/main/docs/configuration.md#diagnostic-rule-defaults
Makes me wonder if we're causing ourselves a bunch of extra pain by being in strict mode...
@@ -466,7 +466,7 @@ class RelationCreatedEvent(RelationEvent): | |||
can occur before units for those applications have started. All existing | |||
relations should be established before start. | |||
""" | |||
unit: None | |||
unit: None # pyright: ignore[reportIncompatibleVariableOverride] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the cleanest fix here is to remove the type from RelationEvent
and add it to RelationChangedEvent
. The downside would be that I'm not sure if we can still then have the attribute have documentation in the base class, although it would be in all the subclasses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, I had a play with this, and I don't like the fact that then the docs for RelationEvent
wouldn't have "unit" defined at all, which seems very weird. Or were you suggesting something else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that was what I was suggesting, and I indeed wondered if that meant it was not possible to have it in the RelationEvent
doc (unless there's some Sphinx trick for this I don't know). I agree that's not great.
In balance, I'm fine keeping it how you have it with the ignores. The goal was to have the types correct in the various subclasses so people didn't need a bunch of assert unit is not None
and that all still works, so there doesn't seem to be much value in trying too hard to get rids of the ignores.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting thought about strict! Maybe something to look at next time we need to do this 😄.
For now, looks great to me!
Updates for new version of Pyright, no user-facing changes, merged without further review. |
So annoying how quickly this tooling changes. Couple of notes:
Suggestions to improve any of these welcome.