Skip to content

Type check untyped code using correspoding stub #3505

Closed
@movermeyer

Description

@movermeyer

I have a file I don't want to add type annotations to (not my code, core.py) and a stub for that file (core.pyi).
I would like mypy to check for type issues in the file, pulling in the types from the stub as needed.

Example workflow:

Untyped code (core.py):

def foo():
    return '123'

Corresponding stub file (core.pyi):

def foo() -> int: ...

Note that if the type annotations were in the code, it would catch the type issue;

$>mypy core_with_type_annotations.py
mypy_test.py:2: error: Incompatible return value type (got "str", expected "int")

It would be useful if mypy core.py would pull in type annotations as necessary from the corresponding stub file (possibly by supplying a flag on the command-line):

$>mypy --use-self-stubs core.py
mypy_test.py:2: error: Incompatible return value type (got "str", expected "int")

Since this feature does not exist, I currently have to modify the source directly and add the type annotations, and then back out those changes if I need to make a commit against the source (being careful not to commit the annotations).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions