Jedi does not recognise type parameter lists.
The Issue
Starting from Python 3.12, functions, classes and type aliases may contain a type parameter list. However, this new syntax is not supported by Jedi. For example:
import jedi
source = '''
def add(a: float, b: float) -> float:
"""Add two numbers."""
return a + b
def concat[A, B](a: list[A], b: list[B]) -> list[A | B]:
"""Concatenate two lists."""
return a + b
'''
script = jedi.Script(source)
add = script.infer(2, 7)
concat = script.infer(6, 10)
print(add) #-> [<Name full_name='__main__.add', description='def add'>]
print(concat) #-> []
Because of this, language servers that use Jedi (such as jedi-language-server or python-lsp-server) cannot provide hover information for objects with type parameter lists.
Version Information
- Python 3.12.6
- Jedi 0.19.1
My guess is that this is a parso issue (looking there reveals https://github.com/davidhalter/parso/issues/221), though Jedi may need subsequent changes to make use of the parameters.
@PeterJCLaw is correct. I wanted to change this in parso a while ago, which would have taken maybe an hour, but the problem is that this is absolutely non-trivial in Jedi and even simply ignoring it in Jedi is probably quite a bit of work.
I think this is affecting use of Jedi for jumping to code definitions (jedi-vim).
# class has generic type parameters
some_other_module.GenericClass # jump to this
=> jedi-vim: Couldn't find any definitions for this.
Works fine if I temporarily remove the type parameters from the class. This is using the latest version of Jedi: 0.19.2.
Just adding my voice here: I just went through my codebase converting lots of TypeVars to type parameter lists, and afterwards found that jumping to definitions no longer works for that stuff. This is using jedi 0.19.2 through pylsp in emacs.
@PeterJCLaw is correct. I wanted to change this in parso a while ago, which would have taken maybe an hour, but the problem is that this is absolutely non-trivial in Jedi and even simply ignoring it in Jedi is probably quite a bit of work.
Hello, could you describe what are the practical issues with fixing that? What is non-trivial about it?
The problem is that this affects a lot of code in Jedi and a lot of tests would need to be written.