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

gh-86178: Add wsgiref.types #32335

Merged
merged 4 commits into from
Apr 16, 2022
Merged

gh-86178: Add wsgiref.types #32335

merged 4 commits into from
Apr 16, 2022

Conversation

srittau
Copy link
Contributor

@srittau srittau commented Apr 5, 2022

@AlexWaygood
Copy link
Member

My first thought when I saw this was, "How will we ensure that these types remain up to date with the rest of the wsgiref module?" But these types do appear to have been very stable in typeshed. So, perhaps it's not much of an issue.

@AlexWaygood AlexWaygood added the type-feature A feature request or enhancement label Apr 5, 2022
@srittau
Copy link
Contributor Author

srittau commented Apr 5, 2022

These types are just a representation of PEP 3333, not of the specifics of wsgiref. The only improvement I can see at the moment is potentially typing WSGIEnvironment using TypedDict for some of the predefined fields.

Lib/wsgiref/types.py Outdated Show resolved Hide resolved
Lib/wsgiref/types.py Outdated Show resolved Hide resolved
Doc/library/wsgiref.rst Show resolved Hide resolved
:synopsis: WSGI types for static type checking


This module provides various types for static type checking as described
Copy link
Member

Choose a reason for hiding this comment

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

This documentation feels a bit thin. Perhaps we could have examples of WSGI code using some of these aliases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree about the examples, but when I procrastinated over two options, I gave up :)

  1. Add an example to this section. But right below this section there is the "Example" section and this would feel like a duplication.
  2. Add types to the example section. But in a way this distracts from the point of the example.

Lib/wsgiref/types.py Show resolved Hide resolved
@@ -0,0 +1,53 @@
"""WSGI-related types for static type checking"""
Copy link
Member

@AlexWaygood AlexWaygood Apr 6, 2022

Choose a reason for hiding this comment

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

It might be good to add a note to the module docstring that any changes contributors want to make to this file will need to be made to typeshed simultaneously if they're to have any meaning to static type checkers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure we should mention typeshed explicitly for a few reasons:

  • I don't want to burden CPython contributors to need to contribute to typeshed.
  • In typeshed we are pretty good in picking up changes, and for CPython we have quite some time to make these changes.
  • While this is currently not the case, type checkers could in theory use the implementation module to type check instead of typeshed. In fact, I have an idea to optionally direct type checkers to the stdlib in cases like this, so it's possible that this comment would get outdated during the life time of Python 3.11.

@srittau srittau requested a review from JelleZijlstra April 11, 2022 19:24
@JelleZijlstra JelleZijlstra self-assigned this Apr 12, 2022
@srittau srittau closed this Apr 14, 2022
@srittau srittau reopened this Apr 14, 2022
@srittau srittau changed the title bpo-42012: Add wsgiref.types gh-86178: Add wsgiref.types Apr 14, 2022
@JelleZijlstra
Copy link
Member

I'll merge this in a few days unless more feedback comes up.

class ErrorStream(Protocol):
"""WSGI error stream as defined in PEP 3333"""
def flush(self) -> None: ...
def write(self, s: str, /) -> None: ...
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably be -> object. File objects typically return the number of bytes written as int.

Copy link
Member

Choose a reason for hiding this comment

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

Same for flush, although that doesn't typically return anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a bit embarrassing. :) Will fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-typing type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants