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

Type error when using async function based view in url path function #1874

Closed
medihack opened this issue Dec 13, 2023 · 4 comments
Closed

Type error when using async function based view in url path function #1874

medihack opened this issue Dec 13, 2023 · 4 comments
Labels
bug Something isn't working stubs Issues in stubs files (.pyi)

Comments

@medihack
Copy link

Bug report

What's wrong

When using the below snippet ...

from django.http import HttpResponse
from django.urls import path


def sync_view() -> HttpResponse:
    return HttpResponse(status=200)

async def async_view(): 
    return HttpResponse(status=200)

urlpatterns = [
    path("foo/", sync_view),
    path("bar/", async_view)  # <-- type error
]

... I got the below type error when calling path with the async view:

No overloads for "path" match the provided arguments Pyright[reportGeneralTypeIssues](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportGeneralTypeIssues)
conf.pyi(26, 5): Overload 3 is the closest match
Argument of type "() -> Coroutine[Any, Any, HttpResponse]" cannot be assigned to parameter "view" of type "Sequence[URLResolver | str]" in function "path"
  "function" is incompatible with "Sequence[URLResolver | str]"Pyright[reportGeneralTypeIssues](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportGeneralTypeIssues)
def async_view()
Full name: adit.dicom_explorer.urls.async_view
(function) def async_view() -> Coroutine[Any, Any, HttpResponse]

I used the latest pyright version (not tested with mypy).

How it should be

path function should accept an async function based view.

System information

  • OS:
  • python version: 3.11
  • django version: 4.27
  • pyright version: 1.1.339
  • django-stubs version: 4.2.7
  • django-stubs-ext version: 4.2.7
@medihack medihack added the bug Something isn't working label Dec 13, 2023
@intgr intgr added the stubs Issues in stubs files (.pyi) label Dec 13, 2023
@medihack
Copy link
Author

medihack commented Dec 13, 2023

Just to complete things ... I also tested this with mypy v1.7.1, and it also gives me a type error there:

Argument 2 to "path" has incompatible type "Callable[[], Coroutine[Any, Any, HttpResponseBase]]"; expected "Callable[..., HttpResponseBase]" [arg-type]

As a side note, django-types (from which I tried to switch) does not seem to have this problem.

@bmrobin
Copy link

bmrobin commented Apr 10, 2024

i'm also encountering this issue as well -- is there any update on this?

@jlost
Copy link
Contributor

jlost commented Apr 25, 2024

Same problem
Seems like you should be able to solve with something like:

# path()
@overload
def path(
    route: _StrOrPromise, view: Callable[..., Coroutine[Any, Any, HttpResponseBase]], kwargs: dict[str, Any] = ..., name: str = ...
) -> URLPattern: ...

# re_path()
@overload
def re_path(
    route: _StrOrPromise, view: Callable[..., Coroutine[Any, Any, HttpResponseBase]], kwargs: dict[str, Any] = ..., name: str = ...
) -> URLPattern: ...

@flaeppe
Copy link
Member

flaeppe commented Apr 27, 2024

Closed via #2085

@flaeppe flaeppe closed this as completed Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stubs Issues in stubs files (.pyi)
Development

Successfully merging a pull request may close this issue.

5 participants