davidhalter / jedi

Awesome autocompletion, static analysis and refactoring library for python
http://jedi.readthedocs.io
Other
5.72k stars 502 forks source link

'TreeInstance' object has no attribute 'with_generics' when using typing.Literal["str"] return annotation on property #1990

Open Carreau opened 2 months ago

Carreau commented 2 months ago

See upstream issue ipython/ipython#14412, that I was able to reduce to

import jedi
import typing

class A:
    @property
    def value(self) -> typing.Literal["a"]:
        return "a"

a = A()

interp = jedi.Interpreter("a.", [{"a": a}])
interp.complete(line=1, column=2)
Traceback (most recent call last):
  File "/.../jtree.py", line 14, in <module>
    interp.complete(line=1, column=2)
  File "/.../jedi/api/helpers.py", line 487, in wrapper
    return func(self, line, column, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../jedi/api/__init__.py", line 215, in complete
    return completion.complete()
           ^^^^^^^^^^^^^^^^^^^^^
  File "/.../jedi/api/completion.py", line 170, in complete
    cached_name, completion_names = self._complete_python(leaf)
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../jedi/api/completion.py", line 284, in _complete_python
    cached_name, n = self._complete_trailer(dot.get_previous_leaf())
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../jedi/api/completion.py", line 399, in _complete_trailer
    return cached_name, self._complete_trailer_for_values(values)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../jedi/api/completion.py", line 404, in _complete_trailer_for_values
    return complete_trailer(user_context, values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../jedi/api/completion.py", line 550, in complete_trailer
    completion_names += filter.values()
                        ^^^^^^^^^^^^^^^
  File "/.../jedi/inference/compiled/value.py", line 494, in values
    names += self._get(
             ^^^^^^^^^^
  File "/.../jedi/inference/compiled/value.py", line 462, in _get
    ).execute_annotation()
      ^^^^^^^^^^^^^^^^^^^^
  File "/.../jedi/inference/compiled/value.py", line 260, in execute_annotation
    return ValueSet([
                    ^
  File "/.../jedi/inference/compiled/value.py", line 261, in <listcomp>
    v.with_generics(arguments)
    ^^^^^^^^^^^^^^^
AttributeError: 'TreeInstance' object has no attribute 'with_generics'
davidhalter commented 2 months ago

Thanks!

SarthakNikhal commented 2 months ago

@Carreau @davidhalter Can I help with this issue? Let me know if you have any ideas on fixing it.

davidhalter commented 2 months ago

I think Literal needs special casing here. This is definitely something can and should do. We should however probably try to also catch the case where with_generics does not exist on v. (Essentially catch the AttributeError)

jamespreed commented 1 month ago

This is affecting completion of pandas.Series objects as well.

import pandas as pd
from jedi import Interpreter

series = pd.Series([1,2,3])
code = 'series.'
ns = [{'series': series}]
Interpreter(code, ns).complete()

which raises the same error: AttributeError: 'TreeInstance' object has no attribute 'with_generics'