Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WPS412: Flask App Factory Pattern Incompatibility #1108

Closed
elpapi42 opened this issue Jan 16, 2020 · 9 comments
Closed

WPS412: Flask App Factory Pattern Incompatibility #1108

elpapi42 opened this issue Jan 16, 2020 · 9 comments
Labels
question Further information is requested

Comments

@elpapi42
Copy link

elpapi42 commented Jan 16, 2020

Rule request

Thesis

Change WPS412 to accept logic inside init.py files.

Reasoning

Flask App Factory Pattern depend on the main package _init.py for define the create_app() function, this is the natural location for this type of logic. Check this example Flask project's init.py

Maybe this can be a problem too for when a subpackage if defined, and is natural to expose its internals in the init.py for avoid boiler plate, for example:

Change

from deepbay.denseblock import DenseBlock

To

from deepbay import DenseBlock

If my reasoning is not good enough, feedback will be appreciated, thanks!

@elpapi42 elpapi42 added the rule request Adding a new rule label Jan 16, 2020
@elpapi42
Copy link
Author

elpapi42 commented Jan 16, 2020

I can mention this Flask Mega-Tutorial as reference and backup for my suggestion.

@sobolevn
Copy link
Member

Why not application/app.py?

@elpapi42
Copy link
Author

elpapi42 commented Jan 16, 2020

@sobolevn that sounds legit, but dont cover the other case, reduce boilerplate when importing libraries

Change

from deepbay.denseblock import DenseBlock

To

from deepbay import DenseBlock

It dont cover a subpackage case, for example, a Flask Blueprint Package, where the "Package" its a representation of the Blueprint, and like so, sounds good to init the Blueprint required stuff in its init.py. Following your solution this can be solved with a blueprint.py in each Blueprint Subpackage, and that is an overkill from my point of view. this also means that for import the created Blueprint we must do:

from application.<blueprint-name>.blueprint import <blueprint-name>_bp

Instead of just

from application.<blueprint-name> import <blueprint-name>_bp

@sobolevn
Copy link
Member

In our rules __init__.py only solves the compatibility issues. That's why it is configured via --i-dont-control-code setting.

And namespaces are always a great idea! We need more of those!

@sobolevn sobolevn added question Further information is requested and removed rule request Adding a new rule labels Jan 16, 2020
@elpapi42
Copy link
Author

Got it @sobolevn. can you give me some insights or recommendations about how i can refactor my code?

Go for the application..bluepirnt.py for init the blueprint related stuff?

In other python applications, should i keep the init.py for expose the subpackage internals more directly to the namespace?

Just looking for a clear path to resolve this, thanks in advance!

@sobolevn
Copy link
Member

Just rename your __init__.py to blueprint.py (or whatever suits you best) and create a new empty __init__.py

Does this solve your problem?

@elpapi42
Copy link
Author

just one last question: What i must do with the imports in the init.py:

  1. get rid of them and let the imports be:
from deepbay.denseblock import DenseBlock
  1. Keep the imports and do:
from deepbay import DenseBlock

In the last case, WPS will still mark unused imports warning.

What approach you recommend to me?

Thanks for your time, sorry for pick it up a bit.

@sobolevn
Copy link
Member

I always keeps imports out of __init__.py - it allows to match files and modules as 1-to-1 entities.
Helpful and readable!

@sobolevn
Copy link
Member

I am going to close this as solved, if you have any other questions - please, feel free to ask!
You can reopen this one or create new issues if you have other questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants