-
Notifications
You must be signed in to change notification settings - Fork 5
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
Partially override types of a module to supply only the needed incomplete types #274
Comments
You can currently provide partial stubs, and when using an member that isn't in the stub you get the error in the OP, are you suggesting that if the stub is annotated somehow as 'incomplete'/'partial' then you will instead receive a different error such as "using member from incomplete stub"?
You could just vendor the entire stub and fix that def. If you mean you want to do something like patching in/overriding specific types then that sounds like #17. And avoiding the |
Yes, or something like "incomplete stub does not contain definition for requests.model.PreparedRequest"
Looks like we'd have to include the whole of types-requests, and then handle updating it when the official version is updated. I'd prefer not to have to do that to make one small fix. |
But that doesn't usually make them correct, it just gets rid of the Better example is cookies.pyi, where almost everything is untyped, so can't even tell which functions return. |
I don't like the sound of this, if you are removing all of the stub except for the parts you are currently using then you lose IDE completions. #17 would enable patching specific fixes on top of existing types, which I think addresses the issue more robustly than this suggestion. |
I guess that could be worked around, eg. using a different file extension so any IDE that doesn't understand the partial stubs will ignore them. Presumably, IDE completion is able to be done without stubs. But put another way, my reason for raising this issue was to have a way of limiting the scope (eg. specify that the stub is only for one class / module / function, and doesn't imply the existence or otherwise of other classes / modules / functions). This could be taken further to separate the typing from what exists. |
IDE completion is normally based off a vendored typeshed (PyCharm vendors typeshed, and VSCode uses a vendored typeshed for pyright) then falling back to analyzing the modules. A new file extension would be a nightmare.
I think #17 covers this use case fairly comprehensively. |
PEP 484 specifies how stub files can be incomplete, using def __getattr__(name) -> Any: ... Maybe there could be another value that could be used instead of PEP 561 specifies a way that stub packages can be partial (not include stubs for all modules). This seems to already be supported by [based]mypy. |
some good points were raised on the typescript issue for the this feature: the biggest issue imo being the concern that it would make users will be less likely to fix incorrect/incomplete types upstream |
so basically, broken/incomplete stubs should be fixed upstream |
TLDR: Provide a way to have stubs that don't contain everything in the package.
Currently, when creating stubs, you have to create stubs for everything under the top level (including
__init__.py
files so it treats folders as modules). It would be useful to be able to mark the stub (or a class / function within it) as incomplete, and give better errors if later attempting to use something not included in the stub.eg. When using requests
example.py
requests.pyi
So that stub is enough so basedmypy can check example.py, but doesn't include:
requests.PreparedRequest
andrequests.api
)requests.Response
requests.get
If the code then tried replacing
requests.get
withrequests.Session().get
, basedmypy currently complains:Would be useful to be able to mark it somehow as partial, so it will provide a message to say that the stub does not contain
requests.Session
.The same behaviour currently occurs even when types-requests is installed (say for example we wanted to use those stubs, but fix one function or field in there that was incorrectly typed, or just contained
Any
s).Example:
requests.model.Response' contains this function, with lots of
Any`s:The text was updated successfully, but these errors were encountered: