[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2021-07-17 Thread Łukasz Langa

Łukasz Langa  added the comment:

Thanks! ✨  ✨

--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2021-07-17 Thread Łukasz Langa

Łukasz Langa  added the comment:


New changeset df7c62980d15acd3125dfbd81546dad359f7add7 by Miss Islington (bot) 
in branch '3.9':
bpo-40897:Give priority to using the current class constructor in 
`inspect.signature` (GH-27177) (GH-27209)
https://github.com/python/cpython/commit/df7c62980d15acd3125dfbd81546dad359f7add7


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2021-07-17 Thread miss-islington


Change by miss-islington :


--
pull_requests: +25747
pull_request: https://github.com/python/cpython/pull/27209

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2021-07-17 Thread Łukasz Langa

Łukasz Langa  added the comment:


New changeset ed2db9b5940ccbc0bc72d01bf62d8b3095ccef21 by Łukasz Langa in 
branch '3.9':
bpo-40897: Partially backport GH-22583's refactor of inspect.py to allow bugfix 
backports (#27193)
https://github.com/python/cpython/commit/ed2db9b5940ccbc0bc72d01bf62d8b3095ccef21


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2021-07-16 Thread Łukasz Langa

Change by Łukasz Langa :


--
pull_requests: +25728
pull_request: https://github.com/python/cpython/pull/27193

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2021-07-16 Thread Łukasz Langa

Łukasz Langa  added the comment:

On second thought it's a bummer not to fix this in 3.9.x that will still be the 
only stable version until October. I'll refactor the relevant part of 
inspect.py in 3.9 to make the backport applicable.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2021-07-16 Thread Łukasz Langa

Łukasz Langa  added the comment:


New changeset 948e39a866ccf33b4e30668c3f88a95a65966159 by Miss Islington (bot) 
in branch '3.10':
bpo-40897:Give priority to using the current class constructor in 
`inspect.signature` (GH-27177) (#27189)
https://github.com/python/cpython/commit/948e39a866ccf33b4e30668c3f88a95a65966159


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2021-07-16 Thread Łukasz Langa

Łukasz Langa  added the comment:

We won't be backporting this fix to 3.9 due to larger changes between versions.

--
versions: +Python 3.10, Python 3.11

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2021-07-16 Thread miss-islington


Change by miss-islington :


--
nosy: +miss-islington
nosy_count: 10.0 -> 11.0
pull_requests: +25725
pull_request: https://github.com/python/cpython/pull/27189

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2021-07-16 Thread Łukasz Langa

Łukasz Langa  added the comment:


New changeset 6aab5f9bf303a8e4cd8377fabcdcb499e0541f9a by Weipeng Hong in 
branch 'main':
bpo-40897:Give priority to using the current class constructor in 
`inspect.signature` (#27177)
https://github.com/python/cpython/commit/6aab5f9bf303a8e4cd8377fabcdcb499e0541f9a


--
nosy: +lukasz.langa

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2021-07-16 Thread hongweipeng


hongweipeng  added the comment:

>>> from datetime import timedelta as a
>>> from _datetime import timedelta as b
>>> a is b
True
>>>

`timedelta` is a C-level class, so inspect.signature(timedelta) is the same 
with inspect.signature(int).

But `signature` allow C-level function such as `inspect.signature(len)`, I 
think one way to improve is to use the C-level function when the python-level 
function cannot be found.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2021-07-16 Thread hongweipeng


Change by hongweipeng :


--
pull_requests: +25714
pull_request: https://github.com/python/cpython/pull/27177

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2021-07-14 Thread Mauricio Villegas


Mauricio Villegas  added the comment:

I created another issue since the problem appears to be a bit different: 
https://bugs.python.org/issue44618

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2021-07-10 Thread Mauricio Villegas


Mauricio Villegas  added the comment:

I think this is affecting me or it is a new similar issue. And it is not only 
for python 3.9, but also from 3.6 up. I am working on making code configurable 
based on signatures (see 
https://jsonargparse.readthedocs.io/en/stable/#classes-methods-and-functions). 
Now we need this to work for datetime.timedelta which defines parameters in 
__new__ instead of __init__. The following happens:

>>> from datetime import timedelta
>>> import inspect
>>> inspect.signature(timedelta.__new__)

>>> inspect.signature(timedelta.__init__)

>>> inspect.signature(timedelta)
...
ValueError: no signature found for builtin type 

I am expecting to get parameters for days, seconds, microseconds, milliseconds, 
minutes, hours and weeks, see 
https://github.com/python/cpython/blob/bfe544d2f2c2e7a7c03a764bed3276a1e27a0f5c/Lib/datetime.py#L461-L462.

Hopefully this gives some insight into what should be done. Independent from 
the fix, I would like to know if currently there is any way I can get the 
signature for __new__ that I could use until there is a proper fix for it.

--
nosy: +mauvilsa

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2021-02-17 Thread Guido van Rossum


Guido van Rossum  added the comment:

I doubt that solution is correct, given that we already established that the 
problem is *not* specific to Generic.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2021-02-17 Thread Jonathan Slenders


Jonathan Slenders  added the comment:

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()

--
nosy: +jonathan.slenders

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2020-11-17 Thread hongweipeng


Change by hongweipeng :


--
keywords: +patch
nosy: +hongweipeng
nosy_count: 6.0 -> 7.0
pull_requests: +5
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/23336

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor

2020-06-08 Thread Guido van Rossum


Guido van Rossum  added the comment:

Changing the topic to not point fingers at Generic.

--
nosy:  -levkivskyi
title: Inheriting from Generic causes inspect.signature to always return 
(*args, **kwargs) for constructor (and all subclasses) -> Inheriting from class 
that defines __new__ causes inspect.signature to always return (*args, 
**kwargs) for constructor

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com