Created
March 2, 2022 23:00
-
-
Save scharf/25380f2c3e56f70624facf28809d7472 to your computer and use it in GitHub Desktop.
Allow type annotation in the python invoke project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from unittest.mock import patch | |
from inspect import getfullargspec, ArgSpec | |
import invoke | |
def fix_annotations(): | |
""" | |
Pyinvoke doesnt accept annotations by default, this fix that | |
Based on: https://github.com/pyinvoke/invoke/pull/606 | |
Copied from: https://github.com/pyinvoke/invoke/issues/357#issuecomment-583851322 | |
""" | |
if getattr(invoke.tasks.Task, '_ispatched', False): | |
# do not patch multiple times... | |
return | |
def patched_inspect_getargspec(func): | |
spec = getfullargspec(func) | |
return ArgSpec(*spec[0:4]) | |
org_task_argspec = invoke.tasks.Task.argspec | |
def patched_task_argspec(*args, **kwargs): | |
with patch(target="inspect.getargspec", new=patched_inspect_getargspec): | |
return org_task_argspec(*args, **kwargs) | |
invoke.tasks.Task.argspec = patched_task_argspec | |
invoke.tasks.Task._ispatched = True | |
fix_annotations() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on this comment pyinvoke/invoke#357 (comment)