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

Deprecate Starlette and Router decorators #1897

Merged
merged 15 commits into from
Dec 3, 2022

Conversation

Kludex
Copy link
Member

@Kludex Kludex commented Oct 5, 2022

Description

The motivation of this PR is to deprecate part of the code that is discouraged for around 4 years.

As we discussed, this PR only deprecates decorators! The methods that are used by them are not deprecated.

Deprecation Table

Term Description
✔️ Stay
Deprecated
N/A

Methods

- add_middleware add_event_handler add_route add_websocket mount host add_exception_handler
Starlette ✔️ ✔️ ✔️ ✔️ ✔️ ✔️ ✔️
Router ✔️ ✔️ ✔️ ✔️ ✔️

Decorators

- middleware on_event route websocket exception_handler
Starlette
Router

@Kludex Kludex force-pushed the deprecate/decorators-and-similar branch 2 times, most recently from 5c14be1 to 465d536 Compare October 5, 2022 19:32
@Kludex Kludex mentioned this pull request Oct 5, 2022
11 tasks
@@ -864,24 +864,6 @@ def test_mount_asgi_app_with_middleware_url_path_for() -> None:
mounted_app_with_middleware.url_path_for("route")


def test_add_route_to_app_after_mount(
Copy link
Member

Choose a reason for hiding this comment

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

I'd keep this test as long as the feature code exists.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll readd it with the warning filter.

@alex-oleshkevich
Copy link
Member

Won't it emit two same warnings when using app.mount? If so then keeping warnings in router is enough.

@Kludex
Copy link
Member Author

Kludex commented Oct 5, 2022

Won't it emit two same warnings when using app.mount? If so then keeping warnings in router is enough.

yep, it's written on the description. I'll remove the duplications.

@Kludex Kludex force-pushed the deprecate/decorators-and-similar branch from 303abfd to 51e22d6 Compare October 12, 2022 07:19
@Kludex
Copy link
Member Author

Kludex commented Oct 12, 2022

I've readded the methods, because I think they are actually handy, and only deprecate the decorators. Based mainly on...

image

@Kludex Kludex requested a review from a team October 12, 2022 07:46
@Kludex Kludex mentioned this pull request Oct 12, 2022
@adriangb
Copy link
Member

adriangb commented Oct 12, 2022

I'd lean towards removing the methods as well. I remember users complaining about add_middleware rebuilding the entire middleware stack -> instantiating their middleware twice. I think no one should rely on their class being instantiated only once but I do agree it's confusing behavior.

You literally already mentioned that, my bad. I'd still vote for removing all of the methods in favor of more "immutable" classes, but mostly because of personal preference.

@Kludex Kludex added the clean up Refinement to existing functionality label Oct 12, 2022
@Kludex Kludex added this to the Version 0.22.0 milestone Oct 12, 2022
@Kludex
Copy link
Member Author

Kludex commented Oct 12, 2022

You literally already mentioned that, my bad. I'd still vote for removing all of the methods in favor of more "immutable" classes, but mostly because of personal preference.

Those methods may be handy for third party packages. For example, I have a workaround to duplicate endpoint paths that end with "/" with "" and vice-versa, so my application doesn't redirect. Let's not focus on how valid the example is (👀), but the fact that people may be using them for other ideas.

Given that the discussions previously were focusing on the decorators, I would prefer to deal with them separately, or even not deal with them at all, and let them stay.

@tomchristie
Copy link
Member

tomchristie commented Oct 13, 2022

Okay, I needed a bit of time to get back to speed on this.

So, everything listed here is clearly marked in the codebase as "don't use this", and none of these methods or decorators exist in the documentation. Personally I think everything here is on a deprecation line, we just haven't escalated that into warnings or removals yet. But anyways.

Where are we?...

  • Given that there's some lack of clarity over if we want to treat the methods and the decorators listed here in the same way we probably should consider them as separate issues.
  • Wrt. the decorators, I think we're all okay with pushing forward with their deprecation. Is there any disagreement there?
  • I don't think this pull request should be removing any of the code comments warning against usage. I think it should either... (1) add warnings to each of the decorator cases. or (2) outright remove each of the decorator cases, possibly just leaving stubs with error exceptions to help guide anyone who was still using them.

@Kludex
Copy link
Member Author

Kludex commented Oct 13, 2022

Given that there's some lack of clarity over if we want to treat the methods and the decorators listed here in the same way we probably should consider them as separate issues.

Sure. We should take a decision on the methods before 1.0.0.

Possible paths:

  1. Deprecate and remove before 1.0.0.
  2. Document and add tests (as there are pragma: no cover there).

Wrt. the decorators, I think we're all okay with pushing forward with their deprecation. Is there any disagreement there?

No disagreement, we can proceed.

I don't think this pull request should be removing any of the code comments warning against usage. I think it should either... (1) add warnings to each of the decorator cases. or (2) outright remove each of the decorator cases, possibly just leaving stubs with error exceptions to help guide anyone who was still using them.

I'll readd the docstrings.

@Kludex
Copy link
Member Author

Kludex commented Oct 13, 2022

I'll remove the deprecation on add_middleware from this PR, just to shrink the scope. But add_middleware is a different case than the other methods, as it has a side effect (mentioned in the description).

@Kludex
Copy link
Member Author

Kludex commented Oct 13, 2022

The docstrings I've removed are from methods, not from decorators. JFYK

@Kludex Kludex requested a review from adriangb October 22, 2022 07:20
@Kludex
Copy link
Member Author

Kludex commented Oct 22, 2022

This is ready for review. 👀

@florimondmanca
Copy link
Member

florimondmanca commented Nov 14, 2022

I'm quite happy with the separation b/w treating decorators vs methods. This keeps an escape hatch for users (including frameworks) to use the methods to alter the app after-the-fact, but officialises that as far as Starlette is concerned, the decorator API shouldn't exist anymore.

Should there be tests on deprecated code usage?

Using this: https://docs.pytest.org/en/7.1.x/how-to/capture-warnings.html#ensuring-code-triggers-a-deprecation-warning

def test_decorator_deprecations():
    app = Starlette()

    with pytest.deprecated_call():
        app.middleware(...) 

    ...

Copy link
Member

@tiangolo tiangolo left a comment

Choose a reason for hiding this comment

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

Yep, looks good! There's probably a bunch of things that will have to be done in FastAPI but this makes sense! 🤓

@Kludex
Copy link
Member Author

Kludex commented Nov 16, 2022

I'm not going to include this on the 0.22.0 release because it implies FastAPI changes. I'll include this (with tests @florimondmanca 🙏) on the 0.23.0 release.

@Kludex Kludex modified the milestones: Version 0.22.0, Version 0.23.0 Nov 20, 2022
@Kludex Kludex force-pushed the deprecate/decorators-and-similar branch from 09e5b9c to d9db160 Compare November 28, 2022 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clean up Refinement to existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants