-
I am writing some library code which takes heavy advantage of type checking to assert correctness before runtime (a la typestate programming). Since appropriate behavior of my code includes catching certain mistakes at type-check-time, I'd like to integration-test the typing of my module. Specifically, I'd like to be able to write a set of Python tests which, when run, will pass a sample through Pyright and snapshot-test the checker output. My tests should fail if the code sample passes type-check, or if the output from Pyright changes relative to past runs. What I'm looking for is similar in spirit to I'm confident there are tests of this sort in Pyright itself. I've glanced through the samples but haven't taken a close look at the infrastructure. I suspect that this existing infrastructure wouldn't be of great use in an otherwise Python project (I want pytest to be the singular test harness entry point) but I would love to hear any learnings from that. Is anyone aware of an existing project or prior art in this area? If not, are there any particular suggestions on how one should go about doing this? It would be pretty straightforward to write a utility library to invoke the CLI on test files, but since Pyright is a Node.js library rather than native Python it'll be slightly ugly; I figure others might have thoughts on the least-bad way to do it. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
python/mypy#8655 asks for a similar feature and has some discussion of what's already possible with mypy. Another place to look would be the test suites for existing mypy plugins, such as pydantic at https://github.com/samuelcolvin/pydantic/blob/5261fd05a0374b84ce2602d45990baf480fa2417/tests/mypy/test_mypy.py I don't have anything to share regarding pyright. I suspect it's a bit more difficult to extend its internal test suite in a Python project because pyright is written in TypeScript, not Python. |
Beta Was this translation helpful? Give feedback.
-
I think you're going to hit this no matter what; pyright runs in node so you'll need node to run it. If you're looking for something in CI, I maintain https://github.com/jakebailey/pyright-action for GitHub Actions, which runs pyright over whatever you want and then reports the errors. But, if you're looking to have a list of known errors and check diff, I think you'd need to be using |
Beta Was this translation helpful? Give feedback.
-
Since it didn't seem like there was much prior art in this area, I've implemented some simple infrastructure for this myself. I developed it initially in a private repo but I've re-posted it here for others' reference: https://github.com/WasabiFan/pyright-ui-test-poc I'd love to hear thoughts/feedback on it and whether others think it would be useful. I have developed a spatial frame transformation library as part of an internal project which uses generics to denote the reference frame a measurement is in, and am now testing it with these utilities. So far, it's been going pretty well. Thanks to everyone for the other answers and discussion in this thread. |
Beta Was this translation helpful? Give feedback.
Since it didn't seem like there was much prior art in this area, I've implemented some simple infrastructure for this myself.
I developed it initially in a private repo but I've re-posted it here for others' reference: https://github.com/WasabiFan/pyright-ui-test-poc
I'd love to hear thoughts/feedback on it and whether others think it would be useful. I have developed a spatial frame transformation library as part of an internal project which uses generics to denote the reference frame a measurement is in, and am now testing it with these utilities. So far, it's been going pretty well.
Thanks to everyone for the other answers and discussion in this thread.