sasha-tkachev / fastapi-cloudevents Goto Github PK
View Code? Open in Web Editor NEWCloudEvents FastAPI plugin. Allows to easily consume and produce events in CloudEvents format over REST API.
License: Apache License 2.0
CloudEvents FastAPI plugin. Allows to easily consume and produce events in CloudEvents format over REST API.
License: Apache License 2.0
Hi, this looks great but I am trying to use it and I am struggling.
I get this error:
File "/usr/local/lib/python3.9/site-packages/uvicorn/config.py", line 458, in load
self.loaded_app = import_from_string(self.app)
File "/usr/local/lib/python3.9/site-packages/uvicorn/importer.py", line 21, in import_from_string
module = importlib.import_module(module_str)
File "/usr/local/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/Users/sebgoa/Desktop/localcompose/./main.py", line 7, in <module>
app = install_fastapi_cloudevents(app)
File "/usr/local/lib/python3.9/site-packages/fastapi_cloudevents/installation.py", line 33, in install_fastapi_cloudevents
if app.default_response_class != JSONResponse:
AttributeError: 'FastAPI' object has no attribute 'default_response_class'
I am using fastAPI 0.80.0
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.
แ
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 ๐
Is your feature request related to a problem? Please describe.
Would like to use fastapi-cloudevents
when running against the latest fastapi
which requires pydantic v2
Describe the solution you'd like
Describe alternatives you've considered
NA
Additional context
NA
Describe the bug
When using this package's CloudEvent
type as a request object, event requests fail due to 422 errors:
[ErrorWrapper(exc=ValidationError(model='CloudEvent', errors=[{'loc': ('type',), 'msg': 'field required', 'type': 'value_error.missing'}]), loc=('body',))]
The type
field is not in the request body, but rather, it is present in the Ce-Type
header.
To Reproduce
Run this against real events from eventarc
System Details (please complete the following information):
Is your feature request related to a problem? Please describe.
It is not currently possible to use fastapi-cloudevents in projects which use Pydantic v2. My organisation is currently using the plugin in a new service, but we need the service to support Pydantic v2 to allow us to take advantage of its performance benefits.
We have bumped cloudevents[pydantic] to 1.10.0, which does support Pydantic v2, but currently fastapi-cloudevents version 1.1.0 fails to import with the following error:
../../.pyenv/versions/3.11.5/envs/redacted-service-name/lib/python3.11/site-packages/fastapi_cloudevents/settings.py:4: in <module>
from pydantic import BaseSettings, Field
E pydantic.errors.PydanticImportError: `BaseSettings` has been moved to the `pydantic-settings` package. See https://docs.pydantic.dev/2.4/migration/#basesettings-has-moved-to-pydantic-settings for more details.
Describe the solution you'd like
A version of fastapi-cloudevents which supports Pydantic v2 would be ideal. Alternatively, a version which has Pydantic namespaced to allow it to work with v1 or v2 depending on what the downstream package has available would also solve this.
Describe alternatives you've considered
We planned to submit a PR to namespace Pydantic to allow cross-compatibility, but we found the main branch was already updated to support Pydantic v2, so the PR would probably introduce regressions rather than solve problems.
We would be willing to submit a PR based on the 1.1.0 tag if that would make it easier to solve this short term.
Describe the bug
When I followed the first example, pasting the curl with the binary events I get a response:
{"detail":[{"loc":["body"],"msg":"value is not a valid dict","type":"type_error.dict"}]}
When I tried the second curl command, the one with a structured event it worked.
To Reproduce
Steps to reproduce the behavior:
I installed and followed the instructions as per the readme.
I installed using poetry.
Expected behavior
I expected to see both structured and binary methods work as specified by the documentation.
Screenshots
If applicable, add screenshots to help explain your problem.
System Details (please complete the following information):
name = "cloudevents" version = "1.6.1"
name = "fastapi" version = "0.85.0"
name = "fastapi-cloudevents" version = "1.0.2"
name = "pydantic" version = "1.10.2"
Additional context
Add any other context about the problem here.
Hi @sasha-tkachev,
I hope you and your loved ones are doing well in these cruel times.
I encountered an issue with one of the files of the last 2.0.0 release, which is different than the source file in the repository.
Describe the bug
The file cloudevent_response.py
in the release tarball fastapi-cloudevents-2.0.0.tar.gz differs from the source file in the repo fastapi_cloudevents/cloudevent_response.py and the source tarball v2.0.0.tar.gz
Concretely the diff looks like this:
@@ -63,7 +63,7 @@
)
@classmethod
- def configured(cls, settings: CloudEventSettings) -> Type["_CloudEventResponse"]:
+ def configured(cls, settings: CloudEventSettings) -> Type["_CloudEventResponse:q:"]:
class ConfiguredStructuredCloudEventResponse(cls):
_settings = settings
To Reproduce
Prepare a clean test environment:
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install fastapi_cloudevents==2.0.0
Try to import the fastapi_cloudevents
python3 -c "import fastapi_cloudevents"
and see the error message:
Traceback (most recent call last):
File "/usr/local/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 850, in __init__
code = compile(arg_to_compile, '<string>', 'eval')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<string>", line 1
_CloudEventResponse:q:
^
SyntaxError: invalid syntax
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/sebbae/bugreport/.venv/lib/python3.11/site-packages/fastapi_cloudevents/__init__.py", line 3, in <module>
from fastapi_cloudevents.cloudevent_response import (
File "/Users/sebbae/bugreport/.venv/lib/python3.11/site-packages/fastapi_cloudevents/cloudevent_response.py", line 43, in <module>
class StructuredCloudEventResponse(JSONResponse, _CloudEventResponse):
File "/Users/sebbae/bugreport/.venv/lib/python3.11/site-packages/fastapi_cloudevents/cloudevent_response.py", line 66, in StructuredCloudEventResponse
def configured(cls, settings: CloudEventSettings) -> Type["_CloudEventResponse:q:"]:
~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 352, in inner
return cached(*args, **kwds)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 1561, in __getitem__
params = tuple(_type_check(p, msg) for p in params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 1561, in <genexpr>
params = tuple(_type_check(p, msg) for p in params)
^^^^^^^^^^^^^^^^^^^
File "/usr/local/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 186, in _type_check
arg = _type_convert(arg, module=module, allow_special_forms=allow_special_forms)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 164, in _type_convert
return ForwardRef(arg, module=module, is_class=allow_special_forms)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 852, in __init__
raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
SyntaxError: Forward reference must be an expression -- got '_CloudEventResponse:q:'
Expected behavior
Having the same files in the released package as in the tagged GH release and no error when importing the package.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.