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

fix(typing): update to latest version of Pyright and fix errors #1105

Merged
merged 4 commits into from
Jan 10, 2024

Conversation

benhoyt
Copy link
Collaborator

@benhoyt benhoyt commented Jan 9, 2024

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.

Suggestions to improve any of these welcome.

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.
-
Copy link
Contributor

@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.

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
Copy link
Contributor

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?

Copy link
Collaborator Author

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...

test/test_pebble.py Show resolved Hide resolved
test/test_model.py Outdated Show resolved Hide resolved
test/test_charm.py Outdated Show resolved Hide resolved
@@ -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]
Copy link
Contributor

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.

Copy link
Collaborator Author

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?

Copy link
Contributor

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.

Copy link
Contributor

@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.

Interesting thought about strict! Maybe something to look at next time we need to do this 😄.

For now, looks great to me!

@benhoyt benhoyt merged commit 261c441 into canonical:main Jan 10, 2024
18 checks passed
@benhoyt benhoyt deleted the update-pyright2 branch January 10, 2024 23:36
@benhoyt
Copy link
Collaborator Author

benhoyt commented Jan 10, 2024

Updates for new version of Pyright, no user-facing changes, merged without further review.

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.

2 participants