python / python/mypy

Support multi-statement inference for custom types

Open
#13,134 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no repository files or tests. Reproduce the linked mypy-play example, then compare its custom-generic behavior with existing multi-statement inference for empty dict and list; done means the custom generic is inferred from its first usage and regression coverage verifies the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, scikit-learn
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.