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
Metadata
Release files for wsgitypes 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| wsgitypes-0.0.4.tar.gz | 4.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| wsgitypes-0.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.1 kB
Release files / wsgitypes-0.0.4.tar.gz
| Download URL | wsgitypes-0.0.4.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
61d8594bb2ed254b03ae771caba95783c3f8d02911d36dc673e086d013ed2fef
|
|
BLAKE2b-256 checksum How to use checksums |
7748db5208fba0961b1cefa4cb95a656879a486e7a734b4db16122105aa50962
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / wsgitypes-0.0.4-py3-none-any.whl
| Download URL | wsgitypes-0.0.4-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bfe9df03ec37056c5f3f9e8771ef67baf1d8b801792e6b55b1842e1dbb76d15e
|
|
BLAKE2b-256 checksum How to use checksums |
8a691e875743ccd1d7a084020c4fb4449fd38456be5e132cfe7b25c85fb22092
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|