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

Patch to support Cython wrapper callables #451

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

piotrmaslanka
Copy link

@piotrmaslanka piotrmaslanka commented Jan 4, 2021

I've tried using Flasgger with endpoints being defined as Cython's defs. The one problem is that Flasgger expects them to have a __dict__, whereas builtins functions and methods don't have one, so I wrapped them in something like this:

class Callable:
    """
    Since Cython compiles defs into builtin_function_of_method which do not have
    a __dict__ this wraps out defs to give them one.
    """
    def __init__(self, fun):
        self.fun = fun
        self.__name__ = fun.__name__
        self.__doc__ = getattr(fun, '__doc__', '')
        self.__wrapped__ = fun

    def __call__(self, *args, **kwargs):
        return self.fun(*args, **kwargs)

While this works in Flask, Flasgger raises a TypeError in the first get_root_path in parse_docstring.
Exactly on the filename = inspect.getfile(obj) line. Sure enough, there are no files to view, since Cython has already compiled everything to a nice .so file.

I've worked out a patch that allows this kinds of usage patterns. The docs run just fine (I've tested it on my corporate-grade project), if anyone should define their endpoints as Cython builtins wrapped by Callable.

And just for the record, yes someone had to abuse Flask and Flasgger this way 🗡️

@piotrmaslanka
Copy link
Author

Guys, how about that patch?

@mjTree
Copy link

mjTree commented Jan 15, 2022

Hello, why did I use this patched version and still get an error after Cython compilation

@piotrmaslanka
Copy link
Author

Hello, why did I use this patched version and still get an error after Cython compilation

Could you attach the error message?

@piotrmaslanka
Copy link
Author

The views themselves should also be wrapped in following thing:

class Callable:
    """
    Since Cython compiles defs into builtin_function_of_method which do not have
    a __dict__ this wraps out defs to give them one. This is expected by Flasgger.
    """
    def __init__(self, fun):
        self.fun = fun
        self.__name__ = fun.__name__
        self.__doc__ = getattr(fun, '__doc__', '')
        self.__wrapped__ = fun

     def __call__(self, *args, **kwargs):
        return self.fun(*args, **kwargs)

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.

None yet

2 participants