Skip to content

Typing: use wsgiref.types to validate types and fix issues uncovered - #2467

Merged
lovelydinosaur merged 5 commits into
encode:masterfrom
adriangb:fix-wsgi
Nov 29, 2022
Merged

lovelydinosaur merged 5 commits into
encode:masterfrom
adriangb:fix-wsgi

Conversation

@adriangb

@adriangb adriangb commented Nov 29, 2022 •

Copy link
Copy Markdown
Contributor
  • start_response() must return a write(bytes) function, even though this is now deprecated. It's fine to be a no-op here.
  • sys.exc_info() can return (None, None, None), so make sure to handle that case.

pyright reports:
after.txt
before.txt

- start_response() must return a write(bytes) function, even though this
  is now deprecated. It's fine to be a no-op here.
- sys.exc_info() can return (None, None, None), so make sure to handle that case.
@adriangb
adriangb requested a review from a team November 29, 2022 06:46
@adriangb adriangb mentioned this pull request Nov 29, 2022
@adriangb

Copy link
Copy Markdown
Contributor Author

@tomchristie this one brings type completeness up to 98% 🥳

Comment thread httpx/_transports/wsgi.py Outdated
import types
import typing

import typing_extensions as te

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can't see where this is coming from in the requirements.txt... is this built-in or a package?

Also, can we just import typing_extensions rather than import typing_extensions as te?

@adriangb adriangb Nov 29, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I changed the Protocol to Callable and removed the import. The protocol is more correct, it seems like exc_info must have a default value which you can't describe with Callable. That said, I don't know if this will cause real world issues with WSGI frameworks or not, I think it would depend on how strict their typing is, I'm going to check and report back.

@lovelydinosaur lovelydinosaur left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sure. Thanks.

@lovelydinosaur
lovelydinosaur merged commit 1ff67ea into encode:master Nov 29, 2022
@adriangb

Copy link
Copy Markdown
Contributor Author

Ups looks like #2467 (comment) was too late. I'll still check and we can always follow up in a PR adding the dependency if needed.

@adriangb
adriangb deleted the fix-wsgi branch November 29, 2022 17:58
@adriangb

Copy link
Copy Markdown
Contributor Author

I tested Flask and Falcon by creating an app and passing it to WSGITransport:

from httpx import WSGITransport
from flask import Flask
from falcon import App
WSGITransport(app=Flask(__name__))
WSGITransport(app=App())

No errors from mypy, even in --strict mode. So I think the Callable will be just fine 😄

Comment thread httpx/_transports/wsgi.py
Comment on lines +11 to +25
_T = typing.TypeVar("_T")
_ExcInfo = typing.Tuple[typing.Type[BaseException], BaseException, types.TracebackType]
_OptExcInfo = typing.Union[_ExcInfo, typing.Tuple[None, None, None]]

def _skip_leading_empty_chunks(body: typing.Iterable[bytes]) -> typing.Iterable[bytes]:

# backported wsgiref.types definitions from Python 3.11
StartResponse = typing.Callable[
[str, typing.List[typing.Tuple[str, str]], typing.Optional[_OptExcInfo]],
typing.Callable[[bytes], object],
]


WSGIApplication = typing.Callable[
[typing.Dict[str, typing.Any], StartResponse], typing.Iterable[bytes]
]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm shouldn't we just import these directly from typeshed instead?
E.g.

if typing.TYPE_CHECKING:
    from _typeshed import OptExcInfo
    from _typeshed.wsgi import WSGIApplication

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Probably. With an else: WSGIApplication = Any or something. Maybe make a PR?

samclearman pushed a commit to titanmsp/httpx that referenced this pull request Apr 26, 2025
…ncode#2467)

* Typing: use wsgiref.types to validate types and fix issues uncovered

- start_response() must return a write(bytes) function, even though this
  is now deprecated. It's fine to be a no-op here.
- sys.exc_info() can return (None, None, None), so make sure to handle that case.

* remove typing_extensions

Co-authored-by: Martijn Pieters <mj@zopatista.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants