invoke: Using python3 keyword-only arguments or annotated arguments causes a ValueError from inspect
Defining a task that uses the new Python3 keyword-only or annotation argument syntax and invoking causes a ValueError: Function has keyword-only arguments or annotations, use getfullargspec() API which can support them and halts.
Per the docstring for Python3 inspect, getfullargspec() could be used, but that function is itself deprecated in favor of inspect.signature() Unfortunately, neither of those functions exists in Python 2.6, so some level of cascading will be necessary.
For example, neither of these can be invoked:
from invoke import task
@task
def func(ctx, *args, keyonlyarg=42):
print(keyonlyarg)
@task
def func2(ctx, x:int=3):
print(x)
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 67
- Comments: 39 (3 by maintainers)
Commits related to this issue
- Allow annotations to be used for Python 3 Tasks Use inspect.signature instead of inspect.getargspect when using Python 3 to allow function annotations to be used in tasks. Also adds TaskContextError... — committed to rsxm/invoke by rsxm 8 years ago
- Allow annotations to be used for Python 3 Tasks Use inspect.signature instead of inspect.getargspect when using Python 3 to allow function annotations to be used in tasks. Also fix missing context i... — committed to rsxm/invoke by rsxm 8 years ago
- Allow annotations to be used for Python 3 Tasks Use inspect.signature instead of inspect.getargspect when using Python 3 to allow function annotations to be used in tasks. Also fix missing context i... — committed to rsxm/invoke by rsxm 8 years ago
- Allow annotations to be used for Python 3 Tasks Use inspect.signature instead of inspect.getargspect when using Python 3 to allow function annotations to be used in tasks. Also fix missing context i... — committed to rsxm/invoke by rsxm 8 years ago
- fix #357 - ValueError for annotated tasks As mentioned in #357 - using `getfullargspec` as recommended in the error itself. — committed to toejough/invoke by toejough 5 years ago
- Fix issue when using annotation in arguments. https://github.com/pyinvoke/invoke/issues/357 — committed to dperetti/invoke by dperetti 4 years ago
- Add invoke task to update pip package versions First invoke task: uses pip-compile to update the versions of all the pip packages in requirements.txt and requirements-dev.txt. It also post-processes ... — committed to spectria/tildes by Deimos 4 years ago
Since this issue is ignored i rolled out little monkeypatch. What it does is
getfullargspecwhich won’t raise exceptiongetargspecreturnsinspect.getargspecwith patched argspec for duration ofTask.getargspeccallTask.getargspecIt needs only stdlib + invoke to work
at the top of your
tasks.pydo2.0.0 will be out soon & is now using
inspect.signature!I’ll revisit this after dropping Python 2, which is one of the next things on the roadmap. Should make it much easier to apply/fix overall.
If I declare the variable ‘c’ as Context, I wish set the type. But, this problem prevent me to type-hinting… I can’t understand why this is not fixed.
Any updates on this?
Heya, it looks like this still isn’t fixed? Any idea what the blockers are? It’s been years
For those playing along at home I am now working on issue and PR triage to help catch up on the backlog that has accumulated.
https://bitprophet.org/projects/#roadmap The big tasks that are being worked on are:
Also
invokeis part of thefabricandparamikoecosystem and they need equal amounts of love too.But I am doing my best over the coming weeks and months to help close the items that will have the greatest impact for our limited cognitive bandwidth and volunteer time.
Hi! I’m still seeing this with version
1.1.1.I am not sure what might need to be done to diagnose or resolve it from my end. Is anyone else still running into this?
@bitprophet Hello? How’s it going?
@karan-webkul Python 2 is at EOL, and while everyone using this package already is avoiding using type hints because it does not support it, this issue is around the fact that we’d like to both use the package and type hints, like other modern Python 3 apps.
Hi, still facing this issue, is there any update?
I do something similar to what @dralshehri showed, but for the function.
passing now.
Python 2 doesn’t support type hinting, So removing all the type hinting code will fix the issue.
Any action on this? Seems like using the inspect is now the way to go. Plz advise if this is something that can get resolved.
@nathanielford Well, the bug is more than 2 years old at the moment. Developers still didn’t apply a patch that is proposed by the exception message itself. So, they don’t care. You’re on your own.
@bitprophet - I would also like to contribute. I have previously worked on
vcrpyas a maintainer for a year and my new role is seeing me needinvokea lot and I would like to put the time and energy into breathing new life into keepinginvokerelevant. I couldn’t see aCONTRIBUTING.mdto get a gist of who to contact about becoming a maintainer.In particular I am coming from a few years over in Typescript where projects like
lernaandnpm workspacescoupled with the factnpmhasscriptsbuilt into theirpackage.jsonI feel this is lacking in the Python ecosystem.poetryis about to launchv1.2.+which allows plugins and I think combiningpoetryandinvokeis a really awesome combination especially for multi-project mono-repos.Especially as a consultant I can provide packages of prebaked tasks similar to the
invocationsproject for our clients to get their dev workflows started.If there is a gitter channel hit me up or DM me on LinkedIn or twitter.
We used invoke a lot, but now moving to https://typer.tiangolo.com/. I can really recommend it, it is kind of powerful and simple to use, comes with a lot of example documentation and elevates type hints!
Looks like this project is not maintained anymore, or does not care about Python 3 features 😕
@nathanielford Just run into this too. As it says in the exception itself, replaced
inspect.getargspecat invoke/tasks.py withinspect.getfullargspecmanually. Works for me.@bitprophet - have you folks started working on this support? I’d be interested in contributing this support if not!
As a workaround for code completion to work in PyCharm,
I have to add type comments:
or sometimes I specify the type in docstrings:
Thanks, @0az! Worked for me. Only 1 change to make MyPy happy:
@italomaia and I encountered this using Fabric 2 with Python 3 (3.7.3 and 3.7.4 on Linux and macOS). Type hints in a Fabric task triggers
ValueErrorcomplaining aboutgetargspec: https://github.com/fabric/fabric/issues/1997Several seemingly-easy solutions presented themselves over the years (since 2016?!) in PRs mentioning this issue. To recap:
inspect.getfullargspecfor Python 3: https://github.com/pyinvoke/invoke/pull/373invoke’s vendored module calleddecorator, which already did the heavy lifting: https://github.com/pyinvoke/invoke/pull/373#issuecomment-437580030decoratorhas given this more thought, which was the original holdup for https://github.com/pyinvoke/invoke/pull/373, just copy that block intoinvoke/task.py.another workaround here.
monkey patch #373
save above code to
hotfix.pyand import it when you need.@eruvanos We experimented with Typer as well, but missed the ability to run simple commands like
invoke docs. With Typer, the equivalent would presumably look something likepython -m tasks docs, which is much more verbose. Did you find a solution for that?