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

Allow early Blueprint registrations to still apply later added objects #2260

Merged
merged 4 commits into from Nov 17, 2021

Conversation

ahopkins
Copy link
Member

@ahopkins ahopkins commented Oct 3, 2021

Resolves #2255

The purpose of this PR is to allow blueprints to be registered at anytime. The following example (which previously was not allowed) should now operate as expected.

from sanic import Blueprint, Sanic, text

app = Sanic("app")
bp = Blueprint("bp")

app.blueprint(bp)


@bp.route("/", methods=["GET", "post"])
async def handler(request):
    return text("...")

The difference is the order in which you register the Blueprint. With this PR, you can run app.blueprint(bp) at anytime: either before or after handlers are added to the Blueprint.

This change will behave slightly more obviously as it would otherwise be expected. That is to say that any item added to a Blueprint will be applied to the application instance(s) that the blueprint is registered on.

@ahopkins ahopkins requested a review from a team as a code owner October 3, 2021 13:30
@ahopkins ahopkins marked this pull request as draft October 3, 2021 13:30
@ahopkins ahopkins marked this pull request as ready for review November 15, 2021 09:34
@ahopkins
Copy link
Member Author

This one turned out to be a lot more difficult than originally anticipated. I think this is mainly attributable to the fact that we are storing the Future* objects as tuples. If they were proper objects, then we might be able to make some of the checking a little easier. This would of course be breaking, so it may be best to leave it for another day.

@ahopkins ahopkins added the needs review the PR appears ready but requires a review label Nov 15, 2021
@ahopkins ahopkins changed the title Allow both lazy and inline Blueprint registration Allow early Blueprint registrations to still apply later added objects Nov 15, 2021
Copy link
Member

@vltr vltr left a comment

Choose a reason for hiding this comment

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

this looks good! some interesting Python code applied to the solution as well 😎

@ahopkins ahopkins merged commit 85e7b71 into main Nov 17, 2021
@ahopkins ahopkins deleted the flexible-blueprint-registration branch November 17, 2021 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs review the PR appears ready but requires a review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

log a warning or error when adding something to blueprint after adding it to application
2 participants