Skip to content

Conversation

@vallsv
Copy link
Contributor

@vallsv vallsv commented Apr 15, 2025

Propagated the types when @docstring is used.

See update_wrapper: https://docs.python.org/3/library/functools.html#functools.update_wrapper

The downfall is that we probably can't override the types, but that's probably not a common usecase.

As the documentation is on it's own a specification, IMO there is no way to distinguish the doc and the types -> If you have to tune the type you probably should not use the @docstring helper because you also have to tune the documentation.

Checklist:


@vallsv vallsv requested a review from t20100 April 15, 2025 09:19
raise ValueError("origin class has no %s method" % dest.__name__)

dest.__doc__ = origin.__doc__
functools.update_wrapper(dest, origin)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to specify the assigned and updated. According to the name of the function I would expect to have only __doc__ ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me this function is not properly named.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed I think this is related to the questions of thomas.

  • If the behavior change -> the function might be renamed
  • then if the behavior change the question is: is it safe for the existing source code ? This function is also exposed publicly so changing the behavior on other source code might be an issue.

@t20100
Copy link
Member

t20100 commented Apr 17, 2025

That sounds risky to propagate the typing in @docstring: The test in this PR is an example of what can happen.

If copying typing is needed, a different decorator would be best IMO.

@vallsv
Copy link
Contributor Author

vallsv commented Apr 17, 2025

The test in this PR is an example of what can happen.

I don't understand what you mean.

To me, we should have an @overrideannotation, but that's exactly how @docstring is used.

So, in fact i would be in favor of renaming this helper. Even if we only support classname as argument (which is safer in this case).

@t20100
Copy link
Member

t20100 commented May 7, 2025

You can achieve the same with @functools.wraps with almost the same syntax:

class A:
    def f(self, a: int, b: str = "") -> str:
        """docstring"""
        return f"{a}-{b}"

class B(A):
    @functools.wraps(A.f)
    def f(self, a, b):
        return "B"

I would keep @docstring to only update the docstring and use @functools.wraps to replace it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants