This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author jonathan.slenders
Recipients FFY00, ezyang, gvanrossum, hongweipeng, jonathan.slenders, ralf.gommers, serhiy.storchaka, yselivanov
Date 2021-02-17.10:49:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1613558962.78.0.682068263386.issue40897@roundup.psfhosted.org>
In-reply-to
Content
The following patch to inspect.py solves the issue that inspect.signature() returns the wrong signature on classes that inherit from Generic. Not 100% sure though if this implementation is the cleanest way possible. I've been looking into attaching a __wrapped__ to Generic as well, without success. I'm not very familiar with the inspect code.

To me, this fix is pretty important. ptpython, a Python REPL, has the ability to show the function signature of what the user is currently typing, and with codebases that have lots of generics, there's nothing really useful we can show.


 $ diff inspect.old.py  inspect.py  -p
*** inspect.old.py      2021-02-17 11:35:50.787234264 +0100
--- inspect.py  2021-02-17 11:35:10.131407202 +0100
*************** import sys
*** 44,49 ****
--- 44,50 ----
  import tokenize
  import token
  import types
+ import typing
  import warnings
  import functools
  import builtins
*************** def _signature_get_user_defined_method(c
*** 1715,1720 ****
--- 1716,1725 ----
      except AttributeError:
          return
      else:
+         if meth in (typing.Generic.__new__, typing.Protocol.__new__):
+             # Exclude methods from the typing module.
+             return
+
          if not isinstance(meth, _NonUserDefinedCallables):
              # Once '__signature__' will be added to 'C'-level
              # callables, this check won't be necessary


***

For those interested, the following monkey-patch has the same effect:

def monkey_patch_typing() -> None:
    import inspect, typing
    def _signature_get_user_defined_method(cls, method_name):
        try:
            meth = getattr(cls, method_name)
        except AttributeError:
            return
        else:
            if meth in (typing.Generic.__new__, typing.Protocol.__new__):
                # Exclude methods from the typing module.
                return

            if not isinstance(meth, inspect._NonUserDefinedCallables):
                # Once '__signature__' will be added to 'C'-level
                # callables, this check won't be necessary
                return meth

    inspect._signature_get_user_defined_method = _signature_get_user_defined_method

monkey_patch_typing()
History
Date User Action Args
2021-02-17 10:49:22jonathan.slenderssetrecipients: + jonathan.slenders, gvanrossum, ezyang, serhiy.storchaka, yselivanov, ralf.gommers, hongweipeng, FFY00
2021-02-17 10:49:22jonathan.slenderssetmessageid: <1613558962.78.0.682068263386.issue40897@roundup.psfhosted.org>
2021-02-17 10:49:22jonathan.slenderslinkissue40897 messages
2021-02-17 10:49:22jonathan.slenderscreate