Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Add isort third party detection from requirements.txt #629

Closed
wants to merge 1 commit into from

Conversation

guewen
Copy link
Member

@guewen guewen commented Jan 27, 2020

The current setup used in the OCA for isort is done by using 2
pre-commit hooks:

  1. seed-isort-config: find third-party libs and updates the
    '.isort.cfg' file's "known_third_party" option with the list
  2. isort: runs isort, which will use the list of third party
    libs provided by 'seed-isort-config' in the "known_third_party" option.

This is an issue [0] with the workflow adopted by many contributors of
the OCA: as the pull requests may need some time to be merged, we use
temporary branches where we aggregate the various PRs.

If several pull requests add a third party library, we'll have a
conflict, and when the process of merging these pull requests is
automated (e.g. with git-aggregator [1]), it becomes tedious.

The list of libs in the "known_third_party" variable is not even
editable manually, as "seed-isort-config" overwrite the value on every
commit: it doesn't really make sense to actually store this.

As pointed out by @sbidoul [2], isort mentions in their changelog:

Support for using requirements files to auto determine third-paty
section if pipreqs & requirementslib are installed.

Which is the goal of this change, details:

  • Remove 'seed-isort-config' from pre-commit as the list of libs will be
    provided by requirements.txt
  • Replace the isort pre-commit repo by the official repo (the mirror says
    it is been deprecated in favor of the official repo)
  • Adds pipreqs and pip-api in additional dependencies to activate
    isort's feature to read requirements.txt [3]
  • Adds types: [python] in the pre-commit config: the mirror had it and
    the official repo doesn't. Without it, some files such as .pot are
    modified (different EOF). (it can be removed after the next isort
    release)
  • Add an union merge driver on 'requirements.txt' to resolve conflicts
    on this file (on conflicts, both lines are kept, which works in most
    cases on this file, in the rare case it's an issue because we have
    2 requirements for different versions, we'll have to fix manually)

Note: it is now important to have the Python libraries declared in
"requirements.txt" to have them ordered in the proper place.

Alternatively, we could have tried 'reorder-python-imports' in place of 'isort' [4].

This setup has been tested successfully on OCA/stock-logistics-warehouse#828

[0] #625
[1] https://github.com/acsone/git-aggregator
[2] #625 (comment)
[3] https://github.com/timothycrosley/isort/blob/500bafabbd51a6005c11a00c4738a2438990e48a/pyproject.toml#L42
[4] https://github.com/asottile/reorder_python_imports

Closes #625

@guewen guewen force-pushed the pre-commit-isort-from-reqs branch from 79df308 to 3f4d743 Compare January 27, 2020 08:20
@guewen
Copy link
Member Author

guewen commented Jan 27, 2020

@yajo thanks for your review, I pushed a fixup with the updates.

The current setup used in the OCA for isort is done by using 2
pre-commit hooks:

1. seed-isort-config: find third-party libs and updates the
'.isort.cfg' file's "known_third_party" option with the list
2. isort: runs isort, which will use the list of third party
libs provided by 'seed-isort-config' in the "known_third_party" option.

This is an issue [0] with the workflow adopted by many contributors of
the OCA: as the pull requests may need some time to be merged, we use
temporary branches where we aggregate the various PRs.

If several pull requests add a third party library, we'll have a
conflict, and when the process of merging these pull requests is
automated (e.g. with git-aggregator [1]), it becomes tedious.

The list of libs in the "known_third_party" variable is not even
editable manually, as "seed-isort-config" overwrite the value on every
commit: it doesn't really make sense to actually store this.

As pointed out by @sbidoul [2], isort mentions in their changelog:

> Support for using requirements files to auto determine third-paty
> section if pipreqs & requirementslib are installed.

Which is the goal of this change, details:

* Remove 'seed-isort-config' from pre-commit as the list of libs will be
  provided by requirements.txt
* Replace the isort pre-commit repo by the official repo (the mirror says
  it is been deprecated in favor of the official repo)
* Adds pipreqs and pip-api in additional dependencies to activate
  isort's feature to read requirements.txt [3]
* Adds types: [python] in the pre-commit config: the mirror had it and
  the official repo doesn't. Without it, some files such as .pot are
  modified (different EOF). (it can be removed after the next isort
  release)
* Add an union merge driver on 'requirements.txt' to resolve conflicts
  on this file (on conflicts, both lines are kept, which works in most
  cases on this file, in the rare case it's an issue because we have
  2 requirements for different versions, we'll have to fix manually)

Note: it is now important to have the Python libraries declared in
"requirements.txt" to have them ordered in the proper place. At some
point, this file could be automatically generated: [4].

Alternatively, we could have tried 'reorder-python-imports' in place of 'isort' [5].

This setup has been tested successfully on OCA/stock-logistics-warehouse#828

[0] OCA#625
[1] https://github.com/acsone/git-aggregator
[2] OCA#625 (comment)
[3] https://github.com/timothycrosley/isort/blob/500bafabbd51a6005c11a00c4738a2438990e48a/pyproject.toml#L42
[4] OCA/oca-github-bot#98
[5] https://github.com/asottile/reorder_python_imports

Closes OCA#625
@guewen guewen force-pushed the pre-commit-isort-from-reqs branch from dcad340 to 7c98c8b Compare January 31, 2020 06:17
@guewen
Copy link
Member Author

guewen commented Jan 31, 2020

Squashed

Copy link
Contributor

@moylop260 moylop260 left a comment

Choose a reason for hiding this comment

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

👍

@sbidoul
Copy link
Member

sbidoul commented Feb 8, 2020

@guewen One issue I detected with this is that the usual Odoo dependencies (e.g. lxml, dateutil) are usually not declared in requirements.txt and are now moved to the local imports group by isort.

Ah well... nothing is simple.

@yajo
Copy link
Member

yajo commented Feb 10, 2020

Do you have an example?

@sbidoul
Copy link
Member

sbidoul commented Feb 16, 2020

For example, any addon that imports dateutil will have it misplaced by isort, if known_thirparty does not declare dateutil.

Putting dateutil in the manifest external deps does not help, because what lands in requirements.txt is python-dateutil (which is correct) and not dateutil, and isort therefore does not detect it as third party.

One way to mitigate this is to have the list of common dependencies in known_thirdparty (it could be the list that is also known by pylint-odoo).

That would not automate it fully, as when a third party package has an import name different than it's distribution name, it would need to be added manually to known_thirdparty.

I'm experimenting with that in OCA/mis-builder#260.

BTW, I also wrote a small tool to generate requirements.txt from the manifests in case that ends up being useful: acsone/setuptools-odoo#42

@yajo
Copy link
Member

yajo commented Feb 19, 2020

OK I have another idea.

If we know the ODOO and ODOO_ADDONS sections... then can't we assume all other modules are THIRDPARTY?

isort docs say:

default_section: The default section to place imports in, if their section can not be automatically determined. FIRSTPARTY, THIRDPARTY, etc.

So if we add to .isort.cfg:

default_section=THIRDPARTY

We should be able to use isort exactly the same way without needing to seed it. No conflicts, same behavior. Could this be our missing silver bullet?

yajo pushed a commit to Tecnativa/product-attribute that referenced this pull request Feb 28, 2020
This is a test implementation of the hypothesis explained in OCA/maintainer-quality-tools#629 (comment), which aims to avoid unnecessary and constant git conflicts in `.isort.cfg` file.
@yajo
Copy link
Member

yajo commented Feb 28, 2020

My hypothesis is confirmed, that's the path to go.

See a real world scenario: OCA/product-attribute#582

@sbidoul
Copy link
Member

sbidoul commented Feb 29, 2020

Yes! Nice find @yajo

@pedrobaeza
Copy link
Member

So are we deploying this config in all repos together with the prettier-xml thing?

sbidoul added a commit to acsone/maintainer-quality-tools that referenced this pull request Mar 1, 2020
@sbidoul
Copy link
Member

sbidoul commented Mar 1, 2020

So are we deploying this config in all repos together with the prettier-xml thing?

@pedrobaeza That is what I intend to do yes, when #632 and #636 are cleared.

@pedrobaeza
Copy link
Member

OK, thanks. @yajo can you help in finishing both?

hooks:
- id: isort
name: isort except __init__.py
exclude: /__init__\.py$
# TODO, the next time we upgrade isort's rev, we should remove 'types'
types: [python]
additional_dependencies: ["pipreqs==0.4.10", "pip-api==0.0.13"]
Copy link
Member

Choose a reason for hiding this comment

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

This is possibly not needed now.

@@ -9,4 +9,3 @@ line_length=88
known_odoo=odoo
known_odoo_addons=odoo.addons
sections=FUTURE,STDLIB,THIRDPARTY,ODOO,ODOO_ADDONS,FIRSTPARTY,LOCALFOLDER
known_third_party=
Copy link
Member

Choose a reason for hiding this comment

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

Missing the default section, see #629 (comment).

@sbidoul
Copy link
Member

sbidoul commented Mar 2, 2020

@yajo this one is superseded by #639, if @guewen agrees.

sbidoul added a commit to acsone/maintainer-quality-tools that referenced this pull request Mar 2, 2020
@guewen
Copy link
Member Author

guewen commented Mar 2, 2020

Yes, thanks folks!

@guewen guewen closed this Mar 2, 2020
sergio-teruel pushed a commit to Tecnativa/credit-control that referenced this pull request Mar 4, 2020
This is a test implementation of the hypothesis explained in OCA/maintainer-quality-tools#629 (comment), which aims to avoid unnecessary and constant git conflicts in `.isort.cfg` file.
sergio-teruel pushed a commit to Tecnativa/bank-payment that referenced this pull request Mar 4, 2020
This is a test implementation of the hypothesis explained in OCA/maintainer-quality-tools#629 (comment), which aims to avoid unnecessary and constant git conflicts in `.isort.cfg` file.
@kittiu
Copy link
Member

kittiu commented Apr 3, 2020

Hi, by looking around seem that now we use THIRDPARTY in stead of define own known_thirdparty, right?

We got travis error in this PR OCA/server-tools#1684 as it need openpyxl

Where can I add it? Thanks!

@sbidoul
Copy link
Member

sbidoul commented Apr 3, 2020

@kittiu the issue you are having in that PR is not related to this. .isort.cfg is only used to control sorting of imports in python files, not for installation. I provided a solution as a comment in your the PR.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

.isort.cfg is a conflicts bottleneck
7 participants