-
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
Update Pyright version to latest (1.1.313) #944
Conversation
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
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 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.
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
Created https://github.com/canonical/alertmanager-k8s-operator-1/pull/1 to fix the Pyright issue with CI there. |
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.
LGTM, thanks for the iteration :)
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 atype: 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:
_ModelBackend
, which is privatePart of #920