Skip to content

typing improvements - #2435

Closed
mjpieters wants to merge 5 commits into
encode:masterfrom
mjpieters:typing_improvements
Closed

mjpieters wants to merge 5 commits into
encode:masterfrom
mjpieters:typing_improvements

Conversation

@mjpieters

@mjpieters mjpieters commented Nov 9, 2022 •

Copy link
Copy Markdown
Contributor

This is a series of patches to improve the quality of the type hints provided
by httpx. These type hint updates were driven by our use of httpx in a project
with pyright configured in strict mode, which sets a high bar for type hint
completeness in 3rd party libraries such as httpx.

In the process, I found that the WSGITransport implementation had small issues
and fixed those in situ.

Without this patch, the output of pyright --verifytypes httpx produces:

Symbols exported by "httpx": 67
  With known type: 18
  With ambiguous type: 1
  With unknown type: 48
  Functions without docstring: 2
  Functions without default param: 129
  Classes without docstring: 0

Other symbols referenced but not exported by "httpx": 1061
  With known type: 941
  With ambiguous type: 11
  With unknown type: 109

Type completeness score: 26.9%

With these patches, the output will became:

Symbols exported by "httpx": 67
  With known type: 67
  With ambiguous type: 0
  With unknown type: 0
  Functions without docstring: 2
  Functions without default param: 130
  Classes without docstring: 0

Other symbols referenced but not exported by "httpx": 1060
  With known type: 1060
  With ambiguous type: 0
  With unknown type: 0

Type completeness score: 100%

Included commits:

  • Remove typeshed iscoroutine workaround
  • Typing: use wsgiref.types to validate types and fix issues uncovered
  • Typing: always fill in generic type parameters
  • Typing: add explicit types for public fields

@mjpieters

This comment was marked as outdated.

@Kludex Kludex added the do not merge PRs that should not be merged label Nov 10, 2022
This was resolved in typeshed and newer mypy versions have inherited the fix.
@mjpieters
mjpieters force-pushed the typing_improvements branch 2 times, most recently from 0c8860b to 4012639 Compare November 10, 2022 09:28
- 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.
Being explicit about the parameters helps find bugs and makes the library
easier to use for users.

- Tell mypy to disallow generics without parameter values
- Give all generic types parameters values
Without these, type inference would be required, the output of which can
differ between type checkers. Explict fields also serves as documentation
and can be a place to add explicit field descriptions in future.
@lovelydinosaur lovelydinosaur mentioned this pull request Nov 11, 2022
15 tasks done
@lovelydinosaur

Copy link
Copy Markdown
Member

Thanks!

Yup, I'm on board with us working towards stricter typing if that helps our users.
There's a bit of a mix of different changes in here, some of which would easily pass review, others which I'm less clear on.

I've outlined an incremental approach that I think would work well in #2436.
Would you be up for taking a more step-by-step approach along those lines?

@mjpieters

mjpieters commented Nov 11, 2022 •

Copy link
Copy Markdown
Contributor Author

I actually tried to break this PR up into different aspects; if you view each commit as a separate PR, would that work better?

(If you are familiar with stacked diffs, then that's the approach I've been following here).

If you rather have these as actual separate PRs I'm happy to rework these.

@lovelydinosaur

Copy link
Copy Markdown
Member

If you rather have these as actual separate PRs I'm happy to rework these.

I would prefer separate pull requests, yes.
I'd also prefer we start with tightening up the mypy strict settings before we look at pyright.
Your comments on #2436 look like the right direction to me - I'd suggest starting with a PR targeting just one of those mypy flags, and we continue from there, iteratively.
Seem okay to you?

@adriangb

adriangb commented Nov 29, 2022 •

Copy link
Copy Markdown
Contributor

@mjpieters thank you for the super valuable contribution and the very clean commit history. I went ahead and split up your PR into one PR per commit (#2468 , #2467 , #2466 and #2469) so that it can be reviewed that way as well. I preserved your authorship of the commits.

I also opened #2470 and #2472 to add those mypy flags. The only code changes were removing a couple of # type: ignores; I suggest we just merge them all in 1 PR.

@adriangb

Copy link
Copy Markdown
Contributor

We've now merged all of these except #2496. I think we can close this issue. Thank you so much for for working on this @mjpieters!

@mjpieters

Copy link
Copy Markdown
Contributor Author

We've now merged all of these except #2496. I think we can close this issue. Thank you so much for for working on this @mjpieters!

Excellent, I've been trying to find some time to do the same, so this is very helpful, thanks!

@mjpieters
mjpieters deleted the typing_improvements branch November 29, 2022 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not merge PRs that should not be merged

Development

Successfully merging this pull request may close these issues.

4 participants