MyPy types for WSGI
Project description
This is an attempt to bring some type safety to WSGI applications using Python’s new typing features (TypedDicts, Protocols). It seems to work OK but it’ll be full of gaps, holes, bugs, missteps, etc. I would not recommend depending on it.
This is implemented as a Python module, rather than MyPy stubs, as it represents a protocol things can satisfy rather than a set of types for something concrete.
This package came together during an exploration documented here: https://github.com/python/mypy/issues/7654
Define a callable application as a class:
class MyApplication(wsgitypes.Application[MyEnviron]):
def __call__(
self,
environ: Environ,
start_response: wsgitypes.StartResponse,
) -> wsgitypes.ResponseBody:
my_header = environ.get("REQUEST_METHOD", "")
return []
Environ should be type-safe:
class MyApplication(wsgitypes.Application):
def __call__(self, environ: Environ, start_response: wsgitypes.StartResponse) -> wsgitypes.ResponseBody:
environ["wsgi.input"] # Good
environ["wsgi.unpot"] # BORK! MyPy will catch this.
return []
You can define your own extensions to Environ using TypedDict inheritance, like so:
class MyEnviron(wsgitypes.Environ):
HTTP_X_MY_HEADER: t.Optional[str]
class MyApplication(wsgitypes.Application):
def __call__(self, environ: wsgitypes.Environ, start_response: wsgitypes.StartResponse) -> wsgitypes.Response:
environ = typing.cast(MyEnviron, environ)
environ.get("HTTP_X_MY_HEADER")
return []
Note that you need to use typing.cast to convert the incoming environ to your derived version. An attempt was made to use a type param for Environ, but it wasn’t viable: https://github.com/python/mypy/issues/7654
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file wsgitypes-0.0.4.tar.gz
.
File metadata
- Download URL: wsgitypes-0.0.4.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.8.0 tqdm/4.33.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61d8594bb2ed254b03ae771caba95783c3f8d02911d36dc673e086d013ed2fef |
|
MD5 | 789e8fb0592b212426f0f3e185afff0f |
|
BLAKE2b-256 | 7748db5208fba0961b1cefa4cb95a656879a486e7a734b4db16122105aa50962 |
File details
Details for the file wsgitypes-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: wsgitypes-0.0.4-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.8.0 tqdm/4.33.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bfe9df03ec37056c5f3f9e8771ef67baf1d8b801792e6b55b1842e1dbb76d15e |
|
MD5 | e6114e7b169207cef21ce9fff81914b9 |
|
BLAKE2b-256 | 8a691e875743ccd1d7a084020c4fb4449fd38456be5e132cfe7b25c85fb22092 |