~shabbyrobe/wsgitypes

5bdc2cfd918b79c51473bf383e2d09a91ee4b38c — Blake Williams 1 year, 1 month ago 9eb430b master
Use markdown for readme
2 files changed, 37 insertions(+), 37 deletions(-)

R README.rst => README.md
M setup.py
R README.rst => README.md +35 -35
@@ 14,50 14,50 @@ https://github.com/python/mypy/issues/7654

Define a callable application as a class:

.. code-block:: py
```py
import wsgitypes

    import wsgitypes
    
    class MyApplication(wsgitypes.Application):
        def __call__(
            self, 
            environ: wsgitypes.Environ,
            start_response: wsgitypes.StartResponse,
        ) -> wsgitypes.Response:
            my_header = environ.get("REQUEST_METHOD", "")
            return []
class MyApplication(wsgitypes.Application):
    def __call__(
        self, 
        environ: wsgitypes.Environ,
        start_response: wsgitypes.StartResponse,
    ) -> wsgitypes.Response:
        my_header = environ.get("REQUEST_METHOD", "")
        return []
```

Environ should be type-safe:

.. code-block:: py

    class MyApplication(wsgitypes.Application):
        def __call__(
            self,
            environ: wsgitypes.Environ,
            start_response: wsgitypes.StartResponse,
        ) -> wsgitypes.Response:
            environ["wsgi.input"] # Good
            environ["wsgi.unpot"] # BORK! MyPy will catch this.
            return []
```py
class MyApplication(wsgitypes.Application):
    def __call__(
        self,
        environ: wsgitypes.Environ,
        start_response: wsgitypes.StartResponse,
    ) -> wsgitypes.Response:
        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:

.. code-block:: py
```py
class MyEnviron(wsgitypes.Environ):
    HTTP_X_MY_HEADER: t.Optional[str]

    class MyEnviron(wsgitypes.Environ):
        HTTP_X_MY_HEADER: t.Optional[str]
    
    class MyApplication(wsgitypes.Application):
        def __call__(
            self,
            environ: MyEnviron,
            start_response: wsgitypes.StartResponse,
        ) -> wsgitypes.Response:
            environ = typing.cast(MyEnviron, environ)
            environ.get("HTTP_X_MY_HEADER") # Good
            return []
class MyApplication(wsgitypes.Application):
    def __call__(
        self,
        environ: MyEnviron,
        start_response: wsgitypes.StartResponse,
    ) -> wsgitypes.Response:
        environ = typing.cast(MyEnviron, environ)
        environ.get("HTTP_X_MY_HEADER") # Good
        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

M setup.py => setup.py +2 -2
@@ 7,7 7,7 @@ from setuptools import find_packages
__version__ = "0.0.4"


with open('README.rst', 'r') as f:
with open('README.md', 'r') as f:
    readme = f.read()




@@ 21,7 21,7 @@ setup(
    version=__version__,
    description='MyPy types for WSGI',
    long_description=readme,
    long_description_content_type='text/x-rst',
    long_description_content_type='text/markdown',
    license='MIT',
    url="https://github.com/shabbyrobe/wsgitypes",
    author="Blake Williams",