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

cython with inject problem #107

Open
ehanhalici opened this issue Aug 30, 2024 · 1 comment
Open

cython with inject problem #107

ehanhalici opened this issue Aug 30, 2024 · 1 comment

Comments

@ehanhalici
Copy link

Hi, first of all I would like to thank you for your work.

As for my question, I use inject in my python flask project. There is no problem when working with python, but when I compile with cython, I get the error “init() takes exactly 2 positional arguments (1 given)”. the reason for this error is that inject cannot do its job

inject.py is


import importlib
import inspect
import pkgutil
from flask_injector import FlaskInjector
from injector import Binder, singleton

from src.resources.base_resource import BaseResource


def injects_config(app):
    def configure(binder: Binder):
        package = importlib.import_module("src.resources")
        
        for _, module_name, _ in pkgutil.walk_packages(package.__path__, package.__name__ + "."):
            module = importlib.import_module(module_name)
            for name, obj in inspect.getmembers(module):
                if inspect.isclass(obj) and issubclass(obj, BaseResource) and obj is not BaseResource:
                    print(f"Binding {obj} in module {module_name}")
                    binder.bind(obj, to=obj, scope=singleton)

    FlaskInjector(app=app, modules=[configure])

and using like this:

from injector import inject
class XResource(BaseResource):

    def __init__(self, x_service: XService):
        super().__init__()
        self.x_service = x_service

the code works fine in python but when I compile with cython I get the error “init() takes exactly 2 positional arguments (1 given)””

I don't get this error if I remove inject and derive objects manually, like this:

class XResource(BaseResource):

    def __init__(self):
        super().__init__()
        self.x_service = XService()

the library versions I use are as follows

Flask==2.2.5
Flask-Injector==0.14.0
injector==0.20.1

I am waiting for the help of those who have experience with the problem. Thank you very much.

@ivankorobkov
Copy link
Owner

Hi!

Take a look, there is no call to inject here.

from injector import inject

class XResource(BaseResource):
    def __init__(self, x_service: XService):
        super().__init__()
        self.x_service = x_service

Maybe, you meant:

class XResource(BaseResource):
    @inject.autoparams()
    def __init__(self, x_service: XService):
        pass

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

No branches or pull requests

2 participants