-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Allow overriding own code with stubs #2502
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
Comments
The Zulip project does this, see https://github.com/zulip/zulip/blob/master/zerver/lib/request.pyi which is a custom stub file for https://github.com/zulip/zulip/blob/master/zerver/lib/request.py. It works because Zulip has a script to run mypy (https://github.com/zulip/zulip/blob/master/tools/run-mypy) that builds a list of files to pass to mypy and filters out It might be sensible to make this mypy's default behavior: prefer |
Hm, I think if you have a script to generate the list of files, the script
can easily do this. I don't like the idea of silently checking a different
file than the one you pass on the command line.
|
I was more thinking about the case of passing a package(?) name to mypy, like |
But for modules inside a package it already prefers pyi over py.
|
Oh hmm. In that case I don't understand what situation the OP had where mypy wasn't working as expected. |
My guess he's imagining that the stub and the .py file would be combined somehow. @rtpg can you clarify what you are trying to do? |
So my understanding of how mypy currently looks up types is:
I would like to be able to exclude on a class/type basis, rather than a file basis, because I have files with class definitions and functions that I would like to be checked by mypy. The issue with the By being able to say "this object should not be looked at by mypy for the type definition", I can stub in parts of a file that are trickier without needing to do refactors of my code itself. An alternative solution for my case would be to re-export:
I'll see if this works for my use case. |
@rtpg What you want is already possible on module level. The trick is to invoke mypy to check modules and not files and set MYPYPATH such that is has multiple directories. Your stub directory should come before your source directory. Then you're working on module paths and imports, so you can write a But it won't be possible on class level, this would be rather confusing for the reader of the code. The file boundary is pretty obvious to explain and easily visible with verbose logging. A class/type boundary, I don't see how that could be expressed. |
For "stubbing" a single class, I suppose you could use |
I hope that the ideas discussed above are good enough. Closing due to lack of activity. |
mypy currently has a bit of trouble with some of the more complicated types out there, especially when it comes to metaprogramming.
It would be pretty useful if I can provide a stub that would override a class defined in my own code, in cases where mypy fails to generate the proper type for a class.
For example, have a file like:
with the stub file being:
(Here f would type check because the type lookup for C would happen through the stub, not the implementation)
This will let some people properly type complex objects generated by tools like ORMs.
I tried simply providing a stub for C along with the implementation, but currently it seems like the implementation's type inference works here.
The text was updated successfully, but these errors were encountered: