PyCQA / isort

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

Unrecoverable exception thrown when parsing when parsing a module #2288

Open daskol opened 2 months ago

daskol commented 2 months ago

Here is a source file which causes the issue.

# datasets/__init__.py
from .dataset import batched3, load_dataset, sample_negatives

__all__ = ('batched3', 'load_dataset', 'sample_negatives')

Here is a pre-commit configuration file (if one omits args then everything is fine).

# .pre-commit-config.yaml
repos:
  - repo: https://github.com/pycqa/isort
    rev: 5.13.2
    hooks:
      - id: isort
        args: ['-c']

Here is a stack trace produced by pre-commit hook.

ERROR: Unrecoverable exception thrown when parsing dataset/__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 "/home/username/.cache/pre-commit/repo5m26nnl8/py_env-python3.12/bin/isort", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/username/.cache/pre-commit/repo5m26nnl8/py_env-python3.12/lib/python3.12/site-packages/isort/main.py", line 1228, in main
    for sort_attempt in attempt_iterator:
                        ^^^^^^^^^^^^^^^^
  File "/home/username/.cache/pre-commit/repo5m26nnl8/py_env-python3.12/lib/python3.12/site-packages/isort/main.py", line 1212, in <genexpr>
    sort_imports(  # type: ignore
  File "/home/username/.cache/pre-commit/repo5m26nnl8/py_env-python3.12/lib/python3.12/site-packages/isort/main.py", line 87, in sort_imports
    incorrectly_sorted = not api.check_file(file_name, config=config, **kwargs)
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/username/.cache/pre-commit/repo5m26nnl8/py_env-python3.12/lib/python3.12/site-packages/isort/api.py", line 338, in check_file
    return check_stream(
           ^^^^^^^^^^^^^
  File "/home/username/.cache/pre-commit/repo5m26nnl8/py_env-python3.12/lib/python3.12/site-packages/isort/api.py", line 264, in check_stream
    changed: bool = sort_stream(
                    ^^^^^^^^^^^^
  File "/home/username/.cache/pre-commit/repo5m26nnl8/py_env-python3.12/lib/python3.12/site-packages/isort/api.py", line 210, in sort_stream
    changed = core.process(
              ^^^^^^^^^^^^^
  File "/home/username/.cache/pre-commit/repo5m26nnl8/py_env-python3.12/lib/python3.12/site-packages/isort/core.py", line 153, in process
    sort_section_pointer += output_stream.write(sorted_code)
TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'