Documentation
- Injection
- Frameworks examples
- Resolving class without provider
I can work on this documentation if you don't mind.
@Denis-Frunza would be great!
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 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
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.
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
Resources are initialized automatically or by
init_async_resourcesmethod and finalized through container'stear_downmethod. There is no need to have context managers in providers itselves
I got it, thank you for the explanation.