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

Basic support for TypedDicts (PEP 589) #134

Merged
merged 1 commit into from Sep 24, 2019

Conversation

ilai-deutel
Copy link
Contributor

#129 added support for type hinting-based docs. This adds basic support for TypedDicts, which were not supported because of their type.

Example:

from sanic import Sanic
from sanic_openapi import doc, swagger_blueprint
from typing_extensions import TypedDict

app = Sanic()
app.blueprint(swagger_blueprint)

class Car(TypedDict):
    make: str
    model: str

@app.get("/car/<id:int>")
@doc.summary("Gets a car")
@doc.produces(Car)
async def get_car(request, car_id):
    car: Car = {"make": "Nissan", "model": "370Z"}
    # Alternative syntax: car = Car(make="Nissan", model="370Z")
    return json(car)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

TypedDict inheritance works, as well as Nested TypedDicts.

This PR provides basic support for TypedDict. Full support should probably analyze the totality of the TypedDict and set required to True or False accordingly.

@codecov-io
Copy link

codecov-io commented Sep 2, 2019

Codecov Report

Merging #134 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #134   +/-   ##
=======================================
  Coverage   95.56%   95.56%           
=======================================
  Files           4        4           
  Lines         451      451           
  Branches       95       95           
=======================================
  Hits          431      431           
  Misses          6        6           
  Partials       14       14
Impacted Files Coverage Δ
sanic_openapi/doc.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ee640f2...9cbd2ba. Read the comment docs.

@viniciusd
Copy link
Contributor

viniciusd commented Sep 2, 2019

Neat :)

I am all in on adding that to #128, making that issue a tracking issue for the complete type hinting support

Copy link
Member

@chenjr0719 chenjr0719 left a comment

Choose a reason for hiding this comment

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

LGTM

@chenjr0719 chenjr0719 merged commit 360a033 into sanic-org:master Sep 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants