tiangolo / full-stack

Full stack, modern web application generator. Using Flask, PostgreSQL DB, Docker, Swagger, automatic HTTPS and more.
MIT License
523 stars 81 forks source link

apispec marshmellow error when attempting to run app via manage.py #17

Closed AYEG closed 5 years ago

AYEG commented 5 years ago

Hi! I'm attempting to run the flask app with flask manager. I've simplified it abit from my last project, and it seems to be working.

Using it like this: run command $ python test-app/backend/app/manage.py runserver manage.py:

from flask_script import Manager
manager = Manager(app)
if __name__ == '__main__':
    manager.run() 

It's been helpful already in quickly correcting a few mistakes I made instead of rebuilding the docker images. The problem I can't seem to resolve right now is, I'm running into this strange apispec error:

File "/Users/ayeg/miniconda3/envs/test-project/test-app/backend/app/app/api/api_v1/endpoints/user.py", line 75, in <module>
    def route_users_post(email=None, password=None, first_name=None, last_name=None): 
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/extension.py", line 119, in register
    resource_class_args, resource_class_kwargs)
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/extension.py", line 66, in _defer
    bound()
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/extension.py", line 134, in _register
    paths = self.view_converter.convert(target, endpoint, blueprint)
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/apidoc.py", line 37, in convert
    return [self.get_path(rule, target, **kwargs) for rule in rules]
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/apidoc.py", line 37, in <listcomp>
    return [self.get_path(rule, target, **kwargs) for rule in rules]
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/apidoc.py", line 47, in get_path
    for method, view in six.iteritems(operations)
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/apidoc.py", line 48, in <dictcomp>
    if method.lower() in (set(VALID_METHODS) - {'head'})
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/apidoc.py", line 60, in get_operation
    'parameters': self.get_parameters(rule, view, docs, parent),
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/apidoc.py", line 90, in get_parameters
    extra_params = converter(schema, **options) if args else []
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/apispec/ext/marshmallow/openapi.py", line 441,in fields2parameters
    prop = self.fields2jsonschema(fields, use_refs=use_refs, dump=False)
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/apispec/ext/marshmallow/openapi.py", line 624,in fields2jsonschema
    if field_name in exclude or (field_obj.dump_only and not dump):
AttributeError: 'str' object has no attribute 'dump_only'
(test-project) Bleep-2:test-project ayeg$ python -c "import marshmallow; print(marshmallow.__version__)"
2.16.3
(test-project) Bleep-2:test-project ayeg$ python test-app/backend/app/manage.py runserver
/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py:794: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it toTrue or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
None
Traceback (most recent call last):
  File "test-app/backend/app/manage.py", line 6, in <module>
    from app.main import app
  File "/Users/ayeg/miniconda3/envs/test-project/test-app/backend/app/app/main.py", line 8, in <module>
    from .core import app_setup  # noqa
  File "/Users/ayeg/miniconda3/envs/test-project/test-app/backend/app/app/core/app_setup.py", line 21, in <module>
    from ..api.api_v1 import api as api_v1  # noqa
  File "/Users/ayeg/miniconda3/envs/test-project/test-app/backend/app/app/api/api_v1/api.py", line 12, in <module>
    from .endpoints import user
  File "/Users/ayeg/miniconda3/envs/test-project/test-app/backend/app/app/api/api_v1/endpoints/user.py", line 75, in <module>
    def route_users_post(email=None, password=None, first_name=None, last_name=None, avatar_colors=None): # noqa
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/extension.py", line 119, in register
    resource_class_args, resource_class_kwargs)
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/extension.py", line 66, in _defer
    bound()
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/extension.py", line 134, in _register
    paths = self.view_converter.convert(target, endpoint, blueprint)
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/apidoc.py", line 37, in convert
    return [self.get_path(rule, target, **kwargs) for rule in rules]
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/apidoc.py", line 37, in <listcomp>
    return [self.get_path(rule, target, **kwargs) for rule in rules]
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/apidoc.py", line 47, in get_path
    for method, view in six.iteritems(operations)
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/apidoc.py", line 48, in <dictcomp>
    if method.lower() in (set(VALID_METHODS) - {'head'})
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/apidoc.py", line 60, in get_operation
    'parameters': self.get_parameters(rule, view, docs, parent),
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/flask_apispec/apidoc.py", line 90, in get_parameters
    extra_params = converter(schema, **options) if args else []
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/apispec/ext/marshmallow/openapi.py", line 441,in fields2parameters
    prop = self.fields2jsonschema(fields, use_refs=use_refs, dump=False)
  File "/Users/ayeg/miniconda3/envs/test-project/lib/python3.6/site-packages/apispec/ext/marshmallow/openapi.py", line 624,in fields2jsonschema
    if field_name in exclude or (field_obj.dump_only and not dump):
AttributeError: 'str' object has no attribute 'dump_only'

I haven't specified a specified an apispec version during install, should I be using an older version? Or could this be related to python 2x vs python 3x?

There are also pylint errors throughout the endpoint files on fields.Str() objects which shows:

"module "webargs.fields" has no 'Str' member [E1101]" .

Update: When importing import marshmallow.fields as fields instead of from webargs import fields the pylint error is not shown anymore, but the marshmallow openapi.py error remains. AttributeError: 'str' object has no attribute 'dump_only'.

Br

AYEG commented 5 years ago

Resolved! This error had nothing to do with apispec or other library versions. I made a mistake declaring a field in use_kwargs. Instead of "avatar_color" : fields.Str(), I had already placed random.choice(avatar_colors) instead of inside the function. In addition I forgot a function argument elsewhere

tiangolo commented 5 years ago

Sorry for the delay! I was busy building FastAPI.

I'm glad you solved your problem.


Just so you have it in mind: Webargs doesn't declare all the fields from Marshmallow, although it does expose them.

So, it would actually work, but Pylint, type checks, completion, etc won't work well. (That's why I designed FastAPI around auto-completion and editor support, learning a lot of the ideas from Marshmallow, Webargs and family).

You can use fields from Marshmallow, but have in mind that only when using fields from Webargs you can use Nested and DelimitedList: https://webargs.readthedocs.io/en/latest/api.html#module-webargs.fields