Skip to content

Instantly share code, notes, and snippets.

@scharf
Created March 2, 2022 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scharf/25380f2c3e56f70624facf28809d7472 to your computer and use it in GitHub Desktop.
Save scharf/25380f2c3e56f70624facf28809d7472 to your computer and use it in GitHub Desktop.
Allow type annotation in the python invoke project
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()
@scharf
Copy link
Author

scharf commented Mar 2, 2022

Based on this comment pyinvoke/invoke#357 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment