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

Write docs on context resources #15

Closed
lesnik512 opened this issue May 12, 2024 · 7 comments · Fixed by #41
Closed

Write docs on context resources #15

lesnik512 opened this issue May 12, 2024 · 7 comments · Fixed by #41
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@lesnik512
Copy link
Member

lesnik512 commented May 12, 2024

No description provided.

@lesnik512 lesnik512 self-assigned this May 12, 2024
@lesnik512 lesnik512 changed the title Documentation Write docs on context resources Jun 15, 2024
@lesnik512 lesnik512 added the documentation Improvements or additions to documentation label Jun 15, 2024
@Denis-Frunza
Copy link
Contributor

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

@lesnik512
Copy link
Member Author

@Denis-Frunza would be great!

@lesnik512 lesnik512 removed their assignment Jun 17, 2024
@Denis-Frunza
Copy link
Contributor

Denis-Frunza commented Jun 17, 2024

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?

@lesnik512
Copy link
Member Author

@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

@Denis-Frunza
Copy link
Contributor

Denis-Frunza commented Jun 17, 2024

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.

@lesnik512
Copy link
Member Author

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

@Denis-Frunza
Copy link
Contributor

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.

@lesnik512 lesnik512 self-assigned this Jun 21, 2024
@lesnik512 lesnik512 linked a pull request Jun 21, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants