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

Make styles able to be extended by users #103

Merged
merged 3 commits into from
Feb 6, 2017

Conversation

dahlia
Copy link
Contributor

@dahlia dahlia commented Jan 30, 2017

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! 👍

@dahlia dahlia force-pushed the discover-styles-using-entry-points branch from 3bc1a6f to bc364c0 Compare January 30, 2017 09:46
@dahlia
Copy link
Contributor Author

dahlia commented Jan 30, 2017

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'

tox.ini Outdated
deps = docutils
deps =
docutils
Pygments
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is Pygments here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sigmavirus24 Because I added .. code-block:: python directive to README.rst. Docutils warns if Pygments is not installed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think PyPI supports .. code-block:: python so I'm not sure it should be used in the readme.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sigmavirus24 PyPI supports .. code-block:: directive and it's even colored. 😄 GitHub also supports it and it's colored too.

Copy link
Member

@sigmavirus24 sigmavirus24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So pkg_resources as well as iter_entry_points are expensive. Calling them more times than is necessary seems wasteful. I think what you should do is retrieve all of them once and store away the style entry-point before running any checks.

help=("Style to follow. Available: "
"cryptography, google, smarkets, appnexus, pep8"),
help=("Style to follow. Available: " +
", ".join(cls.list_available_styles())),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be sorted otherwise they'll never be displayed the same way twice.

style = Edited(imports)
else:
try:
style_entry_point = next(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably pass the entry-point through to here on the options object so you don't have to delay style loading until here. This also means you're not loading it for each and every file.

)
for entry_point in entry_points:
yield entry_point.name

@classmethod
def parse_options(cls, options):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be finding the entry-point here and passing it through on the options object.

Adjusted a way to load style entry points, and made
available style names (in --import-order-style help)
sorted.

PyCQA#103 (review)
@dahlia
Copy link
Contributor Author

dahlia commented Jan 30, 2017

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

@dahlia
Copy link
Contributor Author

dahlia commented Feb 1, 2017

What things would there be to change more?

@sigmavirus24
Copy link
Member

@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.

@sigmavirus24 sigmavirus24 dismissed their stale review February 1, 2017 11:44

Feedback was addressed

@pgjones
Copy link
Member

pgjones commented Feb 1, 2017

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
Copy link
Contributor Author

dahlia commented Feb 2, 2017

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
Copy link
Member

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.

https://travis-ci.org/PyCQA/flake8-import-order/builds/196644226

Failure of setuppy build is due to a bug of distutils.command.check [1]
which fixed at CPython 2.7.10 [2].

Since Travis CI provides CPython 2.7.9, I changed setuppy build
to use Python 3.5 instead to workaround CPython 2.7.9's bug.

[1] https://bugs.python.org/issue23063
[2] https://hg.python.org/cpython/rev/38826e21f0db
@dahlia
Copy link
Contributor Author

dahlia commented Feb 2, 2017

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
Copy link
Member

pgjones commented Feb 6, 2017

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

@pgjones pgjones merged commit 6ab9653 into PyCQA:master Feb 6, 2017
pgjones pushed a commit that referenced this pull request Feb 6, 2017
Adjusted a way to load style entry points, and made
available style names (in --import-order-style help)
sorted.

#103 (review)
@dahlia
Copy link
Contributor Author

dahlia commented Feb 6, 2017

Thanks for merging! 🙏

dahlia added a commit to spoqa/flake8-import-order-spoqa that referenced this pull request Jul 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants