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

Sort init import #10801

Merged
merged 6 commits into from
Mar 19, 2021
Merged

Sort init import #10801

merged 6 commits into from
Mar 19, 2021

Conversation

sgugger
Copy link
Collaborator

@sgugger sgugger commented Mar 19, 2021

What does this PR do?

Not a high-priority item but I get bored at nights and I like writing those kinds of scripts 😅

So this PR adds a script to properly sort the import inside _import_structure because people have been absolutely ruthless and putting their objects in any kind of random order. That's not very feng-shui so I'm bringing back harmony by having the same sort as isort applied to all __init__ that contain an _import_structure.

@sgugger sgugger requested a review from LysandreJik March 19, 2021 00:24
@@ -2170,7 +2166,7 @@
TFLxmertPreTrainedModel,
TFLxmertVisualFeatureEncoder,
)
from .models.marian import TFMarian, TFMarianMTModel
from .models.marian import TFMarianModel, TFMarianMTModel
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Completely unrelated to this PR, renamed that object to its proper name.

Copy link
Member

Choose a reason for hiding this comment

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

Nice catch!

Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

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

Very nice! LGTM

@@ -2170,7 +2166,7 @@
TFLxmertPreTrainedModel,
TFLxmertVisualFeatureEncoder,
)
from .models.marian import TFMarian, TFMarianMTModel
from .models.marian import TFMarianModel, TFMarianMTModel
Copy link
Member

Choose a reason for hiding this comment

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

Nice catch!

Makefile Outdated
python utils/style_doc.py src/transformers docs/source --max_len 119
python utils/style_doc.py src/transformers docs/source --max_len 119 --check_only
Copy link
Member

Choose a reason for hiding this comment

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

If I'm not mistaken, this prevents your style_doc.py to do changes when used with make fixup, is that voluntary?

python utils/class_mapping_update.py

# this target runs checks on all files
quality:
black --check $(check_dirs)
isort --check-only $(check_dirs)
python utils/custom_init_isort.py --check_only
Copy link
Member

Choose a reason for hiding this comment

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

I would put this in the extra_quality_checks too so that it runs with make fixup

utils/custom_init_isort.py Outdated Show resolved Hide resolved
sgugger and others added 2 commits March 19, 2021 09:52
@sgugger
Copy link
Collaborator Author

sgugger commented Mar 19, 2021

To address your comments on the MakeFile, I have removed some checks from extra_quality_checks because they are checks that modify content and make quality is only supposed to check, not change.

To have make fixup still work as intended, I put the checks that change content in extra_style_checks that is called both by make fixup and make style. Could you double-check it looks okay @LysandreJik and @stas00 ? Thanks!

Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

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

Your changes LGTM. I agree with you that @stas00's review would be welcome before we merge.

Copy link
Contributor

@stas00 stas00 left a comment

Choose a reason for hiding this comment

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

LGTM

Added another small tweak suggestion

Makefile Outdated
python utils/check_copies.py
python utils/check_table.py
python utils/check_dummies.py
python utils/check_repo.py
python utils/style_doc.py src/transformers docs/source --max_len 119
python utils/class_mapping_update.py
Copy link
Contributor

@stas00 stas00 Mar 19, 2021

Choose a reason for hiding this comment

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

should we create a new target for all the auto-generation items so it's loud-n-clear? It'd be something like:

autogenerate_code: deps_table_update
        python utils/class_mapping_update.py

as these aren't style or quality

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh I hadn't realized class_mapping_update was writing things. It should also disappear from make quality then to go with the others in extra_style_checks.

For me:

make fixup -> applies the styling patches and checks everything passes quickly
make style -> applying styling patches and other auto-generated code
make quality -> only checks, does not write anything

Copy link
Contributor

Choose a reason for hiding this comment

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

I only ever use make fixup - the initial design was that fixup = style+quality on only modified files.

So it was created before we added 2 items that generated code, so if others prefer the quick version then fixup should include the autogenerate target too.

Actually, is there is a situation where fixup won't do the right thing already? When does one need to run the full make style?

Copy link
Contributor

@stas00 stas00 Mar 19, 2021

Choose a reason for hiding this comment

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

class_mapping_update doesn't do anything if the relevant file hasn't changed so it's blazingly fast

we can do the same for deps_table_update so it'd be instant 99% of the time, as currently it always runs.

Copy link
Collaborator Author

@sgugger sgugger Mar 19, 2021

Choose a reason for hiding this comment

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

Yes fixup is style+quality on only modified files. My problem is that some processes that modify files have been added to extra-quality-checks which is run by quality on top of fixup. So fixup runs extra-quality-checks as well as extra-styling-checks to do both, I don't want to change that part.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

See latest version for my proposal:

  • extra-quality-checks common processes for quality and fixup, checks that are fast or need to run on all files, not changing any file
  • extra-style-checks common processes for style and fixup, checks that are fast or need to run on all files, changing files if necessary
  • style style all files + other checks that are "writing" the right thing
  • quality check all files + other checks (in check only mode)
  • fixup same as style + quality but only on modified files.

Makefile Show resolved Hide resolved
@sgugger sgugger merged commit 21e86f9 into master Mar 19, 2021
@sgugger sgugger deleted the sort_init_import branch March 19, 2021 20:17
Iwontbecreative pushed a commit to Iwontbecreative/transformers that referenced this pull request Jul 15, 2021
* Initial script

* Add script to properly sort imports in init.

* Add to the CI

* Update utils/custom_init_isort.py

Co-authored-by: Lysandre Debut <lysandre@huggingface.co>

* Separate scripts that change content from quality

* Move class_mapping_update to style_checks

Co-authored-by: Lysandre Debut <lysandre@huggingface.co>
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