[Python-ideas] Proposal: Use mypy syntax for function annotations

Chris Angelico rosuav at gmail.com
Fri Aug 15 00:44:54 CEST 2014


On Fri, Aug 15, 2014 at 6:05 AM, Dennis Brakhane
<brakhane at googlemail.com> wrote:
> Does Mypy provide a way to "fix/monkeypatch" incorrect type declarations
> in function signatures? For example, by modifying __annotations__?

AIUI there's a separation of execution and type checking here - you
*either* run under mypy to check types, *or* run under Python to
execute the code. If that's the case, it should be possible to create
a magic module that you import which overwrites a few functions with
stubs.

# spam.py (which you can't change)
def func(x: int) -> int:
    return x * 3 + 5

# type_fixes.py in the regular PYTHONPATH
"""This is a stub; the mypy equivalent fixes some type
annotations used in third-party libraries."""

# type_fixes.py in the mypy PYTHONPATH
import spam
def func(x: Number) -> Number: pass
spam.func = func

# in main_module.py
import type_fixes
import spam
x = func(42.5)


Monkey-patching entire functions shouldn't be a problem if they don't
need bodies.

ChrisA


More information about the Python-ideas mailing list