Repository: davidhalter/jedi

Issue #1695: Complete returns a strange docstring if property has a return type hints None or Optional[...]
Created: 2020-11-11T09:33:12Z
Hello,
I would like to show this issue based on the following example (script test.py):
from typing import Optional, List

class Test:

    @property
    def prop1(self) -> int:
        """Returns prop1."""
        return 1
    
    @property
    def prop2(self) -> List[int]:
        """Returns a list of integer (prop2)."""
        return [2, 3, 4]
    
    @property
    def prop3(self) -> Optional[List[int]]:
        """Returns None or a list of integer (prop3)."""
        return [2, 3, 4]
    
    @property
    def prop4(self) -> None:
        """Non-sense property."""
        return None

The class Test has four different properties with different type hints for the returned value.
Now I use Jedi (0.17.2) together with Python 3.7.0 (Win 10, x64) and try to get the docstrings and type_hints of these properties. Therefore I created the following test script:
import jedi

code = \
"""import test
myTest = test.Test()
p1 = myTest.prop"""

script = jedi.Script(code=code)
compl = script.complete(line=3, column=14)

for c in compl:
    print(f"----- {c} -----")
    print("Docstring:\n", c.docstring())
    print("Return type:\n", c.get_type_hint())

This script is stored in the same folder than test.py, such that the import statement in the code literal can be resolved.
The output looks like this:
----- <Completion: prop1> -----
Docstring:
 Returns prop1.
Return type:
 int
----- <Completion: prop2> -----
Docstring:
 Returns a list of integer (prop2).
Return type:
 List[int]
----- <Completion: prop3> -----
Docstring:
 NoneType()

Returns None or a list of integer (prop3).
Return type:
 Optional[List[int]]
----- <Completion: prop4> -----
Docstring:
 NoneType()

Non-sense property.
Return type:
 None

I am wondering, why the docstring of prop1 and prop2, that are neither None nor Optional[...] contains the original
docstring only (this is what I would expect), however the docstrings of prop3 and prop4 start with NoneType(), followed
by a new line and the original docstring.
Is this desired behaviour?
Thank you very much.

Duplicate issue #2063: Incorrect docstring signature `NoneType()` for parameters annotated with `Optional` in completion
Created: 2025-04-24T15:55:40Z
In the following case I would expect the docstring to be empty. However, it is NoneType():
code = '''\
from typing import Optional

def func(arg: Optional[int]):
    pass

func(arg'''
script = jedi.Script(code=code)
completions = script.complete(
    line=len(code.splitlines()),
    column=len(code.splitlines()[-1])
)
assert len(completions) == 1
assert completions[0].docstring() == 'NoneType()'
This is regardless of what is within Optional. It works well with Literal or with just int or with other generics like dict[str, str].
The issue comes from _get_docstring_signature invoked here:

  
    
      jedi/jedi/api/classes.py
    
    
        Lines 300 to 308
      in
      c4f0538
    
  
  
    

        
          
           doc = self._get_docstring() 
        

        
          
           if raw: 
        

        
          
               return doc 
        

        
          
            
        

        
          
           signature_text = self._get_docstring_signature() 
        

        
          
           if signature_text and doc: 
        

        
          
               return signature_text + '\n\n' + doc 
        

        
          
           else: 
        

        
          
               return signature_text + doc

Closing comment (2025-04-24T21:10:04Z): It looks related to #1695 which was solved back in 2021 with 7d160f9