Skip to content

Implement StartResponse using a protocol #2392

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 4 commits into from
Aug 17, 2018

Conversation

srittau
Copy link
Collaborator

@srittau srittau commented Aug 16, 2018

Also export ExcInfo and OptExcInfo from sys

Test case:

from sys import OptExcInfo
from typing import List, Tuple, Callable, Any
from wsgiref.types import StartResponse

def my_sr(status: str, headers: List[Tuple[str, str]], exc_info: OptExcInfo = None) -> Callable[[bytes], Any]:
    return lambda _: None

def wrong_sr1(status: str, headers: List[Tuple[str, str]]) -> Callable[[bytes], Any]:
    return lambda _: None

def wrong_sr2(status: str, headers: List[Tuple[str, str]], exc_info: OptExcInfo) -> Callable[[bytes], Any]:
    return lambda _: None

def cb(sr: StartResponse) -> None:
    pass

cb(my_sr)  # no error
cb(wrong_sr1)  # type error
cb(wrong_sr2)  # type error

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Looks good, but I'd prefer to keep the stub-only aliases prefixed with an underscore. You can still use them in other stubs.

stdlib/2/sys.pyi Outdated
@@ -6,6 +6,10 @@ from typing import (
)
from types import FrameType, ModuleType, TracebackType, ClassType

# The following type alias are stub-only and do not exist during runtime
ExcInfo = Tuple[Type[BaseException], BaseException, TracebackType]
Copy link
Member

Choose a reason for hiding this comment

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

Then we should prefix these with an underscore.

python/mypy#1178 is about variable-length tuples, while exc_info()
always returns a tuple with length 3. Ideally, exc_info() would
return Union[Tuple[Type[_E], _E, TracebackType], Tuple[None, None, None]],
but that is a different issue.
@JelleZijlstra JelleZijlstra merged commit 25ac4d6 into python:master Aug 17, 2018
@srittau srittau deleted the start-response branch August 17, 2018 15:38
yedpodtrzitko pushed a commit to yedpodtrzitko/typeshed that referenced this pull request Jan 23, 2019
* Add ExcInfo and OptExcInfo type aliases

* Implement StartResponse using a protocol

* Mark stub-only types with an underscore

* Remove wrong TODO note

python/mypy#1178 is about variable-length tuples, while exc_info()
always returns a tuple with length 3. Ideally, exc_info() would
return Union[Tuple[Type[_E], _E, TracebackType], Tuple[None, None, None]],
but that is a different issue.
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.

2 participants