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

Auto generate route name #2928

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

Conversation

hamedsh
Copy link

@hamedsh hamedsh commented Mar 22, 2024

The ability to automatically generate names for routes has been added to Sanic. This feature is controlled by the 'generate_name' parameter available in the application, blueprint, and route definitions. Additionally, comprehensive tests have been added to ensure the correct functionality of this new feature.

a parameter added to the Sanic and Blueprint constructor
app = Sanic(name='hello', unique_route_name_generate=True)

in recent versions of Sanic, it's some kind of essential to assign names fo routes (it makes migrating to a new version of Sanic, very time-consuming). For example this little example:

app = Sanic(name='hello')

@app.route('/')
async def handler(request):
    await asyncio.sleep(2)
    print('Still running')
    return response.text('OK')


@app.route('/err')
async def handler(request):
    await asyncio.sleep(0.1)
    return response.text('OK')

by using this new argument, its becomes mandatory to assign names and be more backward compatible also naming can be ignored in most cases.

@hamedsh hamedsh requested a review from a team as a code owner March 22, 2024 07:05
Copy link

codecov bot commented Mar 22, 2024

Codecov Report

Attention: Patch coverage is 64.70588% with 6 lines in your changes are missing coverage. Please review.

Project coverage is 88.144%. Comparing base (1a70930) to head (7ff7939).

Files Patch % Lines
sanic/app.py 20.000% 4 Missing ⚠️
sanic/mixins/base.py 80.000% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##              main     #2928       +/-   ##
=============================================
+ Coverage   87.985%   88.144%   +0.158%     
=============================================
  Files           94        94               
  Lines         7408      7423       +15     
  Branches      1307      1306        -1     
=============================================
+ Hits          6518      6543       +25     
+ Misses         599       595        -4     
+ Partials       291       285        -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@ahopkins ahopkins left a comment

Choose a reason for hiding this comment

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

Can you explain a little the use case for this? I am trying to understand how it will be useful other than the two existing options:

  1. Use the handler function's name, or
  2. Explicit naming with name param.

I guess I can see possible a use case like:

class BaseMixin:
    def generate_name(self) -> str:
        return self._generate_name()

This would then expose it as an easily overridable function.

tests/test_blueprints.py Outdated Show resolved Hide resolved
sanic/app.py Outdated Show resolved Hide resolved
hamed added 3 commits March 25, 2024 09:32
Sanic mixins have been updated to support more informative route naming. `_generate_name()` is now enhanced to include methods and URIs as part of the route name, providing more context about the route configuration. The update also takes care of potential naming conflicts in different scenarios.
The ability to automatically generate names for routes has been added to Sanic. This feature is controlled by the 'generate_name' parameter available in the application, blueprint, and route definitions. Additionally, comprehensive tests have been added to ensure the correct functionality of this new feature.
@hamedsh hamedsh requested a review from ahopkins April 2, 2024 04:48
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.

None yet

2 participants