Repository: Lightning-AI/pytorch-lightning
Pull request: CI: Added isort import check for the code on pull-request
## What does this PR do?

<!--
Please include a summary of the change and which issue is fixed.
 Please also include relevant motivation and context.
 List any dependencies that are required for this change.
-->

Fixes #4241 

# Before submitting

- [x] Was this discussed/approved via a Github issue? (no need for typos and docs improvements)
- [x] Did you read the [contributor guideline](https://github.com/PyTorchLightning/pytorch-lightning/blob/master/.github/CONTRIBUTING.md), Pull Request section?
- [x] Did you make sure your PR does only one thing, instead of bundling different changes together? Otherwise, we ask you to create a separate PR for every change.
- [ ] Did you make sure to update the documentation with your changes?
- [ ] Did you write any new necessary tests? 
- [ ] Did you verify new and existing tests pass locally with your changes?
- [ ] If you made a notable change (that affects users), did you update the [CHANGELOG](https://github.com/PyTorchLightning/pytorch-lightning/blob/master/CHANGELOG.md)?

<!-- For CHANGELOG separate each item in unreleased section by a blank line to reduce collisions -->

## 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 🙃


Problem statement:
add CI check for valid import formatting
## 🚀 Feature

add a CI checker for valid import formatting - three section: build-in, third party, and this package
note that all "unused" imports in init shall be ignored and they are meant such a way... 

### Motivation

We shall follow pep8 recomendation

Hints from issue discussion:
@Borda I'm up for adding this, since I've already done this for my own project based on PL :wink:

Commit message: Applied isort to root-level, docs and benchmarks

Patch to review:
diff --git a/.github/workflows/code-formatting.yml b/.github/workflows/code-formatting.yml
index e549f5b8f1cfb..31445c7ae96a3 100644
--- a/.github/workflows/code-formatting.yml
+++ b/.github/workflows/code-formatting.yml
@@ -7,6 +7,21 @@ on:  # Trigger the workflow on push or pull request, but only for the master bra
     branches: [master, "release/*"]
 
 jobs:
+  isort:
+    name: Check valid import formatting with isort
+    runs-on: ubuntu-20.04
+    steps:
+      - name: Checkout
+        uses: actions/checkout@v2
+      - name: Set up Python 3.8
+        uses: actions/setup-python@v2
+        with:
+          python-version: 3.8
+      - name: Install isort
+        run: pip install isort==5.6.4
+      - name: Run isort
+        run: isort --settings-path=./pyproject.toml --check-only --diff .
+
   code-black:
     name: Check code formatting with Black
     runs-on: ubuntu-20.04
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 63987145ce9c0..9b481e9ecf0a1 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -21,3 +21,12 @@ repos:
     hooks:
       - id: trailing-whitespace
       - id: end-of-file-fixer
+
+  - repo: local
+    hooks:
+      - id: isort
+        name: isort
+        entry: python -m isort
+        args: [--settings-path, ./pyproject.toml]
+        language: system
+        types: [python]
diff --git a/benchmarks/test_parity.py b/benchmarks/test_parity.py
index d2bc97deff598..d54742489a62c 100644
--- a/benchmarks/test_parity.py
+++ b/benchmarks/test_parity.py
@@ -6,7 +6,7 @@
 
 import tests.base.develop_utils as tutils
 from pytorch_lightning import Trainer, seed_everything
-from tests.base.models import ParityModuleRNN, ParityModuleMNIST
+from tests.base.models import ParityModuleMNIST, ParityModuleRNN
 
 
 @pytest.mark.parametrize('cls_model,max_diff', [
diff --git a/docs/source/conf.py b/docs/source/conf.py
index c662e1e9c912e..bb725893d3346 100644
--- a/docs/source/conf.py
+++ b/docs/source/conf.py
@@ -12,14 +12,14 @@
 # add these directories to sys.path here. If the directory is relative to the
 # documentation root, use os.path.abspath to make it absolute, like shown here.
 
-import os
-import sys
+# import m2r
+import builtins
 import glob
-import shutil
 import inspect
+import os
+import shutil
+import sys
 
-# import m2r
-import builtins
 import pt_lightning_sphinx_theme
 
 PATH_HERE = os.path.abspath(os.path.dirname(__file__))
diff --git a/pyproject.toml b/pyproject.toml
index 539d8117be4d2..83109f5b6cbb7 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -13,3 +13,19 @@ ignore = ["W504", "W504", "E402", "E731", "C40", "E741", "F40", "F841"]
 line-length = 120
 target-version = ["py38"]
 exclude = "(.eggs|.git|.hg|.mypy_cache|.nox|.tox|.venv|.svn|_build|buck-out|build|dist)"
+
+[tool.isort]
+known_first_party = [
+    "bencharmks",
+    "docs",
+    "pl_examples",
+    "pytorch_lightning",
+    "tests",
+]
+skip_glob = [
+    "pl_examples/*",
+    "pytorch_lightning/*",
+    "tests/*"
+]    # Only apply formatting on root-level scripts, docs and benchmarks
+profile = "black"
+line_length = 120
diff --git a/requirements/test.txt b/requirements/test.txt
index d98048568fa75..73ec319443e4f 100644
--- a/requirements/test.txt
+++ b/requirements/test.txt
@@ -9,6 +9,7 @@ check-manifest
 twine==1.13.0
 scikit-image
 black>=20.8b1
+isort>=5.6.4
 pre-commit>=1.0
 
 cloudpickle>=1.2
diff --git a/setup.cfg b/setup.cfg
index 7c31ee5f5542f..f1026b1eac136 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -90,8 +90,3 @@ convention = pep257
 # D202: Ignore a blank line after docstring (collision with Python Black in decorators)
 add-ignore = D104,D107,D202
 max-line-length = 120
-
-[tool:isort]
-known_first_party = pytorch_lightning,tests
-default_section=THIRDPARTY
-line_length=120
diff --git a/setup.py b/setup.py
index d25918417e639..e811f7a33d972 100755
--- a/setup.py
+++ b/setup.py
@@ -15,8 +15,9 @@
 
 import os
 from io import open
+
 # Always prefer setuptools over distutils
-from setuptools import setup, find_packages
+from setuptools import find_packages, setup
 
 try:
     import builtins


Code review comments:
diff_hunk: @@ -7,6 +7,21 @@ on:  # Trigger the workflow on push or pull request, but only for the master bra
     branches: [master, "release/*"]
 
 jobs:
+  isort:
path: .github/workflows/code-formatting.yml
text: @user1:
```suggestion
  imports-check-isort:
```