-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
CI: Added isort import check for the code on pull-request #4242
CI: Added isort import check for the code on pull-request #4242
Conversation
|
||
[tool.isort] | ||
known_first_party = ["pytorch_lightning","tests","pl_examples"] | ||
profile = "black" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Borda would we want use black style for sorting or other? There is also pycharm style https://pycqa.github.io/isort/docs/configuration/profiles/
Hi @nathanpainchaud which profile do you think is suitable for lightning which follows pep8 for code formatting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we get some examples?
for me split to three sections: build-in, third-party and locals
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is with black profile using command as this PR
import collections
import copy
import inspect
import os
import re
import tempfile
from abc import ABC
from argparse import Namespace
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union
import torch
from torch import ScriptModule, Tensor
from torch.nn import Module
from torch.optim.optimizer import Optimizer
from pytorch_lightning import _logger as log
from pytorch_lightning.core.grads import GradInformation
from pytorch_lightning.core.hooks import CheckpointHooks, DataHooks, ModelHooks
from pytorch_lightning.core.memory import ModelSummary
from pytorch_lightning.core.saving import ALLOWED_CONFIG_TYPES, PRIMITIVE_TYPES, ModelIO
from pytorch_lightning.core.step_result import Result
from pytorch_lightning.utilities import rank_zero_warn
from pytorch_lightning.utilities.device_dtype_mixin import DeviceDtypeModuleMixin
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities.parsing import AttributeDict, collect_init_args, get_init_args
from pytorch_lightning.utilities.xla_device_utils import XLADeviceUtils
with pycharm profile
import collections
import copy
import inspect
import os
import re
import tempfile
from abc import ABC
from argparse import Namespace
from typing import (
Any,
Callable,
Dict,
List,
Optional,
Sequence,
Tuple,
Union
)
import torch
from torch import (
ScriptModule,
Tensor
)
from torch.nn import Module
from torch.optim.optimizer import Optimizer
from pytorch_lightning import _logger as log
from pytorch_lightning.core.grads import GradInformation
from pytorch_lightning.core.hooks import (
CheckpointHooks,
DataHooks,
ModelHooks
)
from pytorch_lightning.core.memory import ModelSummary
from pytorch_lightning.core.saving import (
ALLOWED_CONFIG_TYPES,
PRIMITIVE_TYPES,
ModelIO
)
from pytorch_lightning.core.step_result import Result
from pytorch_lightning.utilities import rank_zero_warn
from pytorch_lightning.utilities.device_dtype_mixin import DeviceDtypeModuleMixin
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities.parsing import (
AttributeDict,
collect_init_args,
get_init_args
)
from pytorch_lightning.utilities.xla_device_utils import XLADeviceUtils
with google profile
from abc import ABC
from argparse import Namespace
import collections
import copy
import inspect
import os
import re
import tempfile
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union
import torch
from torch import ScriptModule
from torch import Tensor
from torch.nn import Module
from torch.optim.optimizer import Optimizer
from pytorch_lightning import _logger as log
from pytorch_lightning.core.grads import GradInformation
from pytorch_lightning.core.hooks import CheckpointHooks
from pytorch_lightning.core.hooks import DataHooks
from pytorch_lightning.core.hooks import ModelHooks
from pytorch_lightning.core.memory import ModelSummary
from pytorch_lightning.core.saving import ALLOWED_CONFIG_TYPES
from pytorch_lightning.core.saving import ModelIO
from pytorch_lightning.core.saving import PRIMITIVE_TYPES
from pytorch_lightning.core.step_result import Result
from pytorch_lightning.utilities import rank_zero_warn
from pytorch_lightning.utilities.device_dtype_mixin import DeviceDtypeModuleMixin
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities.parsing import AttributeDict
from pytorch_lightning.utilities.parsing import collect_init_args
from pytorch_lightning.utilities.parsing import get_init_args
from pytorch_lightning.utilities.xla_device_utils import XLADeviceUtils
no profile
import collections
import copy
import inspect
import os
import re
import tempfile
from abc import ABC
from argparse import Namespace
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union
import torch
from torch import ScriptModule, Tensor
from torch.nn import Module
from torch.optim.optimizer import Optimizer
from pytorch_lightning import _logger as log
from pytorch_lightning.core.grads import GradInformation
from pytorch_lightning.core.hooks import CheckpointHooks, DataHooks, ModelHooks
from pytorch_lightning.core.memory import ModelSummary
from pytorch_lightning.core.saving import ALLOWED_CONFIG_TYPES, PRIMITIVE_TYPES, ModelIO
from pytorch_lightning.core.step_result import Result
from pytorch_lightning.utilities import rank_zero_warn
from pytorch_lightning.utilities.device_dtype_mixin import DeviceDtypeModuleMixin
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities.parsing import AttributeDict, collect_init_args, get_init_args
from pytorch_lightning.utilities.xla_device_utils import XLADeviceUtils
All of them split built-in, third-party, locals
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ydcjeff already gave a good explanation. What I can add to this is that since PL has already decided to use black
for formatting the code, I would heavily suggest using the black
profile. Otherwise, we might fall into a "deadlock" between checks, where isort
and black
suggest conflicting changes (speaking from experience there 😛) Using the black
profile guarantees that both tools won't conflict with each other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think PL isn't using black
cc: @Borda
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not back for codebase just for formating imports
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, my bad! I saw the pre-commit hook for black
and I thought it was already in use. In that case, it's a bit up to personal preference, and what we want to prioritize.
I like the black format myself, but for a community project like PL I think a profile with single line imports (like google
and open_stack
) might prevent some conflicts in the imports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 from me on black
with consistent string formatting too. makes my OCD go crazy when i see some variables with single quotes and others with double quotes in the same file :)
- name: Install isort | ||
run: pip install isort==5.6.4 | ||
- name: Run isort | ||
run: isort --settings-path=./pyproject.toml --check-only . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a strong opinion, but how about adding --diff
option?
-df, --diff Prints a diff of all the changes isort would make to a
file, instead of changing it in place
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @nathanpainchaud, --diff
would be nice to see actually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The --check-only
option already lists the file that would be changed, without changing them in-place. From what I understand, --diff
would print all the changes, even inside each file. I've got no problem adding it, I just want to make sure it wouldn't be considered too noisy in the end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot ! Great addition to Pytorch Lightning. Would you mind adding it in pre-commit and resolving unit-tests ?
- name: Install isort | ||
run: pip install isort==5.6.4 | ||
- name: Run isort | ||
run: isort --settings-path=./pyproject.toml --check-only . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @nathanpainchaud, --diff
would be nice to see actually.
@@ -7,6 +7,21 @@ on: # Trigger the workflow on push or pull request, but only for the master bra | |||
branches: [master] | |||
|
|||
jobs: | |||
isort: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add isort
to pre-commit too. Here is an example: https://github.com/PyCQA/isort/blob/develop/.pre-commit-config.yaml
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I already did actually 😉 But I asked @Borda about it, and he wanted to keep the pre-commit hook for a different PR. When you're ready for it, you can just ping me though, because I already have it locally!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes as with accepting the CI check we need to format the codebase accordingly...
@tchaton when you mean resolving the unit-tests, do you mean applying the changes |
This pull request is now in conflict... :( |
I added the I didn't know of a way to give the authorship of the formatting commit to an automatic task rather than myself. If you would prefer it that way, I would be grateful if you could point me to indications on how to do so and I'll reapply the formatting. |
Codecov Report
@@ Coverage Diff @@
## master #4242 +/- ##
======================================
Coverage 93% 93%
======================================
Files 117 117
Lines 8914 8914
======================================
+ Hits 8280 8283 +3
+ Misses 634 631 -3 |
This pull request is now in conflict... :( |
1 similar comment
This pull request is now in conflict... :( |
- name: Install isort | ||
run: pip install isort==5.6.4 | ||
- name: Run isort | ||
run: isort --settings-path=./pyproject.toml --diff . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you keep --check
option to obtain UNIX exit status? Without --check
, it always exits with 0
, and it means that isort
check always passes on GitHub Actions no matter what the source code is. I have checked the behaviour at https://github.com/akihironitta/ci-example/runs/1305737153.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I assumed that --diff
also caused returned an error code, since it doesn't modify the files in-place (much like --check-only
in this sense). I didn't think to test it out. I've added back the --check-only
flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the swift action!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isort --help
now has the description of the exit status of --check
on the main branch! PyCQA/isort#1590
Checks the file for unsorted / unformatted imports and
prints them to the command line without modifying the file.
Returns 0 when nothing would change and returns 1 when
the file would be reformatted.
This pull request is now in conflict... :( |
I saw that issue #4241 for this was closed, but that a PR with the same goal was merged in bolts. @Borda, @ananthsub, @tchaton, @akihironitta any updates on what you want me to do with this PR to get this merged? |
With the new configuration, isort only applies on root-level (i.e. Eventually, we'll have to delete some glob expressions from the skipped files (https://github.com/nathanpainchaud/pytorch-lightning/blob/ci/4241_import_sort/pyproject.toml#L25-L29) to turn on the CI checks for them. |
that could be a solution for us, step-by-step reduce the skip pattern, similar as we have type check |
I'm glad that seems to be doing the trick for you! Given how isort functions, I don't even know (as of now) of any other way we could do it incrementally 😅 However, seeing as the pre-commit is still active for all files, I don't think it will be too long before most of the files' imports are sorted 😉 |
I have not checked it yet, just a proposal to try
yes, but not everyone is using pre-commit... for this peace by peace changes you temporarily disable it and apply isort just for some files, right? so let's change this PR such that it will introduce isort in pre-commit and in CI with ignoring all codebase, the create several smaller PRs wich would fix a few files and remove some patter from CI |
Yes. Right now, I just applied isort to root-level (i.e.
What you're proposing is what the PR is doing in its current state (since the changes I made earlier today). The only files currently affected are the exceptions mentioned above. Tests, examples and the library itself are currently excluded from CI checks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this initial step :]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Just a comment.
* added isort CI job and updated isort config * changed CI check output from files to full diff * added isort pre-commit hook * Added missing first party and restricted files affected by isort * Applied isort to root-level, docs and benchmarks * Apply suggestions from code review Co-authored-by: Nathan Painchaud <nathanpainchaud@gmail.com> Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com> Co-authored-by: chaton <thomas@grid.ai>
* added isort CI job and updated isort config * changed CI check output from files to full diff * added isort pre-commit hook * Added missing first party and restricted files affected by isort * Applied isort to root-level, docs and benchmarks * Apply suggestions from code review Co-authored-by: Nathan Painchaud <nathanpainchaud@gmail.com> Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com> Co-authored-by: chaton <thomas@grid.ai>
What does this PR do?
Fixes #4241
Before submitting
PR review
Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.
Did you have fun?
Make sure you had fun coding 🙃