Skip to content

Minor fixes for pyright errors#13459

Merged
bluetech merged 12 commits intomainfrom
pyright-minor-fixes
Jun 2, 2025
Merged

Minor fixes for pyright errors#13459
bluetech merged 12 commits intomainfrom
pyright-minor-fixes

Conversation

@bluetech
Copy link
Member

@bluetech bluetech commented Jun 1, 2025

I ran pyright on src/ and fixed some of the errors which seemed good to fix (see commits for details). There are ~100 remaining errors, I may look at them in the future.

FTR the pyright config:

[tool.pyright]
include = [
    "src",
    # "testing",
    "scripts",
]
extraPaths = [
    "src",
]
pythonVersion = "3.9"
typeCheckingMode = "basic"
reportMissingImports = "none"
reportMissingModuleSource = "none"

@bluetech bluetech added the skip news used on prs to opt out of the changelog requirement label Jun 1, 2025
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

I don't have enough time for an actual review of the typing changes but I think you could add a manual pre-commit conf and add pyright's conf in the pyproject.toml fir future use (like for pylint and pyupgrade)

@bluetech
Copy link
Member Author

bluetech commented Jun 2, 2025

@Pierre-Sassoulas I forgot this was an option. Added!

bluetech added 12 commits June 2, 2025 12:02
These often appear in Protocols and ABCs and are never executed so not
expected to be covered.
Mostly to fix the TODO, doesn't have much semantic meaning.

Also fixes a related typo in import inside `TYPE_CHECKING` block.
It's not different from the `tmp_path_factory` already requested via
fixture.
pyright (somewhat annoyingly IMO) infers unannotated function types,
unlike mypy which treats them as `Any`. In these 2 cases it causes some
follow on type errors, so add explicit `Any`s to prevent pyright from
inferring some big union.
For some reason pyright infers the type of `essential_plugins` and
`default_plugins` as `tuple[Literal[...], ...]` even if they are
explicitly annotated as `tuple[str, ...]`. This then causes an error in
the `builtin_plugins.add` calls.

Replace with one-shot initialization appeases pyright and is nicer
regardless.
This is the expected case for casts -- the correct type is ensured
dynamically, just too complicated for a type checker to understand.
Pyright complains:

    Type annotation for "self" parameter of "__init__" method cannot
    contain class-scoped type variables (reportInvalidTypeVarUse)

and I figure it's not needed since it's already the type of `self`.
There is not much reason to use `TypeGuard` on the implementation
function of an `@overload`ed function, since only the types of the
overloads are used.

This fixes a pyright "Overloaded implementation is not consistent with
signature of overload" error.
For manual testing, as we are not yet passing.
@bluetech
Copy link
Member Author

bluetech commented Jun 2, 2025

I am going to add a coveragerc ignore for def foo(): ... lines which appear in Protocols. Alternative is to raise NotImplementedError() but ... is conventional and shorter.

@bluetech bluetech force-pushed the pyright-minor-fixes branch from 73f1a44 to 7a48181 Compare June 2, 2025 09:42
Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

Nice work!

Curious, why did you decide to test on pyright? Are you using it yourself at work/other projects?

@bluetech
Copy link
Member Author

bluetech commented Jun 2, 2025

My editor started showing squigglies from pyright and I couldn't help fix a few :)

@bluetech bluetech merged commit 5293016 into main Jun 2, 2025
30 of 31 checks passed
@bluetech bluetech deleted the pyright-minor-fixes branch June 2, 2025 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news used on prs to opt out of the changelog requirement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants