Preserve annotations and inferred types when generating stubs

Open
#3,475 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
python
Domain
tooling

Research direction

Start by examining stubgen and the mypy AST flow for a target module or package. Determine how an equivalent mypy run could provide inferred types and how generated stubs would handle Python versions and platform-specific definitions. Done means producing annotated stubs that preserve explicit and inferred types, with the platform and version behavior resolved.

Written by the indexing model from the issue text.

Description

needs discussion topic-stubgen

If a user maintains a 3rd party library module that is type checked using mypy, there is no easy to way to automatically generate annotated typeshed stubs for that library, even though this could be pretty useful. We could perhaps extend stubgen to do this job.

Here's how this could work internally:

  • Do the equivalent of a mypy run against a target module/package.
  • Generate stubs based on the mypy AST, including inferred types.

The main difference from previous stubgen related proposals would be the inclusion of inferred types in the stubs. For example, consider this program:

x = 0
a = [x]

The generated stub could look like this:

from typing import List

x: int
a: List[int]

Here are a few things that complicate this task:

  • It may be hard to generate stubs that work with both Python 2 and 3. However, maybe it's okay to generate separate stubs for 2 and 3, since the stubs hopefully won't need a lot of manual review as they are automatically generated.
  • It may be hard to include platform-specific definitions. One approach would be to generate separate ASTs for all supported platforms and somehow merge them, generating conditional platform checks automatically.
Dominant language
Python
Stars
20.6k
Forks
3.3k
Avg merge
1d 18h
Merged PRs (30d)
54

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.

More from python/mypy

All issues in python/mypy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.