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

@wraps decorator to expose underlying method documentation #573

Closed
wants to merge 2 commits into from

Conversation

hsvlz
Copy link
Contributor

@hsvlz hsvlz commented Oct 23, 2017

Hi Miguel,

adding a @wraps decorator to expose underlying method documentation for every socketio.on event

@miguelgrinberg
Copy link
Owner

@hsvlz the decorator does not wrap the original function, it returns the original function. Did you see a situation in which the documentation of the original function was lost?

@hsvlz
Copy link
Contributor Author

hsvlz commented Oct 23, 2017

When iterating over socketio.server.handlers.items() in my function generating the documentation:

for namespace, rules in socketio.server.handlers.items():
       for rule, func in rules.items():

func ends up always being _handler with no access to func_name or func_docs. By adding this @wraps, I can access the underlying function information of a function using the decorator @socketio.on(...)

@miguelgrinberg
Copy link
Owner

Are you aware that socketio.server is undocumented in Flask-SocketIO, and that server.handlers is undocumented in python-socketio, right? Those can change without deprecation or even notice, because applications are not supposed to use them.

@hsvlz
Copy link
Contributor Author

hsvlz commented Oct 23, 2017

Is there an alternative public way to iterate over function handlers, the same way it is safe to iterate over current_app.url_map.iter_rules() in flask? Otherwise, what do you propose in order to create auto-generated documentation of all socketio endpoints in realtime for a /documentation endpoint?

@miguelgrinberg
Copy link
Owner

No, there is currently no official way to iterate over the handlers. You could use a custom decorator to mark functions as needing documentation to avoid using the private attributes.

@hsvlz
Copy link
Contributor Author

hsvlz commented Oct 24, 2017

The issue with adding custom decorators for documentation is:

  • decorator hell (functions could end up with quite a few decorators and you need to repeat it for every single method)
  • unnecessary decorators are applied - for instance in production, documentation will be turned off, and therefore maintaining unnecessary data-structures and code for documentation will be executed regardless

@socketio.on() is guaranteed to be a decorator, therefore adding @wraps should not affect your implementation. I agree that your underlying implementation could evolve, and that both socketio.server and server.handlers are undocumented - I am using them at my own peril and should the implementation change, I would adapt.

@miguelgrinberg
Copy link
Owner

I'll think about this. It may make more sense to provide an iterator that can give you the actual functions, not the internal wrapper.

@hsvlz
Copy link
Contributor Author

hsvlz commented Oct 24, 2017

That would work for me! Thank you Miguel.

@hsvlz hsvlz reopened this Oct 24, 2017
@bpengu1n
Copy link

@miguelgrinberg Has there been any progress on adding an iterator for handler functions? If not, would you be open to a pull request submission for it? I'm writing a sphinx plugin to generate documentation for Flask_SocketIO API endpoints, and it's proving to be impractical without a way to get at those original references.

@miguelgrinberg
Copy link
Owner

@bpengu1n I haven't done any work on this issue. Open to accept a contribution for it. Thanks!

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

Successfully merging this pull request may close these issues.

None yet

3 participants