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

[Work in progress] Request scoped construction in FastApi #181

Merged
merged 25 commits into from
Nov 30, 2021

Conversation

meadsteve
Copy link
Owner

@meadsteve meadsteve commented Nov 27, 2021

fixes #180.

Usage:

# Define a contextmanager for the dependency. The __exit__ (finally in this case) will happen after the request has loaded.
@context_dependency_definition(container)
def _load_then_clean() -> Iterator[SomeThing]:
    try:
        yield SomeThing()
    finally:
        SomeThing.do_some_cleanup()

# This type then needs to be registered as a managed singleton:
deps = FastApiIntegration(
    container, request_context_singletons=[SomeThing]
)

or a context manager class:

class SomeManager:
    def __enter__(self):
        return SomeThing()

    def __exit__(self, exc_type, exc_val, exc_tb):
        pass # TODO: clean up

# Let the container now that SomeThing is managed by SomeManager
container[ContextManager[SomeThing]] = SomeManager

deps = FastApiIntegration(
    container, request_context_singletons=[SomeThing]
)

TODO:

  • Implement feature using ContextManagers
  • Document fastpi integration
  • Document manual usage of ContextContainer.
  • Add some test coverage around reusing and nesting ContextContainer
  • Provide decorator context_dependency_definition that wraps @contextmanager & registers a ContextManager[X].
  • Looks for ContextManager[X] first as this should be the default behaviour.
  • Test that context_dependency_definition doesn't define the managed dependency by default
  • Add usage of ContextContainer to the function decorators

@meadsteve meadsteve added the enhancement New feature or request label Nov 27, 2021
@meadsteve meadsteve changed the title [Work in progress] Request scoped construction [Work in progress] Request scoped construction in FastApi Nov 28, 2021
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Nov 30, 2021

Sourcery Code Quality Report

❌  Merging this PR will decrease code quality in the affected files by 0.26%.

Quality metrics Before After Change
Complexity 0.47 ⭐ 0.65 ⭐ 0.18 👎
Method Length 25.31 ⭐ 24.97 ⭐ -0.34 👍
Working memory 3.93 ⭐ 3.93 ⭐ 0.00
Quality 93.22% 92.96% -0.26% 👎
Other metrics Before After Change
Lines 450 573 123
Changed files Quality Before Quality After Quality Change
lagom/init.py 95.04% ⭐ 91.56% ⭐ -3.48% 👎
lagom/decorators.py 89.37% ⭐ 90.08% ⭐ 0.71% 👍
lagom/version.py 98.50% ⭐ 98.50% ⭐ 0.00%
lagom/integrations/fast_api.py 92.38% ⭐ 89.22% ⭐ -3.16% 👎
tests/examples.py 100.00% ⭐ 99.33% ⭐ -0.67% 👎
tests/test_dep_definition_functions.py 96.39% ⭐ 96.32% ⭐ -0.07% 👎
tests/integrations/test_fastapi.py 90.67% ⭐ 89.38% ⭐ -1.29% 👎
tests/integrations/fastapi_app/init.py 95.72% ⭐ 96.03% ⭐ 0.31% 👍

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

@meadsteve meadsteve merged commit cb3760a into master Nov 30, 2021
@meadsteve meadsteve deleted the container-as-context-manager branch November 30, 2021 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add request scoped context manager based resources for the fastapi integration
1 participant