Skip to content

Issue with Python 3.11, or type annotations: the inspect module changed #833

Closed

Description

@HacKanCuBa

I'm having an issue running invoke (latest) in Python 3.11-dev (3.11.0a3). For this tasks file, this is the trace I get:

root@18bb2bd21aaa:/b2s# poetry run inv tests
Traceback (most recent call last):
  File "/root/.cache/pypoetry/virtualenvs/blake2signer-uv6dtlQx-py3.11/bin/inv", line 8, in <module>
    sys.exit(program.run())
             ^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/blake2signer-uv6dtlQx-py3.11/lib/python3.11/site-packages/invoke/program.py", line 373, in run
    self.parse_collection()
    ^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/blake2signer-uv6dtlQx-py3.11/lib/python3.11/site-packages/invoke/program.py", line 465, in parse_collection
    self.load_collection()
    ^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/blake2signer-uv6dtlQx-py3.11/lib/python3.11/site-packages/invoke/program.py", line 696, in load_collection
    module, parent = loader.load(coll_name)
                     ^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/blake2signer-uv6dtlQx-py3.11/lib/python3.11/site-packages/invoke/loader.py", line 76, in load
    module = imp.load_module(name, fd, path, desc)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/imp.py", line 235, in load_module
    return load_source(name, filename, file)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/imp.py", line 172, in load_source
    module = _load(spec)
             ^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 721, in _load
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 902, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "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/b2s/tasks.py", line 11, in <module>
    def flake8(ctx):
    ^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/blake2signer-uv6dtlQx-py3.11/lib/python3.11/site-packages/invoke/tasks.py", line 325, in task
    return klass(args[0], **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/blake2signer-uv6dtlQx-py3.11/lib/python3.11/site-packages/invoke/tasks.py", line 76, in __init__
    self.positional = self.fill_implicit_positionals(positional)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/blake2signer-uv6dtlQx-py3.11/lib/python3.11/site-packages/invoke/tasks.py", line 167, in fill_implicit_positionals
    args, spec_dict = self.argspec(self.body)
                      ^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/blake2signer-uv6dtlQx-py3.11/lib/python3.11/site-packages/invoke/tasks.py", line 153, in argspec
    spec = inspect.getargspec(func)
           ^^^^^^^^^^^^^^^^^^
AttributeError: module 'inspect' has no attribute 'getargspec'. Did you mean: 'getargs'?

It works perfectly fine for any other Python version I tried (3.7, 3.8, 3.9, 3.10, PyPy3.7, PyPy3.8, Stackless3.7, Stackless3.8).

The module inspect does indeed not have said attribute, and I see that there's a potential replacement done in invoke.vendor.decorator for some reason.

Activity

  1. self-assigned this
    on Mar 26, 2022
  2. ktbarrett commented on Sep 4, 2022

    @ktbarrett

    I'm getting a similar error in Python 3.10 if I use type annotations. inspect.getargspec was deprecated in 3.0. After looking at the Python 2 documentation, it seems it doesn't have the suggested replacement inspect.getfullargspec nor the more flexible inspect.signature. Fixing this is going to involve some version specific code.

  3. kasium commented on Sep 9, 2022

    @kasium

    It seems like invoke is not python 3.11 ready at all

  4. emcd commented on Oct 26, 2022

    @emcd

    Broken against the 3.11 release.

    Looks like @bitprophet has been working on getting rid of Python 2 support, which would make fixing this issue easier. That said, I agree with the original poster that a potential fix already exists in invoke.vendor.decorator. Seems like the invoke.tasks code could use the replacement getargspec implementation provided in that module and we would have a workaround for the issue.

  5. ian-em commented on Oct 27, 2022

    @ian-em

    Until an update is released this can be worked around temporarily with a hack to monkey patch the inspect module in your tasks.py file. Here is an example working for me on Python 3.11:

    import inspect
    
    if not hasattr(inspect, 'getargspec'):
        inspect.getargspec = inspect.getfullargspec
    
    from invoke import task
    
    @task
    def say_hello(c):
        print("Hello, World!")
  6. manykarim commented on Oct 27, 2022

    @manykarim

    Both PRs below would solve it, right?

    #458
    #606

  7. emcd commented on Oct 29, 2022

    @emcd

    I ended up forking this repo, applying this commit to the fork, and then referencing the package as invoke @ git+https://github.com/emcd/invoke.git@fb40a3c79b3fa59cb589a642d0c4b8fa87ca082e, which pip (and presumably other Python package utilities/systems) can use to build/install the package directly from the repo. Doing this fixed/worked around the Python 3.11 compatibility issue for me.

    Others could do likewise with their own forks, if they need to be unblocked. Not a great long-term solution, obviously....

  8. added 2 commits that reference this issue on Nov 9, 2022
  9. janrito commented on Nov 12, 2022

    @janrito

    This monkeypatch worked for me - adapted from @zelo's in #606

    # type: ignore
    from collections import namedtuple
    from inspect import getfullargspec
    from unittest.mock import patch
    
    import invoke
    
    
    def fix_annotations():
        """
        Pyinvoke doesn't accept annotations by default, this fix that
        Based on: @zelo's fix in https://github.com/pyinvoke/invoke/pull/606
        Context in: https://github.com/pyinvoke/invoke/issues/357
            Python 3.11 https://github.com/pyinvoke/invoke/issues/833
        """
    
        ArgSpec = namedtuple("ArgSpec", ["args", "defaults"])
    
        def patched_inspect_getargspec(func):
            spec = getfullargspec(func)
            return ArgSpec(spec.args, spec.defaults)
    
        org_task_argspec = invoke.tasks.Task.argspec
    
        def patched_task_argspec(*args, **kwargs):
            with patch(
                target="inspect.getargspec", new=patched_inspect_getargspec, create=True
            ):
                return org_task_argspec(*args, **kwargs)
    
        invoke.tasks.Task.argspec = patched_task_argspec
  10. added a commit that references this issue on Nov 12, 2022
  11. 11 remaining items

  12. kasium commented on Feb 15, 2023

    @kasium

    @barismutanpicus seems to be related to flask_class not invoke as you can see in the traceback

  13. bitprophet commented on Feb 16, 2023

    @bitprophet
    Member

    Yup, that's not us this time 😇

  14. HacKanCuBa commented on Mar 8, 2023

    @HacKanCuBa
    Author

    bookworm sid 12 (ubuntu-debian) /usr/bin/gnome-language-selector Traceback (most recent call last): File "/usr/bin/gnome-language-selector", line 5, in <module> from LanguageSelector.gtk.GtkLanguageSelector import GtkLanguageSelector File "/usr/lib/python3/dist-packages/LanguageSelector/gtk/GtkLanguageSelector.py", line 27, in <module> import aptdaemon.client File "/usr/lib/python3/dist-packages/aptdaemon/client.py", line 54, in <module> class AptTransaction(GObject.Object): File "/usr/lib/python3/dist-packages/aptdaemon/client.py", line 656, in AptTransaction @convert_dbus_exception ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/aptdaemon/errors.py", line 177, in convert_dbus_exception argnames, varargs, kwargs, defaults = inspect.getargspec(func)

    There's no mention of invoke in that trace, it doesn't seem to be related to this package :)
    Try on gnome-language-selector bug tracker, or ubuntu's bug tracker.

    BTW, I now feel guilty that this issue's name is attracting a lot of false positives :P
    I'm gonna change the title

  15. changed the title [-]AttributeError: module 'inspect' has no attribute 'getargspec'. Did you mean: 'getargs'?[/-] [+]Issue with Python 3.11, or type annotations: the inspect module changed[/+] on Mar 8, 2023
  16. added a commit that references this issue on Feb 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions