sasha-tkachev/fastapi-cloudevents

Fix typing definition, so that projects using this library can use mypy out of the box

autra opened this issue · 5 comments

autra commented

Is your feature request related to a problem? Please describe.

When using mypy on my client project, and stubgen to generate stub definitions of fastapi-cloudevents, I noticed some typing issues on this library.

Output of mypy
ᐅ mypy fastapi_cloudevents
fastapi_cloudevents/settings.py:14: error: Invalid type comment or annotation  [valid-type]
fastapi_cloudevents/settings.py:14: note: Suggestion: use constr[...] instead of constr(...)
fastapi_cloudevents/cloudevent_request.py:30: error: Missing named argument "default_source" for "CloudEventSettings"  [call-arg]
fastapi_cloudevents/cloudevent_request.py:56: error: Variable "cls" is not valid as a type  [valid-type]
fastapi_cloudevents/cloudevent_request.py:56: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
fastapi_cloudevents/cloudevent_request.py:56: error: Invalid base class "cls"  [misc]
fastapi_cloudevents/cloudevent_response.py:36: error: Incompatible types in assignment (expression has type "Dict[Any, Any]", variable has type "List[Union[bytes, Any]]")  [assignment]
fastapi_cloudevents/cloudevent_response.py:36: error: Argument 1 to "dict" has incompatible type "List[Union[bytes, Any]]"; expected "Iterable[Tuple[Any, Any]]"  [arg-type]
fastapi_cloudevents/cloudevent_response.py:37: error: "List[Union[bytes, Any]]" has no attribute "update"  [attr-defined]
fastapi_cloudevents/cloudevent_response.py:40: error: "List[Union[bytes, Any]]" has no attribute "items"  [attr-defined]
fastapi_cloudevents/cloudevent_response.py:44: error: Missing named argument "default_source" for "CloudEventSettings"  [call-arg]
fastapi_cloudevents/cloudevent_response.py:47: error: Incompatible types in assignment (expression has type "List[Union[bytes, Any]]", base class "Response" defined the type as "List[Tuple[bytes, bytes]]")  [assignment]
fastapi_cloudevents/cloudevent_response.py:67: error: Variable "cls" is not valid as a type  [valid-type]
fastapi_cloudevents/cloudevent_response.py:67: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
fastapi_cloudevents/cloudevent_response.py:67: error: Invalid base class "cls"  [misc]
fastapi_cloudevents/cloudevent_response.py:94: error: Missing named argument "default_source" for "CloudEventSettings"  [call-arg]
fastapi_cloudevents/cloudevent_response.py:100: error: Incompatible default for argument "headers" (default has type "None", argument has type "Dict[Any, Any]")  [assignment]
fastapi_cloudevents/cloudevent_response.py:100: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
fastapi_cloudevents/cloudevent_response.py:100: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
fastapi_cloudevents/cloudevent_response.py:101: error: Incompatible default for argument "media_type" (default has type "None", argument has type "str")  [assignment]
fastapi_cloudevents/cloudevent_response.py:101: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
fastapi_cloudevents/cloudevent_response.py:101: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
fastapi_cloudevents/cloudevent_response.py:102: error: Incompatible default for argument "background" (default has type "None", argument has type "BackgroundTask")  [assignment]
fastapi_cloudevents/cloudevent_response.py:102: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
fastapi_cloudevents/cloudevent_response.py:102: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
fastapi_cloudevents/cloudevent_response.py:113: error: Argument "headers" to "_render_headers" of "BinaryCloudEventResponse" has incompatible type "List[Tuple[bytes, bytes]]"; expected "List[Union[bytes, Any]]"  [arg-type]
fastapi_cloudevents/cloudevent_response.py:113: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
fastapi_cloudevents/cloudevent_response.py:113: note: Consider using "Sequence" instead, which is covariant
fastapi_cloudevents/cloudevent_response.py:117: error: Argument 1 to "from_dict" has incompatible type "Dict[bytes, Any]"; expected "Dict[str, Any]"  [arg-type]
fastapi_cloudevents/cloudevent_response.py:131: error: Argument 1 to "from_dict" has incompatible type "Dict[bytes, Any]"; expected "Dict[str, Any]"  [arg-type]
fastapi_cloudevents/cloudevent_response.py:141: error: Incompatible types in assignment (expression has type "List[Union[bytes, Any]]", variable has type "List[Tuple[bytes, bytes]]")  [assignment]
fastapi_cloudevents/cloudevent_response.py:142: error: Argument 1 to "_update_headers" has incompatible type "List[Tuple[bytes, bytes]]"; expected "List[Union[bytes, Any]]"  [arg-type]
fastapi_cloudevents/cloudevent_response.py:142: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
fastapi_cloudevents/cloudevent_response.py:142: note: Consider using "Sequence" instead, which is covariant
fastapi_cloudevents/cloudevent_response.py:149: error: Variable "cls" is not valid as a type  [valid-type]
fastapi_cloudevents/cloudevent_response.py:149: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
fastapi_cloudevents/cloudevent_response.py:149: error: Invalid base class "cls"  [misc]
fastapi_cloudevents/cloudevent_route.py:19: error: Missing named argument "default_source" for "CloudEventSettings"  [call-arg]
fastapi_cloudevents/cloudevent_route.py:39: error: Variable "cls" is not valid as a type  [valid-type]
fastapi_cloudevents/cloudevent_route.py:39: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
fastapi_cloudevents/cloudevent_route.py:39: error: Invalid base class "cls"  [misc]
fastapi_cloudevents/installation.py:31: error: Missing named argument "default_source" for "CloudEventSettings"  [call-arg]
fastapi_cloudevents/installation.py:34: error: Incompatible types in assignment (expression has type "Type[_CloudEventResponse]", variable has type "Type[Response]")  [assignment]
Found 28 errors in 5 files (checked 8 source files)

Describe the solution you'd like

Fixe the mypy issues (and maybe publish type stubs?) so that projects using mypy can go on their merry (typed) way

Describe alternatives you've considered

Not an alternative, but a workaround: I'm currently fixing the stubs manually.


I consider it as a feature request and not a bug, maybe you don't want to support mypy 🙂

@autra , thank you for submitting this issue!
I will fix it in a few weeks.

Sorry for the late reply.

autra commented

No problem, I've seem bigger delays on open-source project, thanks for caring!

@autra I did not forget you.
I think #11 maybe solves part of your problem?

Currently we are at war and I hope I get the time to fix this issue myself after it.

I gave it a look and it does require a bit of work from me, but nothing too complicated.

Hey @autra,
We kicked their ass so here is a release with your bugfix 😄.

Let me know if you still have issues after the upgrade.

Awesome, thanks! I'll test it as soon as possible.