sparckles / Robyn

Robyn is a Super Fast Async Python Web Framework with a Rust runtime.
https://robyn.tech/
BSD 2-Clause "Simplified" License
4.45k stars 229 forks source link

Fix mypy issues in openapi.py with missing dict and use of typedDict #1034

Open dave42w opened 4 days ago

dave42w commented 4 days ago

Description

This PR fixes mypy issues in openapi.py

Summary

This PR fixes the following

TypedDict

all TypedDict replaced with dict the python docs don't include any examples of using TypedDict for a variable, only as a class. mypy 1.13.0 complains about examples like this query_params: Optional[TypedDict], so I have changed them all to be like query_params: Optional[Dict], All tests still pass

MyPy error: Variable "typing.TypedDict" is not valid as a type

This meant changing a few tests from

query_param_annotations = query_params.__annotations__ if query_params is TypedDict else typing.get_type_hints(query_params)

to

query_param_annotations = query_params.__annotations__ if query_params is Dict else typing.get_type_hints(query_params)

While the tests all pass I'm not sure if the logic of typing.get_type_hints still works for Dict vs TypedDict

missing dict type

def get_path_obj variable openapi_path_object

This wasn't given a type which was causing multiple mypy errors:

openapi_path_object = {
...
                openapi_path_object["parameters"].append(
"Sequence[str]" has no attribute "append"
...
            openapi_path_object["requestBody"] = request_body_object
Incompatible types in assignment (expression has type "dict[str, dict[str, dict[str, dict[str, Collection[str]]]]]", target has type "Sequence[str]")
...
        openapi_path_object["responses"] = {"200": {"description": "Successful Response", "content": {response_type: {"schema": response_schema}}}}
Incompatible types in assignment (expression has type "dict[str, dict[str, Collection[str]]]", target has type "Sequence[str]")

all fixed by changing the declaration to:

        openapi_path_object: dict = {

def get_schema_object local variable properties

same as above. Change declaration to

        properties: dict = {

all the similar warnings are gone

my results

These changes mean that mypy 1.13 finds no warnings in openapi.py

PR Checklist

Please ensure that:

Pre-Commit Instructions:

vercel[bot] commented 4 days ago

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Comments Updated (UTC)
robyn βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Nov 20, 2024 10:46pm
codspeed-hq[bot] commented 4 days ago

CodSpeed Performance Report

Merging #1034 will not alter performance

Comparing dave42w:mypy (989a0ef) with main (3f2e79a)

Summary

βœ… 146 untouched benchmarks

sansyrox commented 4 days ago

Hey @dave42w πŸ‘‹

What python version are you using?

dave42w commented 4 days ago

Hey @dave42w πŸ‘‹

What python version are you using?

Python 3.12.7 and mypy 1.13.0

dave42w commented 1 day ago

@sansyrox Are we ok to merge?