Skip to content
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

Support multi-statement inference for custom types #13134

Open
vnmabus opened this issue Jul 15, 2022 · 1 comment
Open

Support multi-statement inference for custom types #13134

vnmabus opened this issue Jul 15, 2022 · 1 comment
Labels

Comments

@vnmabus
Copy link

vnmabus commented Jul 15, 2022

Feature

Multi-statement inference as proposed in #254 and #1055 is supported for empty standard collection classes, like dict or list, but not for custom classes. Thus, an example such as https://mypy-play.net/?mypy=latest&python=3.10&gist=7df00fc3d8612804369dbad6b5cb973b fails.

My proposal is to allow user-defined generics to infer the generic from their first usage, so that the example work.

Pitch

This problem arose in the context of making a typed library compatible with scikit-learn (which currently is not typed). In scikit-learn, the actual data of a machine learning problem is not passed to the constructor of the object, but instead passed to the fit method (for training). Thus, if one wants to make a scikit-learn compatible object which is generic over the data accepted, one currently needs to provide the annotation, as Mypy won't infer the generic type from the call to fit. This is cumbersome for users, as most of them should benefit from the typing without altering the code to add superfluous annotations. It also obscures the examples, as adding annotations would confuse users not used to them.

@JelleZijlstra
Copy link
Member

This would be useful, but may be difficult to implement. Mostly commenting so I can mention that mypy internally calls this feature "partial types".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants