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

Add support of initializing the extension on a Blueprint #25

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

AlexJMohr
Copy link

@AlexJMohr AlexJMohr commented Nov 22, 2024

Closes #24

Adds support for initializing the extension on a Blueprint.

Work in progress, wanted to get your input on a few things.

Adds support for initializing the extension on a `Blueprint`.
Comment on lines 48 to 49
# TODO: maybe `Scaffold` instead?
FlaskOrBlueprint = Union[Flask, Blueprint]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you prefer Scaffold instead of this union type? Not sure what the implications are.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Python 3.10 typing supports and recommends the shorthand Flask | Blueprint . I think you can use this since it has been released for 3 years now. Also I'm not really sure you need to declare it as a new type. I find the following notation totally fine

def __init__(self, app: Optional[Flask | Blueprint] = None):

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't sure what your minimum supported python version is, which is why I went with Union. Technically python 3.9 is not end-of-life yet but up to you if you're supporting it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 80 to 84
# TODO: support multiple instances of Inertia, each on different
# blueprints? Possibly by using blueprint's name as part of the
# key in app.extensions. e.g:
# app.extensions[f"inertia.{state.blueprint.name}"] = self
self._init_extension(state.app)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are your thoughts on this? Not sure if there is a valid use-case for multiple Inertia instances on multiple blueprints.

The way I have it now, it would silently break in that case. Each blueprint registration would clobber the previous.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or if you can find a way to store a reference to the Inertia instance on the blueprint, that would be better. I didn't see any in the flask docs though. There is no extensions on Blueprints.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reason I would see using this extension on a Blueprint instead of on the current app is a migration plan since you can use the extension alongside a normal Flask app. You don't need to support multiple extension instances.

Comment on lines +45 to +46
class TestBlueprint(unittest.TestCase):
"""Flask-Inertia blueprint tests."""
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a new test class (and test file), but if you'd prefer a different setup let me know. There was some copy/pasting from test_app.py.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not against adding a new file to tests Blueprint but I would prefer to move the tests setup in the __init__ file instead of copying it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean making a base class in __init__.py which TestInertia and TestBlueprint would inherit from?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily a base class but maybe all the views and the config class to avoid copy/pasting.

Comment on lines 48 to 49
# TODO: maybe `Scaffold` instead?
FlaskOrBlueprint = Union[Flask, Blueprint]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Python 3.10 typing supports and recommends the shorthand Flask | Blueprint . I think you can use this since it has been released for 3 years now. Also I'm not really sure you need to declare it as a new type. I find the following notation totally fine

def __init__(self, app: Optional[Flask | Blueprint] = None):

Comment on lines 80 to 84
# TODO: support multiple instances of Inertia, each on different
# blueprints? Possibly by using blueprint's name as part of the
# key in app.extensions. e.g:
# app.extensions[f"inertia.{state.blueprint.name}"] = self
self._init_extension(state.app)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reason I would see using this extension on a Blueprint instead of on the current app is a migration plan since you can use the extension alongside a normal Flask app. You don't need to support multiple extension instances.

Comment on lines +45 to +46
class TestBlueprint(unittest.TestCase):
"""Flask-Inertia blueprint tests."""
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not against adding a new file to tests Blueprint but I would prefer to move the tests setup in the __init__ file instead of copying it.

flask_inertia/inertia.py Show resolved Hide resolved
from flask import Flask, Response, current_app, request
from flask import Blueprint, Flask, Response, current_app, request
from flask.sansio.app import App
from flask.sansio.blueprints import BlueprintSetupState
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you get this type in sansio instead of dedicated module ?

Suggested change
from flask.sansio.blueprints import BlueprintSetupState
from flask.blueprints import BlueprintSetupState

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto import. fixed: f5bdcdd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incremental Adoption Support
2 participants