TypeError: _deserialize () получил неожиданный ключевой аргумент «частичный» в зефире - PullRequest
0 голосов
/ 18 марта 2020

Я пытаюсь загрузить API изображения. Я получаю следующую ошибку

127.0.0.1  "POST //upload/image HTTP/1.1" 500 -
Traceback (most recent call last):
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask_restful\__init__.py", line 272, in error_router
    return original_handler(e)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\_compat.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask_restful\__init__.py", line 272, in error_router
    return original_handler(e)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\_compat.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask_restful\__init__.py", line 468, in wrapper
    resp = resource(*args, **kwargs)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\views.py", line 89, in view
    return self.dispatch_request(*args, **kwargs)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask_restful\__init__.py", line 583, in dispatch_request
    resp = meth(*args, **kwargs)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask_jwt_extended\view_decorators.py", line 108, in wrapper
    return fn(*args, **kwargs)
  File "D:\c_code\projects\python_api\resources\image.py", line 24, in post
    data = image_schema.load(request.files)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\marshmallow\schema.py", line 723, in load
    data, many=many,  unknown=unknown, postprocess=True
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\marshmallow\schema.py", line 861, in _do_load
    unknown=unknown,
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\marshmallow\schema.py", line 669, in _deserialize
    index=index,
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\marshmallow\schema.py", line 493, in _call_and_store
    value = getter_func(data)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\marshmallow\schema.py", line 662, in <lambda>
    val, field_name, data, **d_kwargs
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\marshmallow\fields.py", line 342, in deserialize
    output = self._deserialize(value, attr, data, **kwargs)
TypeError: _deserialize() got an unexpected keyword argument 'partial'

Код File "D:\c_code\projects\python_api\resources\image.py", line 24, in post следующий

from marshmallow import Schema, fields
from werkzeug.datastructures import FileStorage

from marshmallow import Schema, fields
from werkzeug.datastructures import FileStorage


class FileStorageField(fields.Field):
    default_error_messages = {
        "invalid": "Not a valid image."
    }

    def _deserialize(self, value, attr, data) -> FileStorage:
        if value is None:
            return None

        if not isinstance(value, FileStorage):
            self.fail("invalid")

        return value


class ImageSchema(Schema):
    image = FileStorageField(required=True)

Код работает нормально, когда вызовите загрузку изображения API, это вызывает ошибку. Я использую flask зефир, Flask загрузки, пакеты.

1 Ответ

1 голос
/ 21 марта 2020

Поскольку зефир 3, _deserialize может получить partial kwarg.

В каждом настраиваемом поле должна быть скопирована Field._deserialize подпись и приняты неизвестные kwargs.

    def _deserialize(
        self,
        value: typing.Any,
        attr: typing.Optional[str],
        data: typing.Optional[typing.Mapping[str, typing.Any]],
        **kwargs
    ):

Измените свой код следующим образом это принять kwargs молча:

    def _deserialize(self, value, attr, data, **kwargs) -> FileStorage:
        if value is None:
            return None
...