-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Tests specifically for tricky, white-lie signatures #1339
Comments
/cc @JukkaL @gvanrossum @vlasovskikh @matthiaskramm @JelleZijlstra @ambv from previous discussions |
I like this idea. This would interact nicely with python/typing#436: the same test runner proposed there could run tests in typeshed. |
I'm ±0 on this. Pro: Recently, we've had a number of pull requests with commit messages along the lines of Contra: It's a slippery slope. Testing leads to tests, which leads to more tests, which leads to complete test coverage, and look! We've just duplicated https://github.com/python/cpython/tree/master/Lib/test. If we go down this road, we would need very clear wording that unit tests for |
I agree with what Matthias said (and it's why previously I was against such tests). But there are only a few committers on the project and together we can set the bar high for when tests are accepted in PRs. It will help if there are clear words in README.md and CONTRIBUTING.md about this (reviewers can then refer contributors to those words). |
I like the general idea of having tests for stubs. So having them at least for tricky signatures looks like a reasonable compromise to me. |
An easy and hopefully unobjectionable way to do this is to create a repo with whatever code you want to test and add the repo to mypy_primer. |
I think that now that we have the new directory structure, we can reopen this discussion. One way to do this would be to suport an optional Here's an example of how this could work. To add tests for from foobar import complex_function
complex_function(1, arg="x") # On this line there must not be an error
complex_function(1, arg=["x"]) # ERROR # On this line there must be an error We'd run the tests in Python 2 and/or Python 3 modes, depending on what's supported by the stubs. It should be possible to use Python version checks to have different tests for Python 2 and Python 3. In addition to optionally having tests for tricky signatures, I believe that there are a few other valid use cases for tests:
As discussed above, we don't want to have exhaustive tests that test every single signature in stubs. Also PRs that add dozens of annotations probably shouldn't add tests for all of the new signatures (but it may be fine to test a few of the signatures). We'd need to make the conventions explicit and enforce them in code reviews. I believe that after we've had the conventions established for some time, contributors will figure this out by looking at existing tests as examples. It will be important that we don't let poor-quality or too wide tests through code review, as it's possible (even likely) that contributors will start emulating these examples. |
Now that we have a package version in METADATA.toml, we could also run stubtest. This would totally take care of 1. |
Both @JukkaL's and @hauntsaninja's suggestions sound good to me. |
In December 2020, @hauntsaninja wrote:
I would very much like to do something like this to add some regression tests for Some questions, therefore:
If the answer to all of these is "yes", I would be happy to submit a PR, leading to...
|
Thank you! I agree on "yes" for these questions. I don't feel strongly about the structure or naming, but here's a proposal:
|
One complication is that a regression tests for
Option (2) probably won't work until #6307 is fixed, however. |
Another problem is how to ensure the type matches. This doesn't really work:
If
Maybe we should use mypy's test suite instead? It makes it possible to assert the output of |
I would say it "partially works", since we are at least verifying that the inferred type is not |
It mostly works like this:
And the framework complains if the revealed type is something different. I don't think it's easy to use that syntax outside of mypy itself, though there are apparently a few tools like pytest plugins doing that. You can work around the float/int thing by taking advantage of list invariance. This errors for mypy:
|
I think we shouldn't let the perfect be the enemy of the good. If nobody objects, I'll go ahead and draft a PR with some mypy_primer-based regression tests. If somebody else drafts a PR proposing superior tests based on mypy's testing framework, I'll happily abandon my PR (/revert my PR if it's already merged) 🙂
Very nice! |
I cannot refrain from a shameless plug: https://github.com/typeddjango/pytest-mypy-plugins |
This looks great! If others also agree that this would be a better way to go, I'll hold off making a PR and let @sobolevn propose a PR using this framework. |
I was not really paying attentions to this thread 😆 |
We now have such tests: #7663. |
We've had some useful, long discussions about writing tests for stubs (#754, #917). Most of the discussion has been around having tests for the bulk of the functionality in stubs and the libraries they describe. I think we're all broadly in agreement that
This issue is for a narrower proposal: sometimes when a library's real signature is something complex or hard to express in the type system, our signature ends up pretty tricky or we end up telling sort of a white lie to the type-checkers. For these, even when looking right at the signature in one window and the library's docs (or an experiment in the REPL) in the other, it can be hard to tell whether they match. In these cases, I think a hand-written test would be useful -- probably looking a lot like a mypy unit test, with some code to be type-checked and an indication that certain lines should have errors and the rest shouldn't, and then we could e.g. run mypy and pytype against it.
@JukkaL suggested just this in one bullet point of a comment on #754, and gave some examples:
A new example today is #1331:
auto
doesn't really inherit fromIntFlag
, and we may find a new way of refining how we describe that interaction in the stubs. When we do it'll be useful to have a test for the functionality that that little lie in the stubs is intended to provide.The text was updated successfully, but these errors were encountered: