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

Replace current_app.extensions-usages and proxies by access wrappers #454

Open
lukasjuhrich opened this issue Mar 8, 2023 · 1 comment

Comments

@lukasjuhrich
Copy link
Collaborator

Rationale

Instead of using LocalProxys ourselves, we should just store the relevant objects in g and access that.
This would make the code easier to read and more in line with flask tutorials.

To make this well-typed, we could add a „type shim“ for g, which knows about the stored attributes, and import that.

Usages

backends: Backends = cast(Backends,
LocalProxy(lambda: current_app.extensions['backends']))

api: PycroftApi = LocalProxy(lambda: current_app.extensions['pycroft_api'])

config = LocalProxy(lambda: current_app.extensions['sample_users'])

api: PycroftApi = LocalProxy(lambda: current_app.extensions['pycroft_api'])

database = current_app.extensions['db_helios']

@lukasjuhrich
Copy link
Collaborator Author

This was based on a misunderstanding of the scope of the g proxy: It is lifetime-bound to the application context, however usually there is one application context per request.

Using app.extensions is fine, however for type-safe access one might go the flask_babel way and provide well-typed access methods like get_backends(app) / get_api(app).

@lukasjuhrich lukasjuhrich changed the title Use g instead of current_app.extensions Replace current_app.extensions-usages and proxies by access wrappers Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant