Skip to content

Preserve annotations and inferred types when generating stubs #3475

Open
@JukkaL

Description

@JukkaL

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions