Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get AttributeError when getting property params #1139

Closed
pixystone opened this issue Jun 6, 2018 · 5 comments
Closed

get AttributeError when getting property params #1139

pixystone opened this issue Jun 6, 2018 · 5 comments

Comments

@pixystone
Copy link

>>> from jedi import Script
>>> source = """
... class C:
...   @property
...   def id(self):
...     return 1
... x = C()
... x.
... """
>>> s = Script(source, 7, 2)
>>> completion = s.completions()[0]
>>> completion.name
u'id'
>>> completion
<Completion: id>
>>> completion.params
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/jedi/cache.py", line 142, in wrapper
    result = method(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/jedi/api/classes.py", line 345, in params
    raise AttributeError()
AttributeError
@davidhalter
Copy link
Owner

This is intentional. The API documentation even mentions this. I would use this pattern for now:

if definition.type in ('function', 'class'):
    return completion.params()
else:
    return whatever

Would you propose a different solution to this? IMO just returning None is bad as well, returning an empty list does not let you differentiate between objects with parameters and objects without (so some information would get lost)...

If you have a better idea, please share it with me. I'm open for suggestions.

@pixystone
Copy link
Author

The definition type of property is also function.

>>> completion.type
'function'

I make a workaround to support @property, but I have no idea about @property.setter:

if definition.type in ('function', 'method'):
    def_code = definition.get_line_code(1)
    if '@property' in def_code and '@property.setter' not in def_code:
        params = ''
    else:
        params = definition.params()

@CMLL
Copy link

CMLL commented Jun 14, 2018

I stumbled unto this while using python-language-server, I put your workaround @pixystone in place and it worked pretty well, except that the code I was completing also had some setters in the form of
@property_name.setter so I used the following:

if "@" in def_code:
    params = ''

Seems to work well, but not sure if worth for a long term solution.

@davidhalter
Copy link
Owner

I mean if you just want to catch the AttributeError I would do it like this:

try:
    method = def.params
except AttributeError:
    params = []
else:
    params = method()

This would be the correct workaround IMO.

@davidhalter
Copy link
Owner

params will be deprecated and get_signatures returns a list that can be empty instead of the AttributeError.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants