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

[docs] Document linting instructions #32

Open
4 tasks
sneakers-the-rat opened this issue Oct 4, 2024 · 6 comments
Open
4 tasks

[docs] Document linting instructions #32

sneakers-the-rat opened this issue Oct 4, 2024 · 6 comments
Labels
documentation Improvements or additions to documentation

Comments

@sneakers-the-rat
Copy link
Collaborator

Until we get 2.0 done and make the shape annotations uh valid type annotations, we need to document how to make it not blow up your linter.

The rule violation is F22: https://docs.astral.sh/ruff/rules/forward-annotation-syntax-error/
which is a pretty basic one - it's an error that's sort of rare and hard to make, but it's also a rule that is so basic that we shouldn't be violating it (hence 2.0).

Document how to make linters not get annoyed

  • ruff
  • pyright
  • mypy
  • what else?
@sneakers-the-rat sneakers-the-rat added the documentation Improvements or additions to documentation label Oct 4, 2024
@blakeNaccarato
Copy link

blakeNaccarato commented Oct 30, 2024

Careful it looks like you made a typo, calling it F22 in your comment here instead of F722 (in case that typo makes its way into the docs via copy/paste).

To help with documenting the pyright case, I've documented some examples copy/pasted from VSCode's Pylance warnings below (pyright reports the same errors but ofc doesn't mention "Pylance"), and a workaround. I've found that it fires a reportInvalidTypeArguments error on the first parameter to numpydantic.NDArray and numpydantic.Shape with the following form:

Expected no type arguments for class "{class}" Pylance(reportInvalidTypeArguments)

where {class} is Shape in the case of numpydantic.Shape. In the case of numpydantic.NDArray it fires overlapping errors for the following {class}es: H5ArrayPath, H5Proxy, Path, VideoCapture, VideoProxy, and ZarrArrayPath.

It also fires overlapping errors on the numpydantic.NDArray annotation itself for the {type}s Tuple[Path | str, str] and ndarray[Unknown, Unknown], reporting errors of the following form:

Type "{type}" is already specialized Pylance(reportInvalidTypeArguments)

The workaround, therefore, is to annotate any individual lines using numpydantic.Shape or numpydantic.NDArray as in the following example (Python < 3.12):

from typing import TypeAlias
from numpydantic import NDArray, Shape

VectorShape: TypeAlias = Shape["*"]  # noqa: F722  # pyright: ignore[reportInvalidTypeArguments]
Vector: TypeAlias = NDArray[VectorShape, int | float]  # pyright: ignore[reportInvalidTypeArguments]

which confines the messy error suppression to these VectorShape and Vector definitions, allowing their use in arbitrary Pydantic models without having to suppress errors there as well.

@sneakers-the-rat
Copy link
Collaborator Author

Super helpful, and needing to do that on every line is absolutely abysmal on my part.

Proper type annotations using Protocol are coming, i just need to find a day or two to make a small PR upstream to pydantic (i think) and then it should be no problem to switch over and then mark the old form for deprecation (i'll give a full major version's warning before removing the string-based syntax).

Thanks for, uh, seeing through the thicket of linter errors for the time being.

@blakeNaccarato
Copy link

blakeNaccarato commented Oct 31, 2024

Glad I could help! If you end up working it into a larger documentation PR to explain some of the other linter behaviors as well, of course feel free to copy/paste/modify any of the language I used above.

Thanks for, uh, seeing through the thicket of linter errors for the time being.

I guess it's a price that I'm willing to pay for using things like numpydantic! There's an alternate universe me who's using older, "stable"/boring tools to do the same work, and I wonder if they're getting things done faster than me, without all the gaslighting by type checkers trying to statically analyze the black magic that numpydantic and company enable.

I've done a bit of work in appeasing the type checker for funky Pydantic tools myself in (the presently poorly documented) https://github.com/softboiler/context_models and elsewhere, so if you need an extra set of eyes on PRs involving convoluted Protocol wrangling, just @ me and I may be able to take a look. I'm a heavy Pylance/pyright user with most/all the warnings turned on so I tend to catch all the ugly errors, for better or worse.

@sneakers-the-rat
Copy link
Collaborator Author

Lovely :):):) I would love a review once I make those changes. And yes a docs pr is due in the meantime

@marionoxt
Copy link

Hi! I have a project at work where I would love to use numpydantic, but currently the lack of integration with pylance/pyright and ruff block me from making that move.
I am thinking of postponing that work project until numpydantic is ready. This is why I want to ask:
Do you have an idea on how long it will take until pylance/ruff interop will work? You mentioned some 2.0 version.

I don't want to stress you out. Just eager to use numpydantic :) Thanks for your open source work!

@sneakers-the-rat
Copy link
Collaborator Author

working on one time-sensitive project that should be on its feet by the end of this week, and then i need to write a paper on this stuff shortly after, so shouldn't be too long now. I should be able to issue a quick patch that handles at least the need to declare annotations in multiple lines by using literals, and have already scoped out the path to implement it that should be compatible with pydantic and type checkers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants