Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3.11: ImportError: cannot import name 'formatargspec' from 'inspect' #196

Closed
jwilk opened this issue Nov 8, 2021 · 4 comments
Closed

Comments

@jwilk
Copy link
Contributor

jwilk commented Nov 8, 2021

wrapt cannot be imported in Python 3.11:

$ python3.11 --version
Python 3.11.0a2

$ python3.11 -c 'import wrapt'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/jwilk/.local/lib/python3.11/site-packages/wrapt/__init__.py", line 10, in <module>
    from .decorators import (adapter_factory, AdapterFactory, decorator,
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jwilk/.local/lib/python3.11/site-packages/wrapt/decorators.py", line 34, in <module>
    from inspect import ismethod, isclass, formatargspec
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: cannot import name 'formatargspec' from 'inspect' (/usr/local/lib/python3.11/inspect.py)

This is most likely caused by python/cpython#28618 ("Remove deprecated inspect methods").

@GrahamDumpleton
Copy link
Owner

Can anyone suggest how I can replace what wrapt does when using Python 3.11.

The code is:

    # The 'adapter' argument is used to optionally denote a
    # separate function which is notionally used by an adapter
    # decorator. In that case parts of the function '__code__' and
    # '__defaults__' attributes are used from the adapter function
    # rather than those of the wrapped function. This allows for the
    # argument specification from inspect.getfullargspec() and similar
    # functions to be overridden with a prototype for a different
    # function than what was wrapped.

    ...

            if adapter:
                if isinstance(adapter, AdapterFactory):
                    adapter = adapter(wrapped)

                if not callable(adapter):
                    ns = {}

                    # Check if the signature argument specification has
                    # annotations. If it does then we need to remember
                    # it but also drop it when attempting to manufacture
                    # a standin adapter function. This is necessary else
                    # it will try and look up any types referenced in
                    # the annotations in the empty namespace we use,
                    # which will fail.

                    annotations = {}

                    if not isinstance(adapter, string_types):
                        if len(adapter) == 7:
                            annotations = adapter[-1]
                            adapter = adapter[:-1]
                        adapter = formatargspec(*adapter)

The comment doesn't really explain it too well, but docs also have an example in:

What the code is specifically looking for is where adapter is not a callable nor string, and thus should be an ArgSpec or FullArgSpec tuple. In the latter case annotations is ignored, and a string prototype for the function created without any annotations information.

Python docs say that instead of using formatargspec():

Use signature() and Signature Object, which provide a better introspecting API for callables.

How can they be used though? The signature() function is of no use:

and looks what I need to do is instead convert an ArgSpec or FullArgSpec into a Signature object instead, without annotation information, and then use str() on it.

If someone understands Signature objects can you supply an example of what is required to do that?

Also, wrapt probably needs to me modified so it can be supplied a Signature object for adapter as well, with a copy being made with annotations set, to generate the prototype string.

@jwilk
Copy link
Contributor Author

jwilk commented Nov 22, 2021

looks what I need to do is instead convert an ArgSpec or FullArgSpec into a Signature object instead, without annotation information, and then use str() on it.

I suspect the FullArgSpecSignature converter would be as complex as formatargspec(), but the latter is already implemented, so… How about just copy&pasting it from Python 3.10 stdlib?

Also, wrapt probably needs to me modified so it can be supplied a Signature object for adapter as well

Indeed.

@GrahamDumpleton
Copy link
Owner

It need not be that hard since getfullargspec() in recent Python versions is implemented using Signature. So the code gives pointers to how the process can be reversed.

Looks like just need to reconstruct the Parameter list and give it to Signature.

scop added a commit to scop/pytekukko that referenced this issue Dec 5, 2021
scop added a commit to scop/pytekukko that referenced this issue Dec 5, 2021
Fails at the moment because of
GrahamDumpleton/wrapt#196

There are some issues with compiling native versions of at least aiohttp
and yarl at the moment that could be worked around, but let's revisit
those when the wrapt issue is solved.
scop added a commit to scop/pytekukko that referenced this issue Dec 5, 2021
scop added a commit to scop/pytekukko that referenced this issue Dec 5, 2021
Fails at the moment because of
GrahamDumpleton/wrapt#196

There are some issues with compiling native versions of at least aiohttp
and yarl at the moment that could be worked around, but let's revisit
those when the wrapt issue is solved.
scop added a commit to scop/pylttoaine that referenced this issue Dec 9, 2021
LeMyst added a commit to LeMyst/WikibaseIntegrator that referenced this issue Feb 5, 2022
Zac-HD added a commit to HypothesisWorks/hypothesis that referenced this issue Feb 13, 2022
@GrahamDumpleton
Copy link
Owner

Should be fixed in 1.14.0.

scottwittenburg added a commit to scottwittenburg/spack-infrastructure that referenced this issue Nov 2, 2022
The sync script image became broken recently when it was rebuilt and
picked up python 3.11.  At that point, one of the dependencies of the
github package, wrapt, started failing.  See here:

GrahamDumpleton/wrapt#196

This pins python at 3.10 until the github package can update its
(possibly transitive) dependency on wrapt to the fixed version.
carmenbianca added a commit to coopiteasy/oca-addons-repo-template that referenced this issue Dec 15, 2022
pylint pulls a version of wrapt that is broken on Python 3.11. See
<GrahamDumpleton/wrapt#196>.

This breaks pre-commit for any devs running a modern OS.

Because we already pinned flake8 to Python 3.6 to circumvent a similar
problem, we now simply do it universally to prevent that whole class of
problems.

Signed-off-by: Carmen Bianca BAKKER <carmen@coopiteasy.be>
carmenbianca added a commit to coopiteasy/oca-addons-repo-template that referenced this issue Dec 15, 2022
pylint pulls a version of wrapt that is broken on Python 3.11. See
<GrahamDumpleton/wrapt#196>.

This breaks pre-commit for any devs running a modern OS.

Signed-off-by: Carmen Bianca BAKKER <carmen@coopiteasy.be>
brinkflew added a commit to brinkflew/odev that referenced this issue Dec 16, 2022
Add an entry point with basic checks for odev.
Enable logging and prompting to the console with limited helper methods.

[FIX] Pre-Commit PyLint hook version

The PyLint hook was using an older version not compatible with
Python 3.11.

Ref: GrahamDumpleton/wrapt#196
zamberjo pushed a commit to aurestic/oca-addons-repo-template that referenced this issue Feb 22, 2023
pylint pulls a version of wrapt that is broken on Python 3.11. See
<GrahamDumpleton/wrapt#196>.

This breaks pre-commit for any devs running a modern OS.

Signed-off-by: Carmen Bianca BAKKER <carmen@coopiteasy.be>
bazaah added a commit to bazaah/aur-ceph that referenced this issue May 12, 2023
cherrypy is a runtime dependency, while pylint is a build/lint dep, both
of them encountered import errors due to API removals in py3.11, see the
links for more.

pylint's is indirect, a dependency of a dependency:

  pylint->astroid->wrapt

but fixed by moving to a more recent version

- ceph-17.2.6-mgr-dashboard-cherrypy-18.patch
- ceph-17.2.6-mgr-dashboard-pylint-217.patch

Issue: #12
References: cherrypy/cherrypy@8245a74
References: GrahamDumpleton/wrapt#196
bbhtt added a commit to bbhtt/org.flathub.flatpak-external-data-checker that referenced this issue Nov 1, 2023
See GrahamDumpleton/wrapt#196

 - wrapt requires python-cryptography which requires rust
bbhtt added a commit to bbhtt/org.flathub.flatpak-external-data-checker that referenced this issue Nov 1, 2023
See GrahamDumpleton/wrapt#196

 - wrapt requires python-cryptography which requires rust
bbhtt added a commit to bbhtt/org.flathub.flatpak-external-data-checker that referenced this issue Nov 1, 2023
See GrahamDumpleton/wrapt#196

 - wrapt requires python-cryptography which requires rust
bbhtt added a commit to bbhtt/org.flathub.flatpak-external-data-checker that referenced this issue Nov 1, 2023
See GrahamDumpleton/wrapt#196

 - wrapt requires python-cryptography which requires rust
bbhtt added a commit to bbhtt/org.flathub.flatpak-external-data-checker that referenced this issue Nov 1, 2023
See GrahamDumpleton/wrapt#196

 - wrapt requires python-cryptography which requires rust
gasinvein pushed a commit to flathub/org.flathub.flatpak-external-data-checker that referenced this issue Nov 1, 2023
See GrahamDumpleton/wrapt#196

 - wrapt requires python-cryptography which requires rust
walter-hernandez added a commit to walter-hernandez/argilla that referenced this issue Apr 2, 2024
The issue is: "ImportError: cannot import name 'formatargspec' from 'inspect'". More details: GrahamDumpleton/wrapt#196
frascuchon added a commit to argilla-io/argilla that referenced this issue May 6, 2024
…3.11 (#4693)

<!-- Thanks for your contribution! As part of our Community Growers
initiative 🌱, we're donating Justdiggit bunds in your name to reforest
sub-Saharan Africa. To claim your Community Growers certificate, please
contact David Berenstein in our Slack community or fill in this form
https://tally.so/r/n9XrxK once your PR has been merged. -->

# Description

The issue is: "ImportError: cannot import name 'formatargspec' from
'inspect'" with wrapt library that happens for Python 3.11. More
details: GrahamDumpleton/wrapt#196

**Type of change**

(Please delete options that are not relevant. Remember to title the PR
according to the type of change)

- [X] Bug fix (non-breaking change which fixes an issue)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)

**How Has This Been Tested**

(Please describe the tests that you ran to verify your changes. And
ideally, reference `tests`)

- Use current tests of Argilla

**Checklist**

- [X] I followed the style guidelines of this project
- [X] I did a self-review of my code
- [X] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [X] I filled out [the contributor form](https://tally.so/r/n9XrxK)
(see text above)
- [X] I have added relevant notes to the `CHANGELOG.md` file (See
https://keepachangelog.com/)

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Francisco Aranda <francis@argilla.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants