Skip to content

DOC: fixes classes decorated with set_module not showing its source on numpy's documentation (#28629) #28645

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

Merged
merged 5 commits into from
Apr 15, 2025

Conversation

aureliobarbosa
Copy link
Contributor

@aureliobarbosa aureliobarbosa commented Apr 4, 2025

This PR is intended to fix #28629

Classes decorated with the _utils/set_module decorator can't be inspected for their source file, and thus the current strategy for getting the source for these classes using sphinx extension linkcode do not work. Currently 25 classes are affected. The currently problem doesn't affect functions decorated with set_module.

This task should have been done in many ways (capturing __file__, __module__, or via __code__). I choose to capture __file__ because it allowed to fix the documentation with the inclusion of a very small code snipped on the function linkcode_resolve, used by linkcode to obtain the external link to numpy repository. It looked to me the capturing the original __module__ would involve more code and many classes decorated with set_module were just empty, thus avoiding the possibility of capturing any __code__ via function members.

avoid calling stdlib module inspect

TST: adds tests for set_module decorator

DOC: adds __file__ attribute to classes decorated with set_module

simplify set_module and change attribute name

DOC : sets __module_file__ when overriding a __module__ in classes

Specity Exceptions

fix lint error
@aureliobarbosa aureliobarbosa force-pushed the fix_decorated_class_link_doc branch from 6f7127e to 9e2f778 Compare April 8, 2025 14:26
@aureliobarbosa aureliobarbosa marked this pull request as ready for review April 8, 2025 14:56
@@ -26,6 +27,12 @@ def example():
"""
def decorator(func):
if module is not None:
if isinstance(func, type):
try:
func.__module_file__ = sys.modules.get(func.__module__).__file__
Copy link
Member

Choose a reason for hiding this comment

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

Let's just call this _module_file. Giving it a dunder name is confusing because someone might think it's a name for a real dunder attribute that CPython supports. Also Python itself could add a dunder attribute with this name in the future. Another way to think about it is the language itself owns the namespace for symbols with dunder names, but leaves the single leading underscore namespace available for private use by Python libraries.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@melissawm
Copy link
Member

Feel free to merge if you're happy but I'd love to take a look later today

@aureliobarbosa
Copy link
Contributor Author

@melissawm and @ngoldbaum Sorry, but unfortunately it is not building the path in the right way! I will give another shot at fixing this later.

@aureliobarbosa
Copy link
Contributor Author

Now it seems to be working. When building locally it points to the right file on the main branch, as expected. I checked it only for poly1d, so far. I don't now how to make a test for linkcode_resolve (should this be the case of a test?).

Would like the commits to be squashed?

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

This looks good to me, nice! Unfortunately, I don't think numpy is set up to do a test docs run, so I don't know how to test it other than what you did, by building locally.

Some absolutely nitpicky comments in-line -- and might as well squash the commits when you do those. (But perhaps see first if there are other comments, and of course check that they don't break things...)

@@ -26,6 +27,12 @@ def example():
"""
def decorator(func):
if module is not None:
if isinstance(func, type):
try:
func._module_file = sys.modules.get(func.__module__).__file__
Copy link
Contributor

Choose a reason for hiding this comment

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

Total nitpick, but just func._module_file = sys.modules[func.__module__].__file__ would be fine since you check for KeyError.

@aureliobarbosa
Copy link
Contributor Author

@melissawm As soon as you take a look and review I will squash the commits (and fetch, etc).

Copy link
Member

@melissawm melissawm left a comment

Choose a reason for hiding this comment

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

Cool fix for an annoying problem! Tested locally and it works great. Thanks!

EDIT: Feel free to squash but we can do it for you when merging. Cheers!

@ngoldbaum
Copy link
Member

Thanks @aureliobarbosa!

@ngoldbaum ngoldbaum merged commit e151f0d into numpy:main Apr 15, 2025
72 checks passed
@github-project-automation github-project-automation bot moved this from Awaiting a code review to Completed in NumPy first-time contributor PRs Apr 15, 2025
@aureliobarbosa aureliobarbosa deleted the fix_decorated_class_link_doc branch April 16, 2025 07:15
VascoConceicao pushed a commit to VascoConceicao/numpy that referenced this pull request May 6, 2025
…n numpy's documentation (numpy#28629) (numpy#28645)

* DOC: store file with set_module in classes

avoid calling stdlib module inspect

TST: adds tests for set_module decorator

DOC: adds __file__ attribute to classes decorated with set_module

simplify set_module and change attribute name

DOC : sets __module_file__ when overriding a __module__ in classes

Specity Exceptions

fix lint error

* rename variable

* fix variable name

* fix path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

DOC: any class decorated with @set_module does not show its source on automatically generated documentation
4 participants