-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Add --warn-needless-override
option to stubtest
#13321
Comments
CC @srittau and @AlexWaygood |
I've been wondering about something like this, and I love the idea — but I'm not sure it belongs in stubtest. Stubtest is a test that verifies the correctness of a stub. But this feels more like a stylistic thing that a linter should warn about. What if we created a separate script,
Cc. @hauntsaninja as well, as the stubtest expert :) |
The same is true about not just methods, but attributes as well: class A:
a: int
class B(A):
a: int # should be flagged |
I wrote a simple prototype (I was not able to go through all results, but top ~10 looks correct):
|
While I agree that this doesn't seem like a good fit for stubtest, I am not a fan of yet another linter. Would a check like this be hard to integrate into flake8-pyi? |
I don't think it is possible. We need I guess we can do some cheap-by-name-checks in |
flake8-pyi could potentially do something like this on a per-module basis, but it would mean substantially duplicating a lot of the work mypy already does in constructing symbol tables and inheritance trees. That feels silly to me -- flake8-pyi isn't a type checker, and we shouldn't pretend to be one. Also, flake8-pyi only ever looks at code one file at a time, so if a class inherits from a class in another module, flake8-pyi loses all information about the base class. One way around this would be if flake8-pyi added mypy as a runtime dependency. Then we could harness mypy to build the stubs, and then do some linting in flake8-pyi using information based on mypy's build. But that might involve using some mypy internal implementation details, which (as we know all too well at flake8-pyi!) would be fragile and prone to breakage. |
I've done this a couple of times. See https://github.com/wemake-services/typed-linter but, this is pretty hard to implement. You need to somehow match python's AST and mypy nodes (see https://github.com/wemake-services/typed-linter/blob/master/typed_linter/contrib/mypy/traversal/mypy_ast.py). You need to get expression's type information from mypy (it is statefull and very buggy for outsiders, see https://github.com/wemake-services/typed-linter/blob/master/typed_linter/contrib/mypy/type_reveal.py). And it is just not worth it 😞 |
Anyways, I will use the information I collected from |
I would prefer to implement this kind of check in stubtest over adding yet another tool. It's slightly outside stubtest's original use case, but "finding problems in stubs that need type checker support to detect" seems like a reasonable expansion. |
I'm happy to implement things like this as opt-in checks in stubtest. One other option to throw out there, I think this kind of thing might be a better fit for a stub only lint in mypy proper. E.g. it's much more ergonomic to type-ignore with error code such a lint than allowlist it. I think I'd also like to see the stubtest / mypy diff before we merge 30 typeshed PRs. There are ways in which I can imagine this going wrong, e.g. we should probably accept unannotated redefinitions, since a human would want to go and check whether the implementation actually takes the same set of types as the unannotated parent. There might also be cases where the presence of properties or methods on the class itself may matter to type checkers. |
Fair enough; I'm persuaded :)
+1 to both of these thoughts from @hauntsaninja as well. |
Ok then! I will send my prototype for everyone to review 🙂 |
I think we need to be especially careful with dunder methods when doing this kind of thing. Type checkers special-case a lot of dunder methods so that their very existence on a subclass causes them to treat the subclass specially. I've previously caused regressions related to this special-casing: Since python/typeshed#8483 was merged, we've already had to revert a lot of And I think there may also be an issue with some of the For dunder methods, can we take removals from typeshed one dunder method at a time, the same way we did with |
In fact, for some special methods such as |
Yes, I agree - special-casing is quite hard. I think that special PRs for that is the way to go.
|
Feature
Imagine this file with implementation:
Auto-stub creators will create a stub like:
Which technically is right, but not quite.
Since
do_some
has the same signature in bothA
andB
(only runtime implementation is different), we don't actually need it to be duplicated. We should ideally want just:Because we always want minimal correct stubs.
But, right now we only do this by hand.
Pitch
I propose adding
--warn-needless-override
options (with whatever name) that can warn us about needless overrides of parent methods in child classes.I will send a PR with the initial imlementation.
The text was updated successfully, but these errors were encountered: