that-depends icon indicating copy to clipboard operation
that-depends copied to clipboard

Documentation

Open lesnik512 opened this issue 1 year ago • 1 comments

  1. Injection
  2. Frameworks examples
  3. Resolving class without provider

lesnik512 avatar May 12 '24 14:05 lesnik512

I can work on this documentation if you don't mind.

Denis-Frunza avatar Jun 17 '24 05:06 Denis-Frunza

@Denis-Frunza would be great!

lesnik512 avatar Jun 17 '24 05:06 lesnik512

what if AbstractResource link class will be context manager

class AbstractResource(Generic[T], ABC):
    """Abstract Resource Class."""

    @abstractmethod
    def __enter__(self) -> T:
        pass

    @abstractmethod
    def __exit__(self, exc_type, exc_val, exc_tb) -> None:
        pass

then

def create_resource(x: int, y: str) -> Iterator[str]:
    print(f"Creating resource with {x} and {y}")
    yield f"Resource with {x} and {y}"
    print(f"Cleaning up resource with {x} and {y}")

resource = Resource(create_resource, 42, "example")

with resource as res:
    print(res)  # Output: Resource with 42 and example

what do you think?

Denis-Frunza avatar Jun 17 '24 06:06 Denis-Frunza

@Denis-Frunza I'm not sure what is the use case of such context manager.

What I mean under term context resources is this https://github.com/modern-python/that-depends/blob/main/tests/providers/test_context_resources.py

lesnik512 avatar Jun 17 '24 06:06 lesnik512

I believe misunderstood Resource class and context resources. I think of Resource class as properly initializing the resource. This could involve setting up connections, opening files etc... after the resource is no longer needed, the Resource class ensures that it is properly cleaned up. This could involve closing connections, releasing file handles, or other teardown operations thus context manger can be helpful let me know if I am wrong.

Denis-Frunza avatar Jun 17 '24 06:06 Denis-Frunza

Resources are initialized automatically or by init_async_resources method and finalized through container's tear_down method. There is no need to have context managers in providers itselves

lesnik512 avatar Jun 17 '24 06:06 lesnik512

Resources are initialized automatically or by init_async_resources method and finalized through container's tear_down method. There is no need to have context managers in providers itselves

I got it, thank you for the explanation.

Denis-Frunza avatar Jun 17 '24 06:06 Denis-Frunza