hakancelikdev / unimport

:rocket: The ultimate linter and formatter for removing unused import statements in your code.
https://unimport.hakancelik.dev/
MIT License
239 stars 22 forks source link

[BUG] - KeyError: 'requirements' #283

Closed Someone894 closed 1 year ago

Someone894 commented 1 year ago

Since version 0.14.0 I'm experience some kind of bug I can't get my head around. I am using unimport as part of my pre-commit hooks with this configuration:

  - repo: https://github.com/hakancelik96/unimport
    rev: 0.14.1
    hooks:
      - id: unimport
        name: unimport
        description: A linter, formatter for finding and removing unused import statements
        args:
          - -r

on Win10 with Win-python, which leads to this error:

unimport.................................................................Failed
- hook id: unimport
- exit code: 1

Traceback (most recent call last):
  File "C:\Users\Public\WPy64-31080\python-3.10.8.amd64\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Public\WPy64-31080\python-3.10.8.amd64\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\user\.cache\pre-commit\reporfro0p4i\py_env-python3\Scripts\unimport.EXE\__main__.py", line 7, in <module>
  File "C:\Users\user\.cache\pre-commit\reporfro0p4i\py_env-python3\lib\site-packages\unimport\__main__.py", line 6, in main
    main = Main.run()
  File "C:\Users\user\.cache\pre-commit\reporfro0p4i\py_env-python3\lib\site-packages\unimport\main.py", line 107, in run
    self = cls(argv)
  File "<string>", line 4, in __init__
  File "C:\Users\user\.cache\pre-commit\reporfro0p4i\py_env-python3\lib\site-packages\unimport\main.py", line 39, in __post_init__
    self.config = self.argv_to_config()
  File "C:\Users\user\.cache\pre-commit\reporfro0p4i\py_env-python3\lib\site-packages\unimport\main.py", line 46, in argv_to_config
    return ParseConfig.parse_args(
  File "C:\Users\user\.cache\pre-commit\reporfro0p4i\py_env-python3\lib\site-packages\unimport\config.py", line 196, in parse_args
    config_context = cls(Path(path)).parse()
  File "C:\Users\user\.cache\pre-commit\reporfro0p4i\py_env-python3\lib\site-packages\unimport\config.py", line 135, in parse
    return getattr(self, f"parse_{self.config_file.suffix.strip('.')}")()
  File "C:\Users\user\.cache\pre-commit\reporfro0p4i\py_env-python3\lib\site-packages\unimport\config.py", line 166, in parse_cfg
    key_type = config_annotations_mapping[key]
KeyError: 'requirements'
Traceback (most recent call last):
  File "C:\Users\Public\WPy64-31080\python-3.10.8.amd64\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Public\WPy64-31080\python-3.10.8.amd64\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\user\.cache\pre-commit\reporfro0p4i\py_env-python3\Scripts\unimport.EXE\__main__.py", line 7, in <module>
  File "C:\Users\user\.cache\pre-commit\reporfro0p4i\py_env-python3\lib\site-packages\unimport\__main__.py", line 6, in main
    self = cls(argv)
  File "<string>", line 4, in __init__
  File "C:\Users\user\.cache\pre-commit\reporfro0p4i\py_env-python3\lib\site-packages\unimport\main.py", line 39, in __post_init__
    self.config = self.argv_to_config()
  File "C:\Users\user\.cache\pre-commit\reporfro0p4i\py_env-python3\lib\site-packages\unimport\main.py", line 46, in argv_to_config
    return ParseConfig.parse_args(
  File "C:\Users\user\.cache\pre-commit\reporfro0p4i\py_env-python3\lib\site-packages\unimport\config.py", line 196, in parse_args
    config_context = cls(Path(path)).parse()
  File "C:\Users\user\.cache\pre-commit\reporfro0p4i\py_env-python3\lib\site-packages\unimport\config.py", line 135, in parse
    return getattr(self, f"parse_{self.config_file.suffix.strip('.')}")()
  File "C:\Users\user\.cache\pre-commit\reporfro0p4i\py_env-python3\lib\site-packages\unimport\config.py", line 166, in parse_cfg
    key_type = config_annotations_mapping[key]
KeyError: 'requirements'

Adding the --disable-auto-discovery-config option fixes the problem, while generating a requirements.txt (I thought the system may needs it, since the missing key is called 'requirements') does not affect the issue.

Are there any additional information I can provide to assist you in this issue?

hakancelikdev commented 1 year ago

Thanks for the issue, I will work on it!

hakancelikdev commented 1 year ago

@Someone894 Can you try again using as shown below?

repos:
  - repo: https://github.com/hakancelikdev/unimport
    rev: 0.14.1
    hooks:
      - id: unimport
hakancelikdev commented 1 year ago

Can you share your setup.cfg configuration file ?

Someone894 commented 1 year ago

Sorry for the delay, I was on vacation.

@Someone894 Can you try again using as shown below?

repos:
  - repo: https://github.com/hakancelikdev/unimport
    rev: 0.14.1
    hooks:
      - id: unimport

Here is the pre-commit run with the new hook you provided:

 pre-commit run --all-files
[INFO] Initializing environment for https://github.com/hakancelikdev/unimport.
[INFO] Installing environment for https://github.com/hakancelikdev/unimport.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
unimport.................................................................Failed
- hook id: unimport
- exit code: 1

Traceback (most recent call last):
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/bin/unimport", line 8, in <module>
    sys.exit(main())
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/__main__.py", line 6, in main
    main = Main.run()
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/main.py", line 107, in run
    self = cls(argv)
  File "<string>", line 4, in __init__
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/main.py", line 39, in __post_init__
    self.config = self.argv_to_config()
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/main.py", line 46, in argv_to_config
    return ParseConfig.parse_args(
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/config.py", line 196, in parse_args
    config_context = cls(Path(path)).parse()
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/config.py", line 135, in parse
    return getattr(self, f"parse_{self.config_file.suffix.strip('.')}")()
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/config.py", line 166, in parse_cfg
    key_type = config_annotations_mapping[key]
KeyError: 'requirements'
Traceback (most recent call last):
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/bin/unimport", line 8, in <module>
    sys.exit(main())
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/__main__.py", line 6, in main
    main = Main.run()
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/main.py", line 107, in run
    self = cls(argv)
  File "<string>", line 4, in __init__
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/main.py", line 39, in __post_init__
    self.config = self.argv_to_config()
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/main.py", line 46, in argv_to_config
    return ParseConfig.parse_args(
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/config.py", line 196, in parse_args
    config_context = cls(Path(path)).parse()
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/config.py", line 135, in parse
    return getattr(self, f"parse_{self.config_file.suffix.strip('.')}")()
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/config.py", line 166, in parse_cfg
    key_type = config_annotations_mapping[key]
KeyError: 'requirements'
Traceback (most recent call last):
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/bin/unimport", line 8, in <module>
    sys.exit(main())
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/__main__.py", line 6, in main
    main = Main.run()
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/main.py", line 107, in run
    self = cls(argv)
  File "<string>", line 4, in __init__
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/main.py", line 39, in __post_init__
    self.config = self.argv_to_config()
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/main.py", line 46, in argv_to_config
    return ParseConfig.parse_args(
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/config.py", line 196, in parse_args
    config_context = cls(Path(path)).parse()
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/config.py", line 135, in parse
    return getattr(self, f"parse_{self.config_file.suffix.strip('.')}")()
  File "/root/.cache/pre-commit/repobbrgosrf/py_env-python3/lib/python3.10/site-packages/unimport/config.py", line 166, in parse_cfg
    key_type = config_annotations_mapping[key]
KeyError: 'requirements'

Can you share your setup.cfg configuration file ?

Here is my setup.cfg:

[.mypy]
follow_imports = silent
strict_optional = True
warn_redundant_casts = True
disallow_any_generics = True
check_untyped_defs = True
warn_unused_ignores = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
disallow_untyped_calls = True
show_column_numbers = True
show_error_codes = True

[mypy-pandas]
ignore_missing_imports = True

[mypy-pytest]
ignore_missing_imports = True

[isort]
profile = black

[black]
line-length = 100
atomic = true

[flake8]
max-line-length = 100
max-complexity = 18
docstring-convention = google
extend-ignore = E501
per-file-ignores =
__init__.py: F401

[unimport]
remove = true
requirements = true
include-star-import = true
ignore-init = true
gitignore = true

[darglint]
docstring_style=google

I hope this helps and if I can do something else to support you let me know it.

hakancelikdev commented 1 year ago

requirements feature has been removed, can you try again after remove requirements=true from your config.

@Someone894

Someone894 commented 1 year ago

Sorry, its hard to stay up to date with all the changes . . . This actually lead to a different error message:

pre-commit run --all-files
unimport.................................................................Failed
- hook id: unimport
- exit code: 1

Traceback (most recent call last):
  File "C:\Users\Public\WPy64-31080\python-3.10.8.amd64\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Public\WPy64-31080\python-3.10.8.amd64\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\u0101897\.cache\pre-commit\repoukgr2pnh\py_env-python3\Scripts\unimport.EXE\__main__.py", line 7, in <module>
  File "C:\Users\u0101897\.cache\pre-commit\repoukgr2pnh\py_env-python3\lib\site-packages\unimport\__main__.py", line 6, in main
    main = Main.run()
  File "C:\Users\u0101897\.cache\pre-commit\repoukgr2pnh\py_env-python3\lib\site-packages\unimport\main.py", line 107, in run
    self = cls(argv)
  File "<string>", line 4, in __init__
  File "C:\Users\u0101897\.cache\pre-commit\repoukgr2pnh\py_env-python3\lib\site-packages\unimport\main.py", line 39, in __post_init__
    self.config = self.argv_to_config()
  File "C:\Users\u0101897\.cache\pre-commit\repoukgr2pnh\py_env-python3\lib\site-packages\unimport\main.py", line 46, in argv_to_config
    return ParseConfig.parse_args(
  File "C:\Users\u0101897\.cache\pre-commit\repoukgr2pnh\py_env-python3\lib\site-packages\unimport\config.py", line 196, in parse_args
    config_context = cls(Path(path)).parse()
  File "C:\Users\u0101897\.cache\pre-commit\repoukgr2pnh\py_env-python3\lib\site-packages\unimport\config.py", line 135, in parse
    return getattr(self, f"parse_{self.config_file.suffix.strip('.')}")()
  File "C:\Users\u0101897\.cache\pre-commit\repoukgr2pnh\py_env-python3\lib\site-packages\unimport\config.py", line 166, in parse_cfg
    key_type = config_annotations_mapping[key]
KeyError: 'include-star-import'
Traceback (most recent call last):
  File "C:\Users\Public\WPy64-31080\python-3.10.8.amd64\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Public\WPy64-31080\python-3.10.8.amd64\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\u0101897\.cache\pre-commit\repoukgr2pnh\py_env-python3\Scripts\unimport.EXE\__main__.py", line 7, in <module>
  File "C:\Users\u0101897\.cache\pre-commit\repoukgr2pnh\py_env-python3\lib\site-packages\unimport\__main__.py", line 6, in main
    main = Main.run()
  File "C:\Users\u0101897\.cache\pre-commit\repoukgr2pnh\py_env-python3\lib\site-packages\unimport\main.py", line 107, in run
    self = cls(argv)
  File "<string>", line 4, in __init__
  File "C:\Users\u0101897\.cache\pre-commit\repoukgr2pnh\py_env-python3\lib\site-packages\unimport\main.py", line 39, in __post_init__
    self.config = self.argv_to_config()
  File "C:\Users\u0101897\.cache\pre-commit\repoukgr2pnh\py_env-python3\lib\site-packages\unimport\main.py", line 46, in argv_to_config
    return ParseConfig.parse_args(
  File "C:\Users\u0101897\.cache\pre-commit\repoukgr2pnh\py_env-python3\lib\site-packages\unimport\config.py", line 196, in parse_args
    config_context = cls(Path(path)).parse()
  File "C:\Users\u0101897\.cache\pre-commit\repoukgr2pnh\py_env-python3\lib\site-packages\unimport\config.py", line 135, in parse
    return getattr(self, f"parse_{self.config_file.suffix.strip('.')}")()
  File "C:\Users\u0101897\.cache\pre-commit\repoukgr2pnh\py_env-python3\lib\site-packages\unimport\config.py", line 166, in parse_cfg
    key_type = config_annotations_mapping[key]
KeyError: 'include-star-import'
hakancelikdev commented 1 year ago

@Someone894 Your configuration is miswritten, please check out this page -> https://unimport.hakancelik.dev/0.15.0/tutorial/configurations/

in short, you should change the below lines.

include-star-import -> include_star_import
ignore-init -> ignore_init
hakancelikdev commented 1 year ago

Maybe I can open a new issue to support as seen on your configuration.

If I can support both, the unimport's configuration may feature more powerfully.

Thanks for the new idea.

hakancelikdev commented 1 year ago

Hi, I have great news for you.

I have published a new version of unimport 0.16.0, Now Unimport supports configurations like include-star-import or ignore-init, check out -> https://unimport.hakancelik.dev/0.16.0/tutorial/configurations/#manage-like-cli-in-configuration

Someone894 commented 1 year ago

I'm sorry for the trouble. Some time ago I went ahead and copied the config over from another project without researching every line in it, and it worked in the sense, that there where no reported errors and it did what I wanted it to, maybe the settings where just ignored. However, I just checked your changes with the _ instead of - and they work fine. Then I went ahead and checked the 0.16 version, which also works fine. Thank you for releasing the 0.16 version, this way I don't have to change up several projects. And thank you for your help in general :-)

hakancelikdev commented 1 year ago

You're welcome, it was great reporting your problem, if you have any problems again, please don't hesitate and open it as an issue.