google / yapf

A formatter for Python files
Apache License 2.0
13.75k stars 887 forks source link

Allow setting config section #509

Open Martmists-GH opened 6 years ago

Martmists-GH commented 6 years ago

A lot of people want to have multiple linters, including yapf. However, it only searched for the [yapf] group when searching setup.cfg, but some people want a different file, most likely for a tool to wrap it.

Please set [yapf] as default, or allow for choosing a section in this case.

bwendling commented 6 years ago

[yapf] is the default section it looks for. :-) Are you suggesting a flag to allow people to choose, say, [yapf-testing] as the section instead of [yapf]?

Martmists-GH commented 6 years ago

Well, in my project, SnekChek, using a [yapf]section in .snekrc failed to work, claiming there was no [style] section.

bwendling commented 6 years ago

The code in question (_CreateStyleFromConfigParser in the yapf/yapflib/style.py file) looks for [yapf] first. If it doesn't find it, then it defaults to [style]. It might be that if you don't have either of these sections, then YAPF will complain.

Martmists-GH commented 6 years ago

odd, when I did have [yapf] it complained...

   mart@proton   /git/snekchek     53c387  master  
   cat .snekrc
[all]
linters = flake8, pylint, yapf, isort, pyroma, safety, dodgy, vulture, pytest, pypi

[flake8]
max-line-length=100
exclude=build,tests
ignore=I100,I101,I201,B101,I900

[isort]
line_length=100
indent='    '
multi_line_output=0
length_sort=1
use_parentheses=true
sections=FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
import_heading_stdlib=Stdlib
import_heading_thirdparty=External Libraries
import_heading_firstparty=Snekchek
force_sort_within_sections=true

[vulture]
min-confidence=0

[pypi]  # needed for twine

[yapf]  # keep empty for now

[pytest]
testpaths=tests

   mart@proton   /git/snekchek     53c387  master  
   python -m snekchek                     
Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/mart/git/snekchek/snekchek/__main__.py", line 100, in <module>
    main()
  File "/home/mart/git/snekchek/snekchek/__main__.py", line 96, in main
    run_main(args)
  File "/home/mart/git/snekchek/snekchek/__main__.py", line 53, in run_main
    handler.run_linter(style())
  File "/home/mart/git/snekchek/snekchek/structure.py", line 109, in run_linter
    linter.run(self.files)
  File "/home/mart/git/snekchek/snekchek/style.py", line 73, in run
    file, style_config=self.confpath)
  File "/usr/lib/python3.6/site-packages/yapf/yapflib/yapf_api.py", line 90, in FormatFile
    verify=verify)
  File "/usr/lib/python3.6/site-packages/yapf/yapflib/yapf_api.py", line 123, in FormatCode
    style.SetGlobalStyle(style.CreateStyleFromConfig(style_config))
  File "/usr/lib/python3.6/site-packages/yapf/yapflib/style.py", line 450, in CreateStyleFromConfig
    config = _CreateConfigParserFromConfigFile(style_config)
  File "/usr/lib/python3.6/site-packages/yapf/yapflib/style.py", line 495, in _CreateConfigParserFromConfigFile
    'Unable to find section [style] in {0}'.format(config_filename))
yapf.yapflib.style.StyleConfigError: Unable to find section [style] in .snekrc

After editing the file to be [style], however, it DOES work.

bwendling commented 6 years ago

Ah! I think it's because your yapf section is empty. We're unable to distinguish between it being empty and it not existing.

jacobeatsspam commented 4 years ago

@gwelymernans I'm going to boldly hijack a two year old issue and suggest that yapf config options, instead of just style options, should be exposed/pulled from setup.cfg as well. For example, there is no way to configure excludes for yapf in setup.cfg right now. Instead a special file has to be made, which differs from most other projects.