-
-
Notifications
You must be signed in to change notification settings - Fork 957
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 type hints to test_applications.py
#2471
Add type hints to test_applications.py
#2471
Conversation
tests/test_applications.py
Outdated
from starlette.types import ( | ||
ASGIApp, | ||
Receive, | ||
Scope, | ||
Send, | ||
) |
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.
Please stop doing the multiline when not needed.
from starlette.types import ( | |
ASGIApp, | |
Receive, | |
Scope, | |
Send, | |
) | |
from starlette.types import ASGIApp, Receive, Scope, Send |
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.
@Kludex You might want to add skip-magic-trailing-comma
to the Ruff formatter config, it would collapse multiline statements that fit on one line such as this one.
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.
Seprate PR welcome.
tests/test_applications.py
Outdated
from typing import ( | ||
Any, | ||
AsyncIterator, | ||
Callable, | ||
Generator, | ||
) |
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.
from typing import ( | |
Any, | |
AsyncIterator, | |
Callable, | |
Generator, | |
) | |
from typing import Any, AsyncIterator, Callable, Generator |
tests/test_applications.py
Outdated
def test_middleware_stack_init( | ||
test_client_factory: TestClientFactory, | ||
) -> None: |
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.
def test_middleware_stack_init( | |
test_client_factory: TestClientFactory, | |
) -> None: | |
def test_middleware_stack_init(test_client_factory: TestClientFactory) -> None: |
tests/test_applications.py
Outdated
@@ -98,7 +103,7 @@ def custom_ws_exception_handler(websocket: WebSocket, exc: CustomWSException): | |||
] | |||
) | |||
|
|||
exception_handlers = { | |||
exception_handlers: Any = { |
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.
Let's try to minimize the Any
s. Can we remove this one?
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.
If i remove any
from exception_handlers: Any
i get the following error:
error: Argument "exception_handlers" to "Starlette" has incompatible type "Dict[object, function]"; expected "Optional[Mapping[Any, Union[Callable[[Request, Exception], Union[Response, Awaitable[Response]]], Callable[[WebSocket, Exception], Awaitable[None]]]]]"
If I add exception_handlers: ExceptionHandlers
i get the following errors:
Dict entry 0 has incompatible type "int": "Callable[[Request, HTTPException], Coroutine[Any, Any, JSONResponse]]"; expected "Any": "Union[Callable[[Request, Exception], Union[Response, Awaitable[Response]]], Callable[[WebSocket, Exception], Awaitable[None]]]"
Dict entry 1 has incompatible type "int": "Callable[[Request, HTTPException], Coroutine[Any, Any, JSONResponse]]"; expected "Any": "Union[Callable[[Request, Exception], Union[Response, Awaitable[Response]]], Callable[[WebSocket, Exception], Awaitable[None]]]"
Dict entry 2 has incompatible type "Type[HTTPException]": "Callable[[Request, HTTPException], Coroutine[Any, Any, JSONResponse]]"; expected "Any": "Union[Callable[[Request, Exception], Union[Response, Awaitable[Response]]], Callable[[WebSocket, Exception], Awaitable[None]]]"
Dict entry 3 has incompatible type "Type[CustomWSException]": "Callable[[WebSocket, CustomWSException], None]"; expected "Any": "Union[Callable[[Request, Exception], Union[Response, Awaitable[Response]]], Callable[[WebSocket, Exception], Awaitable[None]]]"
If I add exception_handlers: Mapping[Any, ExceptionHandler]
as I saw in starlette/applications.py
i get the same errors as above.
If I add exception_handlers: dict[ typing.Any, typing.Callable[[Request, Exception], Response] ]
I get the following errors:
Dict entry 0 has incompatible type "int": "Callable[[Request, HTTPException], Coroutine[Any, Any, JSONResponse]]"; expected "Any": "Callable[[Request, Exception], Response]"
Dict entry 1 has incompatible type "int": "Callable[[Request, HTTPException], Coroutine[Any, Any, JSONResponse]]"; expected "Any": "Callable[[Request, Exception], Response]"
Dict entry 2 has incompatible type "Type[HTTPException]": "Callable[[Request, HTTPException], Coroutine[Any, Any, JSONResponse]]"; expected "Any": "Callable[[Request, Exception], Response]"
Dict entry 3 has incompatible type "Type[CustomWSException]": "Callable[[WebSocket, CustomWSException], None]"; expected "Any": "Callable[[Request, Exception], Response]"
I have searched everywhere related to exception_handlers
type hints but this is all I could find and it looks like nothing is working. That's why I have added any
there. Do you have any idea on how to fix this?
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.
I'd prefer the type: ignore
than Any
.
test_applications.py
test_applications.py
test_applications.py
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.
Anys need to be removed.
tests/test_applications.py
Outdated
startup_complete = False | ||
cleanup_complete = False | ||
|
||
@asynccontextmanager | ||
async def lifespan(app): | ||
async def lifespan(app: ASGIApp) -> Any: |
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.
remove the anys plz
async def lifespan(app: ASGIApp) -> Any: | |
async def lifespan(app: ASGIApp) -> Iterator[None]: |
…k/starlette into improve-types/test_applications
* added type annotations to test_applications.py * requested changes * Apply suggestions from code review * Apply suggestions from code review * Update tests/test_applications.py --------- Co-authored-by: Scirlat Danut <[email protected]> Co-authored-by: Marcelo Trylesinski <[email protected]>
Summary
Related to this
Type annotation added to
test_applications.py
Checklist