
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. 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