Coder Social home page Coder Social logo

giveme's People

Contributors

pohmelie avatar steinitzu avatar vasiliy566 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

giveme's Issues

Non-obvious error when you try to pass async function as dependency

If you try to as in example

import asyncio

from giveme import Injector

injector = Injector()


class SomeClass:
    async def start(self):
        ...

    async def ping(self):
        ...


@injector.register(name="some_cls")
async def some_class_fabric() -> SomeClass:
    some_class = SomeClass()
    await some_class.start()
    return some_class


@injector.inject
async def async_action(some_cls):
    await some_cls.ping()


asyncio.run(async_action())

you will get

Traceback (most recent call last):
  File "/Users/isaeev-vs/PycharmProjects/giveme/main.py", line 28, in <module>
    asyncio.run(async_action())
  File "/opt/homebrew/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/isaeev-vs/PycharmProjects/giveme/giveme/injector.py", line 217, in awrapper
    return await function(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/isaeev-vs/PycharmProjects/giveme/main.py", line 25, in async_action
    await some_cls.ping()
          ^^^^^^^^^^^^^
AttributeError: 'coroutine' object has no attribute 'ping'
sys:1: RuntimeWarning: coroutine 'some_class_fabric' was never awaited

Because here it is corutine object.

It was good if:

  1. If you try to pass async function as dependency - you get an understandable mistake that you did wrong
  2. If async function is dependency of another async function - it is possible to calculate dependence on injector.inject cause it will be in async wrapper

Get rid of global `Manager`

Currently a single Manager instance is created on import.
The manager acts a global registry for dependencies.

This means there's no easy way to create more than one dependency registry per python process, making giveme unsuitable for use in frameworks and projects which need clear separation between dependency graphs.

To solve this a new class should be introduced, (calling it Injector for now) and the decorators @register and @inject become instance methods of it.
Dependencies will then be bound to a particular instance of Injector rather than all dependencies being registered globally

example of new usage:

from giveme import Injector

my_injector = Injector()

@my_injector.register
def dependency():
    ...

@my_injector.inject
def do_stuff(dependency):
    ...

This is a breaking change so should be introduced with a middle-step with clear deprecation warnings and migration path from the old decorators.

Typing support

The project cannot be integrated in modern projects with static type checkers like mypy - will you support typing?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.