Jedi: How can I get "default" param value?

Created on 5 Feb 2018  路  10Comments  路  Source: davidhalter/jedi

I want to extract default parameter from for call signature.

example (note: the code below is non-pep8 by design, to highlight that description is not the best place to fetch param's default value, but the only one possible place to find one)

# input
import jedi

source = '''
def foo(bar         = 0,
        another_arg = 1,
        other_arg   = 2):
    pass

foo('''

script = jedi.Script(source, 7, len('foo('), '')

for param in script.call_signatures()[0].params:
    print(param.name)
    print(param.description)

# output
bar
param bar         = 0
another_arg
param another_arg = 1
other_arg
param other_arg   = 2

I can't find a "value" or something like that in API. But I see that parso able to parse functions parameters in this way (fetch param names and default values).

Any suggestion how I can do this?

Thanks (:

feature

Most helpful comment

Might be coming when the typeshed branch is ready. But it will take another few months probably. It's definitely on my list.

Anyone who wants to help can fix all the bugs that are out there or checkout the typeshed branch and test it (and report back issues). That would help a lot.

All 10 comments

There is currently no API to do this. It's probably pretty easy to implement this, but at the moment the API simply doesn't exist.

@davidhalter thanks for response! looking forward to using it (:

PS: I hope I would have a time to look into existing API and try to implement.
How hard is this for the first PR?

I guess it wouldn't be too hard. The problem at the moment is that we don't have the structure in the API to do this kind of stuff. What I actually want is returning different class instances for the different types. But I guess this is still somewhat further away. IMO this has to wait a bit. Sorry.

No problem. Thanks for your support (:

I would also like this feature! :) I would be happy to help in any way

any update about this feature ?

Might be coming when the typeshed branch is ready. But it will take another few months probably. It's definitely on my list.

Anyone who wants to help can fix all the bugs that are out there or checkout the typeshed branch and test it (and report back issues). That would help a lot.

Now there is infer_default. There's also ways to format the parameter in the API now. This should help a lot with defaults. However you won't get the value 1. If you need that, let me know and we'll find a solution. You will get int() instead.

Is that good enough?

Yes, I would like to be able to access the default value and type, as well as allowed types e.g.
{ name: param1, default_type: int, default: 1, allowed_types: [int, bool, NoneType] }, { name: param2, default_type: bool, default: False, allowed_types: [str, List[float], Dict[str, str]]}

Why do you want that?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maxnordlund picture maxnordlund  路  5Comments

pwaller picture pwaller  路  9Comments

PeterJCLaw picture PeterJCLaw  路  8Comments

brettcannon picture brettcannon  路  4Comments

KyleMoser picture KyleMoser  路  6Comments