Getting error when running inspect in python 3.11, win11

72 views
Skip to first unread message

Oberdan Santos

unread,
Nov 14, 2023, 1:17:23 PM11/14/23
to pylons-discuss
Hello!!
When trying to run my code I am receiving an error.

import inspect
import sys

try:
    import importlib.metadata as importlib_metadata  # noqa F401
except ImportError:  # pragma: no cover
    # bw-compat shim for py37
    import importlib_metadata  # noqa F401


def fix_type_error(exc_info, callable, varargs, kwargs):
    """
    Given an exception, this will test if the exception was due to a
    signature error, and annotate the error with better information if
    so.

    Usage::

      try:
          val = callable(*args, **kw)
      except TypeError:
          exc_info = fix_type_error(None, callable, args, kw)
          raise exc_info[0], exc_info[1], exc_info[2]
    """
    if exc_info is None:
        exc_info = sys.exc_info()
    if (
        exc_info[0] != TypeError
        or str(exc_info[1]).find('arguments') == -1
        or getattr(exc_info[1], '_type_error_fixed', False)
    ):
        return exc_info
    exc_info[1]._type_error_fixed = True
    argspec = inspect.formatargspec(*inspect.getargspec(callable))
    args = ', '.join(map(_short_repr, varargs))
    if kwargs and args:
        args += ', '
    if kwargs:
        kwargs = sorted(kwargs.items())
        args += ', '.join(['%s=...' % n for n, v in kwargs])
    gotspec = '(%s)' % args
    msg = f'{exc_info[1]}; got {gotspec}, wanted {argspec}'
    exc_info[1].args = (msg,)
    return exc_info



ERRO:
AttributeError: module 'inspect' has no attribute 'formatargspec'.
Did you mean: 'formatargvalues'?


I've tried some changes to the code line, but to no avail.
argspec = inspect.formatargspec(*inspect.getargspec(callable))
Every help is welcome



Laurent Daverio

unread,
Nov 14, 2023, 1:30:53 PM11/14/23
to pylons-...@googlegroups.com
Hello,

a couple of remarks before replying to your question :

1/ This list is dedicated to Pylons and Pyramid, so a general question
about the Python standard library is off-topic
2/ You could have googled "inspect formatargspec" and found answers
3/ You could have read the documentation of the standard library

The documentation of "inspect" for Python 3.10
(https://docs.python.org/3.10/library/inspect.html) states :

"Deprecated since version 3.5: Use signature() and Signature Object,
which provide a better introspecting API for callables."

And indeed, if you switch to the Python 3.11 / 3.12 versions of the
docs, you'll find that the function has been removed. You certainly
had a deprecation warning in Python 3.9/3.10, which you ignored

My $0.02,

Laurent.
> --
> You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/cab4b239-75df-44ba-9d39-4fea371e2b6dn%40googlegroups.com.

Oberdan Santos

unread,
Nov 14, 2023, 1:43:19 PM11/14/23
to pylons-discuss
Hello!! 
The question is, if this model is "Deprecated since version 3.5: Use signature() and Signature Object,

which provide a better introspecting API for callables."
Why does pyramid continue to carry this module in its base application structures, such as cookiecutter?
The application I am requesting support for is based on the Pyramd Cookiecutter application framework. In this sense, my question is pertinent.

Anyway, thank you for your feedback,

Oberdan Costa

Laurent Daverio

unread,
Nov 14, 2023, 2:00:06 PM11/14/23
to pylons-...@googlegroups.com
> The application I am requesting support for is based on the Pyramd Cookiecutter application framework. In this sense, my question is pertinent.

Oh, I see why you posted here, sorry then. For my defence, you
provided no context, no traceback, nothing indicating you were indeed
referring to Pyramid. You only wrote "When trying to run my code I am
receiving an error", and your code was only standard Python.

> Why does pyramid continue to carry this module in its base application structures, such as cookiecutter?

Actually, I'm not sure what problem you encountered :

- it could be in the cookiecutter module, which is not related to
Pyramid at all. It's definitely not a "base application structure" of
Pyramid. I don't use "cookiecutter" very often, but I'm pretty sure I
have used it with Python 3.11, and had no problem.

- it could be in the Pyramid scaffold you used, which is indirectly
related to Pyramid. I haven't used the "official" scaffold in a while,
but I fail to see why it would create a base application which
includes the "inspect" module.

- it could be in Pyramid itself. But I would rule out that choice, as
I use Pyramid with Python 3.11 daily, and the problem doesn't exist
for me.

When does your problem happen? When you're trying to use cookiecutter,
or when you try to launch the generated app?

Laurent.

PS: just a clarification in what I wrote previously: standard module
"inspect" is not deprecated at all, only function "formatargspec" is.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/82cb0cf3-0b00-4e6c-b788-3c71905f196an%40googlegroups.com.

Oberdan Santos

unread,
Nov 14, 2023, 2:15:50 PM11/14/23
to pylons-discuss
Thanks again for your attention. I will provide a brief contextualization.
I have a script based on FASTAPI for predicting the risk of chronic non-communicable diseases to aid clinical decision making in primary health care (backend), for now I will leave it like that. it works perfectly. 
On the frontend I use streamlit to obtain the results of up to 10 chronic diseases simultaneously. This ended up growing to include other modules (routing and intelligent management). At the moment I'm looking for a robust framework for this application purpose and the pyramid environment has allowed me to move in this direction (I'm based on cookiecutter) for my first experiences. I have a modular division already structured. 
Now I'm trying to connect to my database structure and I'm encountering this error when I run my application. From the request: I need help to take this step and fix this problem.

Oberdan Santos

unread,
Nov 14, 2023, 3:13:46 PM11/14/23
to pylons-discuss
Checking the literature on some sites, I see that I have two options ( inspect.signature() or inspect.getfullargspec() to solve the problem. The question is: How to use inspect.signature() or the other option, where to change it ? .The following is the function that was throwing error.
Desde já agradeço,
]
Oberdan Costa

Laurent Daverio

unread,
Nov 14, 2023, 3:16:54 PM11/14/23
to pylons-...@googlegroups.com
Thanks for the explanations :)

After some grepping in the source code, I have found that the function
you quoted belongs to third-party module PasteDeploy. Pyramid depends
on plaster-pastedeploy, which in turn depends on PasteDeploy.

And PasteDeploy (my version is 3.0.1) contains your code in file
paste/deploy/util.py, lines 13 to 46. Please note that it should be
fixed soon, as an unreleased change was recorded in Github last week :

https://github.com/Pylons/pastedeploy/blame/main/src/paste/deploy/util.py#L36

At this point, I would suggest 2 possibilities :

1/ You patch PastDeploy manually, or
2/ You determine who is calling that function, and why. For that, you
need to look at the traceback of the error.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/390dd860-0552-451b-8e88-77eeba1ee112n%40googlegroups.com.

Laurent Daverio

unread,
Nov 14, 2023, 3:17:54 PM11/14/23
to pylons-...@googlegroups.com
Our messages crossed, my previous one contains the answer to your question:

https://github.com/Pylons/pastedeploy/blame/main/src/paste/deploy/util.py#L36
> To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/f175279d-69fc-46fb-81dd-55b88aa93974n%40googlegroups.com.

Oberdan Santos

unread,
Nov 14, 2023, 4:29:44 PM11/14/23
to pylons-discuss

Thanks for the support. The error is gone, now I can see communication with the database.
My specialty is developing prediction models (Machine learning). If you need anything in this field, I am at your disposal.

Laurent Daverio

unread,
Nov 14, 2023, 4:34:21 PM11/14/23
to pylons-...@googlegroups.com
Glad to hear that, and many thanks for offering :) I'll let you know if I need.

You just mentioned a database, and it may be totally
irrelevant/unrelated, but it occurs to me that SQLAlchemy also has an
"inspect" function, which you use for introspecting your database. I
do use it, unlike the standard "inspect" :)

All the best,

Laurent.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/c9a5d252-4bc8-471f-98d9-15fa440702bdn%40googlegroups.com.

Michael Merickel

unread,
Nov 14, 2023, 9:43:54 PM11/14/23
to pylons-...@googlegroups.com
Just fyi there is a pull request on pastedeploy that patches the inspect issue on 3.12. I’m planning to get that released when I can find time this week.

- Michael

> On Nov 14, 2023, at 15:34, Laurent Daverio <ldav...@gmail.com> wrote:
>
> Glad to hear that, and many thanks for offering :) I'll let you know if I need.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/CAB7cU6w_gUL3gVzP1gKepEPOkD-RaciEMdptrbmKoft6xGSG0w%40mail.gmail.com.

Oberdan Santos

unread,
Nov 15, 2023, 6:11:38 AM11/15/23
to pylons-...@googlegroups.com
Obrigado Michael!!

Com os melhores cumprimentos / Best regards / Cordialement / Saludos cordiales


Reply all
Reply to author
Forward
0 new messages