peterjc / flake8-black

flake8 plugin to run black for checking Python coding style
MIT License
165 stars 10 forks source link

Catch bad TOML files; allow using no TOML file #15

Closed peterjc closed 5 years ago

peterjc commented 5 years ago

(Re-submission of #14)

Builds on #11, adds new BLK997 if a pyproject.toml file is invalid, allows using flake8 --black-config '-' ... to mean ignore any pyproject.toml file.

Adds explicit failure during configuration parsing for specified TOML file missing (#12) or invalid, although currently an ugly traceback.

peterjc commented 5 years ago

What do you think @098799 ?

098799 commented 5 years ago

From what I see in my debugger, if you leave black-config = with empty string, you get options.black_config to be equal to empty string, while if you leave no option specified, you get the default specified in add_options(). I see that you've changed the default to empty string -- in this case indeed there's no difference, but if you stayed with None, you can leverage that to see if the user added black-config = or not.

peterjc commented 5 years ago

I had tried None but it seemed to turn into empty string anyway

098799 commented 5 years ago
[flake8]
black-config =

produces:

ipdb> options.black_config                                                                                                             
''
ipdb> options.black_config is None                                                                                                     
False

while

[flake8]

produces

ipdb> options.black_config                                                                                                             
ipdb> options.black_config is None                                                                                                     
True

Btw. setting it to empty string as in:

[flake8]
black-config = ''

generates yet another result:

ipdb> options.black_config                                                                                                             
"''"

(I'm debugging inside parse_options and using my branch)

peterjc commented 5 years ago

Does including normalize_paths=True change things?

https://gitlab.com/pycqa/flake8/issues/561

098799 commented 5 years ago

I'm not sure how to make it work. The only example I found (for "--exclude") was a comma_separated_list option. If I use it, e.g.

        parser.add_option(
            "--black-config",
            default="",
            comma_separated_list=True,
            parse_from_config=True,
            normalize_paths=True,
            help=(
                "Path to black configuration file"
                "(overrides the default pyproject.toml)"
            ),
        )

(doesn't matter what I put in default, can be "", [] and None) I get the same empty list whether or not I define black-config = or not.

The big difference with normalize_paths=True is that if I set a relative path, it will normalize it to absolute relative to the place where flake8 command was executed, which is what we've rejected at the beginning of my PR.

peterjc commented 5 years ago

That result with normalize_paths=True sounds like it should become a flake8 bug report, especially in the light of https://gitlab.com/pycqa/flake8/issues/561

And probably I am making life too complicated by trying to support a --black-config ... way to say ignore all the possible TOML files. I'll take another stab at this tomorrow.

Your input has been valuable, thank you.

peterjc commented 5 years ago

Solved the default value None vs empty string problem by removing type="string", which was turning a default None value into an empty string.

peterjc commented 5 years ago

Another attempt pending...