PyCQA / isort

A Python utility / library to sort imports.
https://pycqa.github.io/isort/
MIT License
6.49k stars 580 forks source link

ERROR: Unrecoverable exception thrown when parsing with Python 3.8.8 #1679

Closed librehat closed 3 years ago

librehat commented 3 years ago

I've recently upgraded my Python interpreter to 3.8.8 (macOS, installed via homebrew), and isort to 5.7.0 (from 4.x). It now crashes when checking a __init__.py file:

poetry run isort --check .
ERROR: Unrecoverable exception thrown when parsing ./<MASKED>/types/__init__.py!This should NEVER happen.
If encountered, please open an issue: https://github.com/PyCQA/isort/issues/new
Traceback (most recent call last):
  File "/Users/<MASKED>/Library/Caches/pypoetry/virtualenvs/<MASKED>-91d2SnbW-py3.8/bin/isort", line 8, in <module>
    sys.exit(main())
  File "/Users/<MASKED>/Library/Caches/pypoetry/virtualenvs/<MASKED>-91d2SnbW-py3.8/lib/python3.8/site-packages/isort/main.py", line 1086, in main
    for sort_attempt in attempt_iterator:
  File "/Users/<MASKED>/Library/Caches/pypoetry/virtualenvs/<MASKED>-91d2SnbW-py3.8/lib/python3.8/site-packages/isort/main.py", line 1071, in <genexpr>
    sort_imports(  # type: ignore
  File "/Users/<MASKED>/Library/Caches/pypoetry/virtualenvs/<MASKED>-91d2SnbW-py3.8/lib/python3.8/site-packages/isort/main.py", line 90, in sort_imports
    incorrectly_sorted = not api.check_file(file_name, config=config, **kwargs)
  File "/Users/<MASKED>/Library/Caches/pypoetry/virtualenvs/<MASKED>-91d2SnbW-py3.8/lib/python3.8/site-packages/isort/api.py", line 295, in check_file
    return check_stream(
  File "/Users/<MASKED>/Library/Caches/pypoetry/virtualenvs/<MASKED>-91d2SnbW-py3.8/lib/python3.8/site-packages/isort/api.py", line 234, in check_stream
    changed: bool = sort_stream(
  File "/Users/<MASKED>/Library/Caches/pypoetry/virtualenvs/<MASKED>-91d2SnbW-py3.8/lib/python3.8/site-packages/isort/api.py", line 186, in sort_stream
    changed = core.process(
  File "/Users/<MASKED>/Library/Caches/pypoetry/virtualenvs/<MASKED>-91d2SnbW-py3.8/lib/python3.8/site-packages/isort/core.py", line 342, in process
    parsed_content = parse.file_contents(import_section, config=config)
  File "/Users/<MASKED>/Library/Caches/pypoetry/virtualenvs/<MASKED>-91d2SnbW-py3.8/lib/python3.8/site-packages/isort/parse.py", line 433, in file_contents
    root = imports[placed_module][type_of_import]  # type: ignore
KeyError: 'LOCALFOLDER'
make: *** [isort] Error 1

This __init__.py file's contents:

from .<MASKED_x> import *  # noqa: F401, F403
from .<MASKED_y> import *  # noqa: F401, F403
from .<MASKED_z> import *  # noqa: F401, F403

I tried to downgrade isort back to major version 4 and that would work without a problem

librehat commented 3 years ago

An addition to help debugging maybe: this failure happens on Debian Linux as well (Docker Python-3.8:slim image)

timothycrosley commented 3 years ago

@librehat are you able to share your config file? IE by running isort . --show-config

timothycrosley commented 3 years ago

For reference, the error traceback makes me thing that you likely have an sections list without LOCALFOLDER in it, and something is recognized as being part of a local folder

librehat commented 3 years ago

@timothycrosley sure, the config is

{
    "_known_patterns": null,
    "_section_comments": null,
    "py_version": "py3",
    "force_to_top": [],
    "skip": [
        ".hg",
        ".git",
        ".eggs",
        ".nox",
        "buck-out",
        ".direnv",
        ".svn",
        "dist",
        ".pants.d",
        ".tox",
        "_build",
        ".bzr",
        ".venv",
        "build",
        "venv",
        "node_modules",
        ".mypy_cache"
    ],
    "skip_glob": [],
    "skip_gitignore": false,
    "line_length": 95,
    "wrap_length": 0,
    "line_ending": "",
    "sections": [
        "STDLIB",
        "THIRDPARTY",
        "FIRSTPARTY",
        "TESTS"
    ],
    "no_sections": false,
    "known_future_library": [
        "__future__"
    ],
    "known_third_party": [],
    "known_first_party": [],
    "known_local_folder": [],
    "known_standard_library": [
        "pickle",
        "html",
        "webbrowser",
        "http",
        "symtable",
        "turtledemo",
        "token",
        "resource",
        "pty",
        "uu",
        "binascii",
        "winsound",
        "logging",
        "threading",
        "copy",
        "graphlib",
        "calendar",
        "getopt",
        "marshal",
        "symbol",
        "msilib",
        "datetime",
        "pdb",
        "xdrlib",
        "enum",
        "hmac",
        "rlcompleter",
        "queue",
        "textwrap",
        "pipes",
        "gettext",
        "plistlib",
        "bisect",
        "readline",
        "poplib",
        "inspect",
        "tracemalloc",
        "tarfile",
        "string",
        "pprint",
        "functools",
        "abc",
        "dis",
        "weakref",
        "faulthandler",
        "locale",
        "linecache",
        "unicodedata",
        "sunau",
        "winreg",
        "tabnanny",
        "pwd",
        "imaplib",
        "select",
        "asynchat",
        "glob",
        "lib2to3",
        "configparser",
        "concurrent",
        "sre_constants",
        "zipimport",
        "wave",
        "codeop",
        "math",
        "syslog",
        "tkinter",
        "ftplib",
        "_dummy_thread",
        "wsgiref",
        "fractions",
        "ossaudiodev",
        "venv",
        "site",
        "bz2",
        "distutils",
        "aifc",
        "cmath",
        "asyncio",
        "sre_parse",
        "sqlite3",
        "base64",
        "ast",
        "pickletools",
        "chunk",
        "selectors",
        "cgitb",
        "mailcap",
        "contextvars",
        "zipfile",
        "imp",
        "reprlib",
        "crypt",
        "sysconfig",
        "builtins",
        "email",
        "tty",
        "colorsys",
        "os",
        "gc",
        "binhex",
        "atexit",
        "spwd",
        "modulefinder",
        "codecs",
        "nntplib",
        "parser",
        "encodings",
        "asyncore",
        "socket",
        "shlex",
        "cmd",
        "decimal",
        "grp",
        "subprocess",
        "array",
        "cProfile",
        "getpass",
        "sre_compile",
        "formatter",
        "compileall",
        "imghdr",
        "termios",
        "posix",
        "telnetlib",
        "smtplib",
        "secrets",
        "stat",
        "filecmp",
        "fileinput",
        "trace",
        "code",
        "pkgutil",
        "fnmatch",
        "xml",
        "ipaddress",
        "random",
        "pathlib",
        "sndhdr",
        "fpectl",
        "operator",
        "collections",
        "typing",
        "xmlrpc",
        "stringprep",
        "doctest",
        "argparse",
        "socketserver",
        "bdb",
        "copyreg",
        "zoneinfo",
        "_thread",
        "errno",
        "macpath",
        "json",
        "smtpd",
        "numbers",
        "zipapp",
        "ensurepip",
        "multiprocessing",
        "profile",
        "py_compile",
        "sched",
        "re",
        "types",
        "signal",
        "ctypes",
        "optparse",
        "difflib",
        "heapq",
        "itertools",
        "platform",
        "sre",
        "cgi",
        "lzma",
        "fcntl",
        "pydoc",
        "quopri",
        "turtle",
        "contextlib",
        "mailbox",
        "nis",
        "tokenize",
        "mimetypes",
        "gzip",
        "shutil",
        "test",
        "hashlib",
        "timeit",
        "time",
        "shelve",
        "pyclbr",
        "dataclasses",
        "tempfile",
        "netrc",
        "posixpath",
        "uuid",
        "zlib",
        "pstats",
        "ssl",
        "dbm",
        "sys",
        "traceback",
        "msvcrt",
        "statistics",
        "audioop",
        "urllib",
        "ntpath",
        "csv",
        "io",
        "unittest",
        "mmap",
        "keyword",
        "struct",
        "runpy",
        "warnings",
        "importlib",
        "dummy_threading",
        "curses"
    ],
    "extra_standard_library": [],
    "known_other": {
        "tests": [
            "tests"
        ]
    },
    "multi_line_output": "VERTICAL_HANGING_INDENT",
    "forced_separate": [],
    "indent": "    ",
    "comment_prefix": "  #",
    "length_sort": false,
    "length_sort_straight": 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": 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,
    "group_by_package": false,
    "ignore_whitespace": false,
    "no_lines_before": [],
    "no_inline_sort": false,
    "ignore_comments": false,
    "case_sensitive": false,
    "sources": [
        {
            "py_version": "py3",
            "force_to_top": [],
            "skip": [
                ".hg",
                ".git",
                ".eggs",
                ".nox",
                "buck-out",
                ".direnv",
                ".svn",
                "dist",
                ".pants.d",
                ".tox",
                "_build",
                ".bzr",
                ".venv",
                "build",
                "venv",
                "node_modules",
                ".mypy_cache"
            ],
            "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": [],
            "known_first_party": [],
            "known_local_folder": [],
            "known_standard_library": [
                "pickle",
                "html",
                "webbrowser",
                "http",
                "symtable",
                "turtledemo",
                "token",
                "resource",
                "pty",
                "uu",
                "binascii",
                "winsound",
                "logging",
                "threading",
                "copy",
                "graphlib",
                "calendar",
                "getopt",
                "marshal",
                "symbol",
                "msilib",
                "datetime",
                "pdb",
                "xdrlib",
                "enum",
                "hmac",
                "rlcompleter",
                "queue",
                "textwrap",
                "pipes",
                "gettext",
                "plistlib",
                "bisect",
                "readline",
                "poplib",
                "inspect",
                "tracemalloc",
                "tarfile",
                "string",
                "pprint",
                "functools",
                "abc",
                "dis",
                "weakref",
                "faulthandler",
                "locale",
                "linecache",
                "unicodedata",
                "sunau",
                "winreg",
                "tabnanny",
                "pwd",
                "imaplib",
                "select",
                "asynchat",
                "glob",
                "lib2to3",
                "configparser",
                "concurrent",
                "sre_constants",
                "zipimport",
                "wave",
                "codeop",
                "math",
                "syslog",
                "tkinter",
                "ftplib",
                "_dummy_thread",
                "wsgiref",
                "fractions",
                "ossaudiodev",
                "venv",
                "site",
                "bz2",
                "distutils",
                "aifc",
                "cmath",
                "asyncio",
                "sre_parse",
                "sqlite3",
                "base64",
                "ast",
                "pickletools",
                "chunk",
                "selectors",
                "cgitb",
                "mailcap",
                "contextvars",
                "zipfile",
                "imp",
                "reprlib",
                "crypt",
                "sysconfig",
                "builtins",
                "email",
                "tty",
                "colorsys",
                "os",
                "gc",
                "binhex",
                "atexit",
                "spwd",
                "modulefinder",
                "codecs",
                "nntplib",
                "parser",
                "encodings",
                "asyncore",
                "socket",
                "shlex",
                "cmd",
                "decimal",
                "grp",
                "subprocess",
                "array",
                "cProfile",
                "getpass",
                "sre_compile",
                "formatter",
                "compileall",
                "imghdr",
                "termios",
                "posix",
                "telnetlib",
                "smtplib",
                "secrets",
                "stat",
                "filecmp",
                "fileinput",
                "trace",
                "code",
                "pkgutil",
                "fnmatch",
                "xml",
                "ipaddress",
                "random",
                "pathlib",
                "sndhdr",
                "fpectl",
                "operator",
                "collections",
                "typing",
                "xmlrpc",
                "stringprep",
                "doctest",
                "argparse",
                "socketserver",
                "bdb",
                "copyreg",
                "zoneinfo",
                "_thread",
                "errno",
                "macpath",
                "json",
                "smtpd",
                "numbers",
                "zipapp",
                "ensurepip",
                "multiprocessing",
                "profile",
                "py_compile",
                "sched",
                "re",
                "types",
                "signal",
                "ctypes",
                "optparse",
                "difflib",
                "heapq",
                "itertools",
                "platform",
                "sre",
                "cgi",
                "lzma",
                "fcntl",
                "pydoc",
                "quopri",
                "turtle",
                "contextlib",
                "mailbox",
                "nis",
                "tokenize",
                "mimetypes",
                "gzip",
                "shutil",
                "test",
                "hashlib",
                "timeit",
                "time",
                "shelve",
                "pyclbr",
                "dataclasses",
                "tempfile",
                "netrc",
                "posixpath",
                "uuid",
                "zlib",
                "pstats",
                "ssl",
                "dbm",
                "sys",
                "traceback",
                "msvcrt",
                "statistics",
                "audioop",
                "urllib",
                "ntpath",
                "csv",
                "io",
                "unittest",
                "mmap",
                "keyword",
                "struct",
                "runpy",
                "warnings",
                "importlib",
                "dummy_threading",
                "curses"
            ],
            "extra_standard_library": [],
            "known_other": {},
            "multi_line_output": "GRID",
            "forced_separate": [],
            "indent": "    ",
            "comment_prefix": "  #",
            "length_sort": false,
            "length_sort_straight": 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,
            "group_by_package": 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,
            "color_output": false,
            "treat_comments_as_code": [],
            "treat_all_comments_as_code": false,
            "supported_extensions": [
                "py",
                "pyi",
                "pyx",
                "pxd"
            ],
            "blocked_extensions": [
                "pex"
            ],
            "constants": [],
            "classes": [],
            "variables": [],
            "dedup_headings": false,
            "only_sections": false,
            "only_modified": false,
            "combine_straight_imports": false,
            "auto_identify_namespace_packages": true,
            "namespace_packages": [],
            "follow_links": true,
            "indented_import_headings": true,
            "source": "defaults"
        },
        {
            "line_length": 95,
            "multi_line_output": "VERTICAL_HANGING_INDENT",
            "include_trailing_comma": true,
            "known_tests": [
                "tests"
            ],
            "sections": [
                "STDLIB",
                "THIRDPARTY",
                "FIRSTPARTY",
                "TESTS"
            ],
            "source": "/Users/<MASKED>/Projects/<MASKED>/<MASKED>/<MASKED>/pyproject.toml"
        }
    ],
    "virtual_env": "",
    "conda_env": "",
    "ensure_newline_before_comments": false,
    "directory": "/Users/<MASKED>/Projects/<MASKED>/<MASKED>/<MASKED>",
    "profile": "",
    "honor_noqa": false,
    "src_paths": [
        "/Users/<MASKED>/Projects/<MASKED>/<MASKED>/<MASKED>/src",
        "/Users/<MASKED>/Projects/<MASKED>/<MASKED>/<MASKED>"
    ],
    "old_finders": false,
    "remove_redundant_aliases": false,
    "float_to_top": false,
    "filter_files": false,
    "formatter": "",
    "formatting_function": null,
    "color_output": false,
    "treat_comments_as_code": [],
    "treat_all_comments_as_code": false,
    "supported_extensions": [
        "py",
        "pyi",
        "pyx",
        "pxd"
    ],
    "blocked_extensions": [
        "pex"
    ],
    "constants": [],
    "classes": [],
    "variables": [],
    "dedup_headings": false,
    "only_sections": false,
    "only_modified": false,
    "combine_straight_imports": false,
    "auto_identify_namespace_packages": true,
    "namespace_packages": [],
    "follow_links": true,
    "indented_import_headings": true
}
timothycrosley commented 3 years ago

@librehat,

It looks like your config is overriding sections, but missing the required LOCALFOLDER sections:

"sections": [
                "STDLIB",
                "THIRDPARTY",
                "FIRSTPARTY",
                "TESTS"
            ],

Could you try adding it:

"sections": [
                "STDLIB",
                "THIRDPARTY",
                "FIRSTPARTY",
                "LOCALFOLDER",
                "TESTS"
            ],

and see if this fixes your issue?

librehat commented 3 years ago

Yes that fixes the problem! Thank you! Didn't realise it was a misconfiguration issue