[Python-Dev] Rough idea for adding introspection information for builtins

Ronald Oussoren ronaldoussoren at mac.com
Tue Mar 19 10:42:16 CET 2013

On 19 Mar, 2013, at 10:24, Larry Hastings <larry at hastings.org> wrote:
>>> We'd want one more mild hack: the DSL will support positional 
>>> parameters, and inspect.Signature supports positional parameters, so 
>>> it'd be nice to render that information.  But we can't represent that in 
>>> Python syntax (or at least not yet!), so we can't let ast.parse see it. 
>>> My suggestion: run it through ast.parse, and if it throws a SyntaxError 
>>> see if the problem was a slash.  If it was, remove the slash, reprocess 
>>> through ast.parse, and remember that all parameters are positional-only 
>>> (and barf if there are kwonly, args, or kwargs). 
>> It will be simpler to use some one-character separator which shouldn't be used unquoted in the signature. I.e. LF.
> I had trouble understanding what you're suggesting.  What I think you're saying is, "normally these generated strings won't have LF in them.  So let's use LF as a harmless extra character that means 'this is a positional-only signature'."
> At one point Guido suggested / as syntax for exactly this case.  And while the LF approach is simpler programmatically, removing the slash and reparsing isn't terribly complicated; this part will be in Python, after all.  Meanwhile, I suggest that for human readability the slash is way more obvious--having a LF in the string mean this is awfully subtle.

You could also add the slash to the start of the signature, for example "/(arg1, arg2)", that way the positional only can be detected without trying to parse it first and removing a slash at the start is easier than removing it somewhere along a signature with arbitrary default values, such as "(arg1='/', arg2=4 /) -> 'arg1/arg2'".  The disadvantage is that you can't specify that only some of the arguments are positional-only, but that's not supported by PyArg_Parse... anyway.


More information about the Python-Dev mailing list