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

If a decorator changes the function docstring, I would like to have the changed function doc displayed in the tooltip. #1921

Closed
Nlea opened this issue Feb 28, 2023 · 5 comments

Comments

@Nlea
Copy link

Nlea commented Feb 28, 2023

At the moment, Jedi makes an IDE like VS Code display the docstring of a function. Even though the function might have a decorator that changes the docstring of the function.

Example:

#Decorator
def magic: 
  def wrapper: 
      result = doSomeMagic() 
      return result 
   wrapper.__doc__ = f'{func.__doc__} and some other stufffunc.__doc__ = wrapper.__doc__
   return wrapper

#Function
@magic 
def sayHello(): 
   '''A simple function to print Hello''' 
    print("Hello")

If I call help(sayHello). I get the wrapper docs printed. If would use functools that would change, but then I change the function docs insight the wrapper.
If I hover over the function sayHello I get: “A simple function to print Hello”. What I would like to see is: “A simple function to print hello and some other stuff”, because the docstring was modified in the wrapper.

Is there a way that this could be included in the Jedi Logic? So that whenever a decorator changes the docstring of a function, the changed docstring is displayed instead of the static docstring from the function itself?

If you would be willing to accept a PR, I would be happy to work on it!

@davidhalter
Copy link
Owner

Did you try to use functools.wraps? AFAIK Jedi respects that.

@Nlea
Copy link
Author

Nlea commented Mar 1, 2023

Hey @davidhalter ,

Thanks for your quick reply. Using functool.wraps doesn't change the tooltip.

#Decorator
def magic: 
  @wraps(func)
  def wrapper: 
      result = doSomeMagic() 
      return result 
   func.__doc__ = f'{func.__doc__} and some other stuff’ 
   return wrapper

#Function
@magic 
def sayHello(): 
   '''A simple function to print Hello''' 
    print("Hello")

This will have the output: "A simple function to print Hello". So it ignores the assigned docstring in the wrapper for the function.

If I assign inside the wrapper the wrapper.__doc__ = f'{func.__doc__} and some other stuff’ I get the output "A simple function to print Hello and some other stuff".

But the Tooltip never changes and always displays the docstring from the function itself.

@davidhalter
Copy link
Owner

Oh ok, now I get it. I misunderstood you at first. This is something that I definitely do not want to support, because it's a side effect and it's very hard to deal with for very little gains. So, I'm sorry but this won't happen.

@davidhalter
Copy link
Owner

davidhalter commented Mar 1, 2023

I however just realized that you might be using it with an Interpreter, if that's the case and the __doc__ is not picked up correctly in that case, that's something you could potentially have a look at. What's not going to happen is static analysis on stuff like func.__doc__ = . This is for performance reasons and other issues.

@davidhalter
Copy link
Owner

Since you @Nlea wrote that you are using an IDE like VS Code, I'm pretty sure that this is a case that's not feasible to support. Sorry.

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

2 participants