On 03/12/2021 22:38, Chris Angelico wrote:
On Sat, Dec 4, 2021 at 8:18 AM Rob Cliffe via Python-ideas
<python-ideas@python.org> wrote:


On 03/12/2021 19:32, Adam Johnson wrote:
The first unwelcome surprise was:

      >>> def func(a=>[]):
      ...     return a
      ...

      >>> import inspect
      >>> inspect.signature(func).parameters['a'].default
      Ellipsis

Here the current behaviour of returning `Ellipsis` is very unfortunate,
and I think could lead to a lot of head scratching — people wondering
why they are getting ellipses in their code, seemingly from nowhere.
Sure, it can be noted in the official documentation that `Ellipsis` is
used as the indicator of late bound defaults, but third-party resources
which aim to explain the uses of `Ellipsis` would (with their current
content) leave someone clueless.

+1.
This may be a very naive question, apologies if it's nonsense.
Instead of Ellipsis, would it be possible to have a built-in LateBound
class and use instances of that class instead of Ellipsis?
The __str__ method of the inspect.Parameter class could be modified to
return something like
      "a=>[]"
(or whatever syntax is adopted for specifying late-bound defaults) in
such cases.
The __repr__ and __str__ methods of a LateBound object could return
something like, respectively,
      "LateBound('[]')"
      "[]"
I am sure there is code that uses inspect.signature that would be
broken, but isn't that inevitable anyway?

That's a possibility for the inspect module. For the core language -
and therefore for anything that directly inspects the function's
dunders - it's much more efficient to use a well-known object.
OK.
I'm guessing that by "well-known" you mean pre-existing.  Python has a number of built-in singleton objects (None, True, False, Ellipsis).  What about adding a new one called LateBound (or other bikeshed colour)?
Best wishes
Rob Cliffe
  In the
current implementation, I've kept inspect.signature() consistent with
the dunders, but if there's good reason to change, I wouldn't be
averse to it. But I would need feedback from people who make heavy use
of inspect.signature, as I don't do much with it other than the basics
of help().

Everything in the reference implementation that isn't part of the PEP
should be considered provisional at best :)

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/B3NBYKH3W2IR4MVWQBUZV4NKUHTEP6R3/
Code of Conduct: http://python.org/psf/codeofconduct/

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/IDV2X72WIM6THMM7KSGW6LZUINYXEVAO/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to