isort icon indicating copy to clipboard operation
isort copied to clipboard

Why does third_party and my_lib get mixed up in isort==5.x? isort==4.x has no such phenomenon.

Open 549982170 opened this issue 6 years ago • 6 comments

Why does third_party and my_lib get mixed up in isort==5.x? isort==4.x has no such phenomenon.

549982170 avatar Jul 27 '20 09:07 549982170

Hi @549982170! I'm sorry you are encountering this issue!

To better help me diagnose this issue can you provide:

The output of isort --show-config The general structure of your project (where is .isort.cfg or repo root, where are your modules? Do you use a src directory?)

Thanks!

~Timothy

timothycrosley avatar Jul 28 '20 03:07 timothycrosley

Hi @timothycrosley ,

I think I met the same problem that the results of 4.3.21 and 5.2.0 are different. Here is my output of isort --show-config:

{
    "_known_patterns": null,
    "_section_comments": null,
    "py_version": "py3",
    "force_to_top": [],
    "skip": [
        "build",
        "buck-out",
        ".venv",
        ".nox",
        ".tox",
        ".mypy_cache",
        "dist",
        ".hg",
        "venv",
        "node_modules",
        ".eggs",
        ".git",
        "_build",
        ".pants.d"
    ],
    "skip_glob": [],
    "skip_gitignore": false,
    "line_length": 100,
    "wrap_length": 0,
    "line_ending": "",
    "sections": [
        "FUTURE",
        "STDLIB",
        "THIRDPARTY",
        "FIRSTPARTY",
        "LOCALFOLDER"
    ],
    "no_sections": false,
    "known_future_library": [
        "__future__"
    ],
    "known_third_party": [
        "numpy",
        "yaml",
        "pycocotools",
        "cv2",
        "matplotlib",
        "yacs",
        "torch",
        "six",
        "portalocker"
    ],
    "known_first_party": [
        "foundation"
    ],
    "known_local_folder": [],
    "known_standard_library": [
        "setuptools",
        "pkg_resources"
    ],
    "extra_standard_library": [],
    "known_other": {},
    "multi_line_output": "VERTICAL_HANGING_INDENT",
    "forced_separate": [],
    "indent": "    ",
    "comment_prefix": "  #",
    "length_sort": false,
    "length_sort_sections": [],
    "add_imports": [],
    "remove_imports": [],
    "append_only": false,
    "reverse_relative": false,
    "force_single_line": false,
    "single_line_exclusions": [],
    "default_section": "THIRDPARTY",
    "import_headings": {},
    "balanced_wrapping": false,
    "use_parentheses": false,
    "order_by_type": true,
    "atomic": false,
    "lines_after_imports": -1,
    "lines_between_sections": 1,
    "lines_between_types": 0,
    "combine_as_imports": true,
    "combine_star": false,
    "include_trailing_comma": true,
    "from_first": false,
    "verbose": false,
    "quiet": false,
    "force_adds": false,
    "force_alphabetical_sort_within_sections": false,
    "force_alphabetical_sort": false,
    "force_grid_wrap": 0,
    "force_sort_within_sections": false,
    "lexicographical": false,
    "ignore_whitespace": false,
    "no_lines_before": [
        "LOCALFOLDER"
    ],
    "no_inline_sort": false,
    "ignore_comments": false,
    "case_sensitive": false,
    "sources": [
        {
            "py_version": "py3",
            "force_to_top": [],
            "skip": [
                "build",
                "buck-out",
                ".venv",
                ".nox",
                ".tox",
                ".mypy_cache",
                "dist",
                ".hg",
                "venv",
                "node_modules",
                ".eggs",
                ".git",
                "_build",
                ".pants.d"
            ],
            "skip_glob": [],
            "skip_gitignore": false,
            "line_length": 79,
            "wrap_length": 0,
            "line_ending": "",
            "sections": [
                "FUTURE",
                "STDLIB",
                "THIRDPARTY",
                "FIRSTPARTY",
                "LOCALFOLDER"
            ],
            "no_sections": false,
            "known_future_library": [
                "__future__"
            ],
            "known_third_party": [
                "google.appengine.api"
            ],
            "known_first_party": [],
            "known_local_folder": [],
            "known_standard_library": [
                "json",
                "operator",
                "errno",
                "mimetypes",
                "pydoc",
                "webbrowser",
                "binhex",
                "uu",
                "turtledemo",
                "pkgutil",
                "nis",
                "array",
                "shlex",
                "struct",
                "asyncore",
                "colorsys",
                "chunk",
                "zipimport",
                "atexit",
                "wsgiref",
                "wave",
                "sunau",
                "msilib",
                "shelve",
                "hashlib",
                "bisect",
                "symtable",
                "locale",
                "gettext",
                "getopt",
                "os",
                "dummy_threading",
                "imp",
                "ntpath",
                "test",
                "sqlite3",
                "encodings",
                "crypt",
                "io",
                "resource",
                "mailbox",
                "collections",
                "pipes",
                "string",
                "sre",
                "random",
                "unittest",
                "bz2",
                "logging",
                "doctest",
                "stringprep",
                "sys",
                "decimal",
                "dbm",
                "tempfile",
                "ssl",
                "cmd",
                "statistics",
                "hmac",
                "zlib",
                "ast",
                "_dummy_thread",
                "difflib",
                "queue",
                "argparse",
                "spwd",
                "formatter",
                "numbers",
                "token",
                "winsound",
                "modulefinder",
                "contextlib",
                "sre_compile",
                "tkinter",
                "ipaddress",
                "multiprocessing",
                "sre_constants",
                "fnmatch",
                "dataclasses",
                "socket",
                "netrc",
                "plistlib",
                "urllib",
                "asyncio",
                "msvcrt",
                "symbol",
                "math",
                "zipfile",
                "tarfile",
                "grp",
                "posixpath",
                "textwrap",
                "pickletools",
                "cProfile",
                "select",
                "imaplib",
                "py_compile",
                "copy",
                "linecache",
                "trace",
                "turtle",
                "pdb",
                "zipapp",
                "concurrent",
                "types",
                "fcntl",
                "fractions",
                "weakref",
                "_thread",
                "contextvars",
                "readline",
                "smtpd",
                "fpectl",
                "venv",
                "pyclbr",
                "getpass",
                "cmath",
                "lzma",
                "tokenize",
                "code",
                "ctypes",
                "warnings",
                "abc",
                "profile",
                "audioop",
                "parser",
                "time",
                "ensurepip",
                "sched",
                "poplib",
                "filecmp",
                "optparse",
                "syslog",
                "itertools",
                "ossaudiodev",
                "enum",
                "timeit",
                "posix",
                "inspect",
                "curses",
                "stat",
                "heapq",
                "termios",
                "socketserver",
                "aifc",
                "nntplib",
                "cgi",
                "mmap",
                "pwd",
                "functools",
                "http",
                "traceback",
                "gzip",
                "re",
                "bdb",
                "fileinput",
                "mailcap",
                "winreg",
                "builtins",
                "pty",
                "marshal",
                "html",
                "selectors",
                "tty",
                "distutils",
                "email",
                "binascii",
                "importlib",
                "xmlrpc",
                "dis",
                "cgitb",
                "sndhdr",
                "unicodedata",
                "runpy",
                "rlcompleter",
                "datetime",
                "base64",
                "smtplib",
                "sysconfig",
                "ftplib",
                "sre_parse",
                "uuid",
                "xml",
                "tracemalloc",
                "calendar",
                "signal",
                "reprlib",
                "platform",
                "codecs",
                "secrets",
                "quopri",
                "copyreg",
                "compileall",
                "imghdr",
                "pstats",
                "macpath",
                "telnetlib",
                "faulthandler",
                "gc",
                "subprocess",
                "shutil",
                "site",
                "pathlib",
                "pickle",
                "typing",
                "keyword",
                "asynchat",
                "lib2to3",
                "tabnanny",
                "codeop",
                "glob",
                "pprint",
                "threading",
                "csv",
                "xdrlib",
                "configparser"
            ],
            "extra_standard_library": [],
            "known_other": {},
            "multi_line_output": "GRID",
            "forced_separate": [],
            "indent": "    ",
            "comment_prefix": "  #",
            "length_sort": false,
            "length_sort_sections": [],
            "add_imports": [],
            "remove_imports": [],
            "append_only": false,
            "reverse_relative": false,
            "force_single_line": false,
            "single_line_exclusions": [],
            "default_section": "THIRDPARTY",
            "import_headings": {},
            "balanced_wrapping": false,
            "use_parentheses": false,
            "order_by_type": true,
            "atomic": false,
            "lines_after_imports": -1,
            "lines_between_sections": 1,
            "lines_between_types": 0,
            "combine_as_imports": false,
            "combine_star": false,
            "include_trailing_comma": false,
            "from_first": false,
            "verbose": false,
            "quiet": false,
            "force_adds": false,
            "force_alphabetical_sort_within_sections": false,
            "force_alphabetical_sort": false,
            "force_grid_wrap": 0,
            "force_sort_within_sections": false,
            "lexicographical": false,
            "ignore_whitespace": false,
            "no_lines_before": [],
            "no_inline_sort": false,
            "ignore_comments": false,
            "case_sensitive": false,
            "sources": [],
            "virtual_env": "",
            "conda_env": "",
            "ensure_newline_before_comments": false,
            "directory": "",
            "profile": "",
            "honor_noqa": false,
            "src_paths": [],
            "old_finders": false,
            "remove_redundant_aliases": false,
            "float_to_top": false,
            "filter_files": false,
            "formatter": "",
            "formatting_function": null,
            "source": "defaults"
        },
        {
            "line_length": 100,
            "multi_line_output": "VERTICAL_HANGING_INDENT",
            "include_trailing_comma": true,
            "known_standard_library": [
                "setuptools",
                "pkg_resources"
            ],
            "known_first_party": [
                "foundation"
            ],
            "default_section": "THIRDPARTY",
            "sections": [
                "FUTURE",
                "STDLIB",
                "THIRDPARTY",
                "FIRSTPARTY",
                "LOCALFOLDER"
            ],
            "no_lines_before": [
                "LOCALFOLDER"
            ],
            "known_third_party": [
                "numpy",
                "yaml",
                "pycocotools",
                "cv2",
                "matplotlib",
                "yacs",
                "torch",
                "six",
                "portalocker"
            ],
            "combine_as_imports": true,
            "force_grid_wrap": 0,
            "force_single_line": false,
            "source": "/path/to/foundation/setup.cfg"
        }
    ],
    "virtual_env": "",
    "conda_env": "",
    "ensure_newline_before_comments": false,
    "directory": "/path/to/foundation",
    "profile": "",
    "honor_noqa": false,
    "src_paths": [
        "/path/to/foundation",
        "/path/to/foundation/src"
    ],
    "old_finders": false,
    "remove_redundant_aliases": false,
    "float_to_top": false,
    "filter_files": false,
    "formatter": "",
    "formatting_function": null
}

And the structure of my project is:

.
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── foundation/
├── setup.cfg
└── setup.py

OneDirection9 avatar Jul 28 '20 11:07 OneDirection9

@OneDirection9, Thanks for sharing. Your config looks okay at first site to me, though you do have some extra unneeded configuration options: known_third_party, known_first_party, 'default_section', 'sections', and force_single_line are all restates of the default - but they shouldn't cause any problems. What exactly is the change you are seeing? Can you provide the diffs of imports of one of the files? Some changes are expected in the new release.

timothycrosley avatar Jul 29 '20 03:07 timothycrosley

Hi @timothycrosley ,

Thanks for your reply. Here is the command that I executed.

(base) ➜  foundation git:(master) pip list | grep isort
isort                              4.3.21
(base) ➜  foundation git:(master) isort -rc --check-only --diff foundation/ tests/
(base) ➜  foundation git:(master) source activate cv
(cv) ➜  foundation git:(master) pip list | grep isort
isort                5.2.0
(cv) ➜  foundation git:(master) isort -rc --check-only --diff foundation/ tests/
ERROR: /path/to/foundation/tests/test_visualizer.py Imports are incorrectly sorted and/or formatted.
--- /path/to/foundation/tests/test_visualizer.py:before  2020-07-29 10:40:01.655204
+++ /path/to/foundation/tests/test_visualizer.py:after   2020-07-29 11:48:01.446405
 from __future__ import absolute_import, division, print_function

 import contextlib
+import cv2
 import io
+import numpy as np
 import os.path as osp
 import unittest
-from typing import List, Tuple, Union
-
-import cv2
-import numpy as np
 from pycocotools import mask as mask_util
 from pycocotools.coco import COCO
+from typing import List, Tuple, Union
.....
.....

It can be see that the 4.3.21 outputs nothing, but 5.2.0 rearanged the cv2, numpy to first party, and so on.

OneDirection9 avatar Jul 29 '20 03:07 OneDirection9

@OneDirection9, I should have noticed sooner! I think you simply need to change known_standard_library to extra_standard_library in your configuration per the upgrade guide: https://timothycrosley.github.io/isort/docs/upgrade_guides/5.0.0/#known_standard_library

timothycrosley avatar Jul 29 '20 04:07 timothycrosley

Hi @timothycrosley ,

Wow, cool! thanks. This solved my problem. ^_^

OneDirection9 avatar Jul 29 '20 04:07 OneDirection9