On Sat, Dec 4, 2021 at 2:52 PM Rob Cliffe via Python-ideas <python-ideas@python.org> wrote:
On 04/12/2021 01:06, Chris Angelico wrote:
On Sat, Dec 4, 2021 at 11:59 AM Rob Cliffe via Python-ideas <python-ideas@python.org> wrote:
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).
Mainly by "well-known" I mean "not private to any particular module", so those four you mention are all well-known, but a singleton as part of the inspect module would be a pain, since the core language would have to import that (or it would have to be magically created).
What about adding a new one called LateBound (or other bikeshed colour)? What would be gained? You would still be able to use LateBound as an early-bound default, so you would still need the same dual check. I'm struggling here. Yes you could use LateBound as an early-bound default (or as a parameter value to explicitly pass to a function) but ISTM that such usages would be perverse. I've a gut feeling that a solution can be found (to avoid the "Ellipsis from nowhere" problem) but I can't put my finger on it. Maybe explicitly specifying LateBound could be an error, perhaps even a SyntaxError? Help, please!
It would be extremely odd if something could fail due to the precise object chosen. x = LateBound def f(x=x): pass # SyntaxError? Runtime error? Awkward. Problematic. And it wouldn't buy you anything anyway - consumers of inspect.Signature would still have to be aware of this special object, so you still have the same problems. ChrisA