Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wsgitypes-0.0.3.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

wsgitypes-0.0.3-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file wsgitypes-0.0.3.tar.gz.

File metadata

  • Download URL: wsgitypes-0.0.3.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

Hashes for wsgitypes-0.0.3.tar.gz
Algorithm Hash digest
SHA256 e90a83f3cf4ca1d809b33a904ca4ed42db9dfbb0630b24ec47be3f0dade85cd9
MD5 f78c12d0386a227634f782369e36bea6
BLAKE2b-256 4b391c71457cbb2132c7d3fa553c8bf518cce18e2a8958087face196dbf49512

See more details on using hashes here.

File details

Details for the file wsgitypes-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: wsgitypes-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.0 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

Hashes for wsgitypes-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ab1708ca95a837d9d0105a9ba157c500399a436def43817de148a1c41c334baf
MD5 f16119e42d6f640dab03e9bfea7ea136
BLAKE2b-256 c3a0ea395836528ba6d866dd6c04a4e9e6e5745afc9104d5a843d059c79f3f35

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page