-
Notifications
You must be signed in to change notification settings - Fork 62
Add decorator type hint #101
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
Open
dev0Guy
wants to merge
6
commits into
PythonNest:main
Choose a base branch
from
dev0Guy:dev-auto-complete
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,16 @@ | ||
| from typing import Callable, List, Union, TypeVar, TypeAlias | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ Failed check: CLI Test / test (SyncORM) Finding type: |
||
| from enum import Enum | ||
| from typing import Any, Callable, List, Union | ||
| import sys | ||
|
|
||
| if sys.version_info >= (3, 10): | ||
| from typing import ParamSpec | ||
| else: | ||
| from typing_extensions import ParamSpec | ||
|
|
||
|
|
||
| P = ParamSpec("P") | ||
| R = TypeVar("R") | ||
| Func: TypeAlias = Callable[[P], R] | ||
|
|
||
| class HTTPMethod(Enum): | ||
| GET = "GET" | ||
|
|
@@ -12,20 +22,22 @@ class HTTPMethod(Enum): | |
| OPTIONS = "OPTIONS" | ||
|
|
||
|
|
||
| def route(http_method: HTTPMethod, route_path: Union[str, List[str]] = "/", **kwargs): | ||
| def route(http_method: HTTPMethod, route_path: Union[str, List[str]] = "/", **kwargs) -> Callable[[Func], Func]: | ||
| """ | ||
| Decorator that defines a route for the controller. | ||
|
|
||
| Args: | ||
| http_method (HTTPMethod): The HTTP method for the route (GET, POST, DELETE, PUT, PATCH). | ||
| route_path (Union[str, List[str]]): The route path for the route. example: "/users" | ||
| route_path (Union[str, List[str]]): The route path for the route. | ||
| **kwargs: Additional keyword arguments to configure the route. | ||
|
|
||
| Returns: | ||
| function: The decorated function. | ||
| function: The decorated function, preserving the signature. | ||
| """ | ||
|
|
||
| def decorator(func): | ||
| def decorator(func: Func) -> Func: | ||
|
|
||
| # Add custom route metadata | ||
| func.__http_method__ = http_method | ||
| func.__route_path__ = route_path | ||
| func.__kwargs__ = kwargs | ||
|
|
@@ -35,29 +47,29 @@ def decorator(func): | |
| return decorator | ||
|
|
||
|
|
||
| def Get(route_path: Union[str, List[str]] = "/", **kwargs) -> Callable[..., Any]: | ||
| def Get(route_path: Union[str, List[str]] = "/", **kwargs): | ||
| return route(HTTPMethod.GET, route_path, **kwargs) | ||
|
|
||
|
|
||
| def Post(route_path: Union[str, List[str]] = "/", **kwargs) -> Callable[..., Any]: | ||
| def Post(route_path: Union[str, List[str]] = "/", **kwargs): | ||
| return route(HTTPMethod.POST, route_path, **kwargs) | ||
|
|
||
|
|
||
| def Delete(route_path: Union[str, List[str]] = "/", **kwargs) -> Callable[..., Any]: | ||
| def Delete(route_path: Union[str, List[str]] = "/", **kwargs): | ||
| return route(HTTPMethod.DELETE, route_path, **kwargs) | ||
|
|
||
|
|
||
| def Put(route_path: Union[str, List[str]] = "/", **kwargs) -> Callable[..., Any]: | ||
| def Put(route_path: Union[str, List[str]] = "/", **kwargs): | ||
| return route(HTTPMethod.PUT, route_path, **kwargs) | ||
|
|
||
|
|
||
| def Patch(route_path: Union[str, List[str]] = "/", **kwargs) -> Callable[..., Any]: | ||
| def Patch(route_path: Union[str, List[str]] = "/", **kwargs): | ||
| return route(HTTPMethod.PATCH, route_path, **kwargs) | ||
|
|
||
|
|
||
| def Head(route_path: Union[str, List[str]] = "/", **kwargs) -> Callable[..., Any]: | ||
| def Head(route_path: Union[str, List[str]] = "/", **kwargs): | ||
| return route(HTTPMethod.HEAD, route_path, **kwargs) | ||
|
|
||
|
|
||
| def Options(route_path: Union[str, List[str]] = "/", **kwargs) -> Callable[..., Any]: | ||
| def Options(route_path: Union[str, List[str]] = "/", **kwargs): | ||
| return route(HTTPMethod.OPTIONS, route_path, **kwargs) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❌ Failed check: CLI Test / test (PostgresSync)
I’ve attached the relevant part of the log for your convenience:
ImportError: cannot import name 'TypeAlias' from 'typing' module in Python 3.9Finding type:
Log ErrorThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this comment has been addressed. The commit 9de7fce modified the import statement to handle different Python versions, addressing the ImportError for TypeAlias in Python 3.9. The code now conditionally imports TypeAlias from typing_extensions for versions below 3.10, resolving the issue raised in the thread.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above