-
Notifications
You must be signed in to change notification settings - Fork 134
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
Hook for modifying method signatures #459
Comments
@jacobtomlinson I've had similar cases happen and I wonder how "correct" that would be from a software point of view: def decorator(func: Callable) -> Callable:
pass
@decorator
async def async_func():
pass Technically the def decorator(func: Callable) -> Callable:
pass
async def async_func():
pass
wrapped_func = decorator(async_func) where I hope that made sense. Like I said, I'm not sure how "correct" it would be to completely "ommit" That being said, when I've had this kind of stuff happen, I've used python stub files to overwrite that. I believe it might work in this case? def async_func(): ... |
Follow up: It could actually be nice to have a feature enhancement in |
Thanks for the quick responses! My code does something similar to the following concept (but I've cut it down massively to make a simple example). import asyncio
from functools import wraps
from typing import Callable
def decorator(func: Callable) -> Callable:
@wraps(func)
def inner(*args, **kwargs):
asyncio.run(func(*args, **kwargs))
return inner
@decorator
async def foo():
await asyncio.sleep(1)
print("Hello World!")
foo() So in this case In my project it's slightly more complex because these are methods on classes, and the decorator is at the class level. There are over 40 classes, each with potentially tens of methods, so I'd prefer not to have to create a stub file and duplicate all of that structure. Ideally I'd love to be able to just hook |
Disclaimer: I am merely an autoapi enthusiast so I'll try to be as helpful as possible 😀 Right, that makes sense. I think there's still a question of whether completely overloading the original docstring makes sense. What I mean is that you're trying to overwrite your "decorated function's" docstring with the doctring from your That being said, if that is still what you want, the following might help: With more "traditional" sphinx doc, you'd probably do it like this: import asyncio
from functools import wraps
from typing import Callable
def decorator(func: Callable) -> Callable:
@wraps(func)
def inner(*args, **kwargs):
"""A new docstring""""
asyncio.run(func(*args, **kwargs))
return inner
@decorator
async def foo():
"""A docstring (to be overloaded)""""
await asyncio.sleep(1)
print("Hello World!")
foo() Obviously since sphinx-autoapi doesn't actually run any of the code this won't work here. I don't think the current tool provides "hooks" as you've described and I think the closest thing would be these "events" although I don't think they fit your use-case. It could be nice to have a feature to overload docstrings based on decorator docstrings: Or maybe even at this level with something like: |
I think If the def remove_async_property(app, what, name, obj, skip, options):
if what == "method":
# Need to figure out how to check if class that owns the method is decorated with my decorator
if ...:
if not name.startswith("_") and not name.startswith("async_"):
if "async" in obj.properties:
obj.properties.remove("async")
def setup(sphinx):
sphinx.connect("autoapi-skip-member", remove_async_property) |
The docs don't seem to point to any other events. From the code point of view, I think this is the only one in here. sphinx-autoapi/autoapi/_objects.py Lines 244 to 249 in d5f9e04
From that it seems that you can "easily" access the docstring on Main problem I see is that I don't think there's an easy way to get the decorator from within a As a hacky workaround, you could add something to you docstring (a decorated tag of sort) then use that to overload and modify your docstring within your Hope I could help! |
Ok I got things working using the It would be awesome if the |
Thanks for this plugin, it's really great!
I have a package which uses a class decorator to modify some methods of a class. Specifically it wraps coroutines in a sync wrapper.
When documenting with
sphinx-autoapi
the methods still show asasync
despite being replaced by a sync method by the decorator. I don't expect the parser to handle this, it's definitely an edge case, but I wondered if there was an event I could hook so I can write a plugin to fix this on my end?The text was updated successfully, but these errors were encountered: