Skip to content

Replace StartResponse arguments with ... #2379

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

Merged
merged 3 commits into from
Aug 15, 2018

Conversation

srittau
Copy link
Collaborator

@srittau srittau commented Aug 11, 2018

StartResponse callbacks are required to accept and optional third argument.
Currently, there is no good way to describe this using type hints.
Previously, a Union was used, but that causes mypy to complain about any call
of start_response().

StartResponse callbacks are required to accept and optional third argument.
Currently, there is no good way to describe this using type hints.
Previously, a Union was used, but that causes mypy to complain about any call
of start_response().
@podhmo
Copy link

podhmo commented Aug 11, 2018

Hello, I have the same problem, today.

In typeshed, cannot use mypy_extensions.VarArg(policy, maybe...), so cannot define the StartResponse type, that the type the function has default arguments.
Basically, using union type, conditional branching is needed.

Why don't you use Protocol, like this?

class StartResponse(Protocol):
    def __call__(
        self,
        status: str,
        headers: List[Tuple[str, str]],
        exc_info: Optional[Any] = None,
    ) -> Callable[[bytes], None]:
        ...

or

class StartResponse(Protocol):
    def __call__(
        self,
        status: str,
        headers: List[Tuple[str, str]],
        **extra: Any,
    ) -> Callable[[bytes], None]:
        ...

@JelleZijlstra
Copy link
Member

That's a good idea! I haven't tried it though.

@srittau
Copy link
Collaborator Author

srittau commented Aug 11, 2018

That's an interesting idea, indeed. Unfortunately, passing in a function where a protocol is expected does not work with mypy 0.620:

from typing import Protocol

class Caller(Protocol):
    def __call__(self) -> None: ...

def call() -> None:
    pass

def func(caller: Caller) -> None:
    pass

func(call)

Although maybe it should? After all, call is a callable object. And it would mean that we could construct any call syntax for functions and don't need special support in Callable for default functions, keyword arguments etc.

@JelleZijlstra JelleZijlstra merged commit f25c954 into python:master Aug 15, 2018
@srittau srittau deleted the wsgi-start-response branch August 15, 2018 19:06
yedpodtrzitko pushed a commit to yedpodtrzitko/typeshed that referenced this pull request Jan 23, 2019
StartResponse callbacks are required to accept and optional third argument.
Currently, there is no good way to describe this using type hints.
Previously, a Union was used, but that causes mypy to complain about any call
of start_response().
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.

3 participants