PyCQA / flake8-import-order

Flake8 plugin that checks import order against various Python Style Guides
GNU Lesser General Public License v3.0
278 stars 72 forks source link

Make styles able to be extended by users #103

Closed dahlia closed 7 years ago

dahlia commented 7 years ago

This patch replaces hardcoded style names with setuptools' entry points.

Entry points are a simple way for distributions to “advertise” Python objects (such as functions or classes) for use by other distributions. Extensible applications and frameworks can search for entry points with a particular name or group, either from a specific distribution or from all active distributions on sys.path, and then inspect or load the advertised objects at will.

The group name of extensible entry points is flake8_import_order.styles (which follows the convention). Built-in styles also become discovered using entry points so that style names don't have to be hardcoded.

Also wrote some docs about extending styles into README.rst, but I'm unsure that my English is fine. If anyone reviews my code and docs I would adjust the patch.

Thanks for the very useful program! :+1:

dahlia commented 7 years ago

The build was broken due to setuppy, but it seems the same to the latest master build. And it's not reproduced in my local environment…

setuppy create: /home/travis/build/PyCQA/flake8-import-order/.tox/setuppy
setuppy installdeps: docutils, Pygments
setuppy inst: /home/travis/build/PyCQA/flake8-import-order/.tox/dist/flake8-import-order-0.11.zip
setuppy installed: appdirs==1.4.0,docutils==0.13.1,flake8-import-order==0.11,packaging==16.8,pycodestyle==2.2.0,Pygments==2.2.0,pyparsing==2.1.10,six==1.10.0
setuppy runtests: PYTHONHASHSEED='2730747044'
setuppy runtests: commands[0] | python setup.py check --metadata --restructuredtext --strict
running check
warning: check: Could not finish the parsing.
error: Please correct your package.
ERROR: InvocationError: '/home/travis/build/PyCQA/flake8-import-order/.tox/setuppy/bin/python setup.py check --metadata --restructuredtext --strict'
dahlia commented 7 years ago

@sigmavirus24 I made it to load a style entry point before ImportOrderChecker is instantiated. Made available style names ordered as well.

dahlia commented 7 years ago

What things would there be to change more?

sigmavirus24 commented 7 years ago

@dahlia I don't merge on this repository. That's @pgjones call. I'll dismiss my review since GitHub's review system is so broken. I would suggest looking at the Travis-CI failures though.

pgjones commented 7 years ago

Do you have a use case in mind for this? I'm not sure what the potential uptake is, and how to weigh it against the extra complexity. (I can't take a detailed look until next week, sorry).

dahlia commented 7 years ago

My motivation was to implement some private rules of my work without changing flake8-import-order's code or maintaining its fork. I believe there would be other companies than my work which have their own modified rules of PEP 8 for some rationale.

sigmavirus24 commented 7 years ago

Adding onto the use-case discussion:

This would mean that new styles need not be added directly to this plugin. People can write other plugins that they distribute separately and which can be installed. Someone could then pip install this plugin and say flake8-import-order-twisted or flake8-import-order-django.

dahlia commented 7 years ago

Failure of tox -e setuppy build was due to a bug of distutils.command.check, and it was fixed at CPython 2.7.10. Since Travis CI provides CPython 2.7.9, I changed tox -e setuppy to use Python 3.5 instead to workaround CPython 2.7.9's bug.

pgjones commented 7 years ago

Ok, I'm convinced and this looks good to me. I'll merge and make a few tweaks before releasing.

dahlia commented 7 years ago

Thanks for merging! 🙏