Skip to content

Hovered docstring on python function is not equal to the printed docstring #252391

Open
@mosc9575

Description

@mosc9575

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.101.1
  • OS Version: Windows_NT x64 10.0.19045

I am using a class to modify the docstring of python functions and classes. The code is derived from matplotlib.

# Substitution is derived from matplotlib.docstring (1.1.0)
# module https://matplotlib.org/users/license.html

class Substitution:
    """
    A decorator to take a function's docstring and perform string
    substitution on it.

    This decorator should be robust even if func.__doc__ is None
    (for example, if -OO was passed to the interpreter)

    Usage: construct a docstring.Substitution with a sequence or
    dictionary suitable for performing substitution; then
    decorate a suitable function with the constructed object. e.g.

    sub_author_name = Substitution(author='Jason')

    @sub_author_name
    def some_function(x):
        "%(author)s wrote this function"

    # note that some_function.__doc__ is now "Jason wrote this function"

    One can also use positional arguments.

    sub_first_last_names = Substitution('Edgar Allen', 'Poe')

    @sub_first_last_names
    def some_function(x):
        "%s %s wrote the Raven"
    """

    def __init__(self, *args, **kwargs) -> None:
        if args and kwargs:
            raise AssertionError("Only positional or keyword arguments are allowed.")

        self.params = args or kwargs

    def __call__(self, func):
        func.__doc__ = func.__doc__ and func.__doc__ % self.params
        return func

    def update(self, *args, **kwargs) -> None:
        """
        Update self.params with supplied args.
        """
        if isinstance(self.params, dict):
            self.params.update(*args, **kwargs)

The Problem is that the modification is not visible is the docstring which is shown when hovering over the function.

To show this on a simple example, please see the MRE below.

SIMPLE_DEFAULT="Test"
@Substitution(default_str=SIMPLE_DEFAULT)
def some_print_function(**kwargs):
    """Some function to show edited docstrings.
    
    Keyword Arguments:
        text (str, %(default_str)s): text to print
    """
    text = kwargs.get("text", SIMPLE_DEFAULT)
    print(text)

If I run print(some_print_function.__doc__) the output is

Some function to show edited docstrings.
    
    Keyword Arguments:
        text (str, Test): text to print

The hovered docstring looks like this:

Image

I want to mention, that in other IDEs the docstring is shown correctly.
The picture below is a screen shot from a Jupyter Lab.

Image

The behavior of VSCode results in hard to read docstrings and missing information for the users.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions