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

Update Pyright version to latest (1.1.313) #944

Merged
merged 9 commits into from
Jun 13, 2023

Conversation

benhoyt
Copy link
Collaborator

@benhoyt benhoyt commented Jun 7, 2023

This PR updates Pyright to the latest version, 1.1.313, and fixes the errors that resulted.

Boy this was more painful than I expected. It seems to have gotten significantly stricter. I engaged in lots of fighting with the compiler -- if I knew any swear words I'd be using them here. In one case (_ModelBackend._run) I stopped fighting and added a type: ignore. I think we need to be pragmatic here: the code is no worse than it was before, and we can always improve specific things in future PRs.

I also ended up disabling a few more Pyright issues, specifically the following seemed more trouble than they're worth:

  • reportPrivateUsage: we do this often in the codebase, for example charm.py and testing.py poke at _ModelBackend, which is private
  • reportUnnecessaryIsInstance: we do lots of isinstance checks to detect type issues at runtime (we've done this since the beginning, and it's useful for people not using type checking)
  • reportUnnecessaryComparison: similar to the above, but for checking "if non_optional_value is None" and the like

Part of #920

Boy this was more painful than I expected. Lots of fighting with the
compiler. I ended up disabling a few more Pyright issues, specifically
these seemed more trouble than they're worth:

* reportPrivateUsage: we do this often in the codebase, for example
  charm.py pokes at _ModelBackend stuff
* reportUnnecessaryIsInstance: we do lots of isinstance checks to
  detect type issues at runtime (we've done this since the beginning,
  and it's useful for people not using type checking)
* reportUnnecessaryComparison: similar to the above, but for checking
  "if non_optional_value is None" and the like
@benhoyt benhoyt changed the title Update Pyright version to latest Update Pyright version to latest (1.1.313) Jun 8, 2023
@benhoyt benhoyt marked this pull request as ready for review June 8, 2023 05:26
Copy link
Contributor

@PietroPasotti PietroPasotti left a comment

Choose a reason for hiding this comment

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

I think globally disabling the private name usage checks is a good thing.
Surprised that a minor vbump made so many changes necessary, but all looks good to me.

@PietroPasotti
Copy link
Contributor

as for the failing o11y static checks, yeah, I know strings, bytes, buffers and the like can be a humongous pain. I've wrestled with those just as hard last year. Have no specific suggestions on how to address it here. Curious to see if the error disappears if we use the previous pyright version? Or is it a consequence of the changes you made?

Currently Pyright isn't (usually?) able to find the return type of
Container.exec, I think due to an import ordering thing? As such,
charms that use it get an Any type and static checks pass.

However, as soon as Pyright *can* find the return type, it's not really
correct, and we get errors like shown below.

We need to use Generic[AnyStr] and various overloads to ensure the
caller gets an ExecProcess[str] if they call exec() with "encoding" set
(the default), or ExecProcess[bytes] if they call exec() with
"encoding" set to None.

$ tox -e static-charm
static-charm: commands[0]> pyright /home/ben/w/grafana-k8s-operator/src
/home/ben/w/grafana-k8s-operator/src/charm.py
  /home/ben/w/grafana-k8s-operator/src/charm.py:929:28 - error: Argument of type "Literal['Version (\\d*\\.\\d*\\.\\d*)']" cannot be assigned to parameter "pattern" of type "bytes | Pattern[bytes]" in function "search"
    Type "Literal['Version (\\d*\\.\\d*\\.\\d*)']" cannot be assigned to type "bytes | Pattern[bytes]"
      "Literal['Version (\\d*\\.\\d*\\.\\d*)']" is incompatible with "bytes"
      "Literal['Version (\\d*\\.\\d*\\.\\d*)']" is incompatible with "Pattern[bytes]" (reportGeneralTypeIssues)
  /home/ben/w/grafana-k8s-operator/src/charm.py:929:56 - error: Argument of type "_StrOrBytes" cannot be assigned to parameter "string" of type "ReadableBuffer" in function "search"
    Type "_StrOrBytes" cannot be assigned to type "ReadableBuffer"
      Type "str" cannot be assigned to type "ReadableBuffer"
        "str" is incompatible with "ReadOnlyBuffer"
        "str" is incompatible with "bytearray"
        "str" is incompatible with "memoryview"
        "str" is incompatible with "array[Any]"
        "str" is incompatible with "mmap"
        "str" is incompatible with "_CData"
    ... (reportGeneralTypeIssues)
2 errors, 0 warnings, 0 informations
@benhoyt
Copy link
Collaborator Author

benhoyt commented Jun 12, 2023

Created https://github.com/canonical/alertmanager-k8s-operator-1/pull/1 to fix the Pyright issue with CI there.

Copy link
Member

@jnsgruk jnsgruk left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the iteration :)

@benhoyt benhoyt merged commit a4ba60b into canonical:main Jun 13, 2023
@benhoyt benhoyt deleted the update-pyright-version branch June 13, 2023 01:26
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.

3 participants