SublimeLinter / SublimeLinter-flake8

SublimeLinter plugin for python, using flake8.
MIT License
184 stars 28 forks source link

Is --per-file-ignores supported for flake8 >= 3.7.0? #113

Closed willcroft closed 5 years ago

willcroft commented 5 years ago

I'm trying to use the --per-file-ignores argument introduced in flake8 3.7.0 through a .flake8 config file.

The top-level folder open in Sublime Text is the project with a .flake8 file at the root. I've confirmed that project-wide settings work as expected, such as:

[flake8]
ignore = E501

But when it comes to setting per-file exclusions, the setting doesn't seem to propagate to the linter:

[flake8]
per-file-ignores =
    transformations/transforms.py:E501

Looking at the console output, the file is run through cat and piped to flake8 which perhaps means the file path is lost?

/Users/foo/bar/project  (working dir)
  $ cat transformations/transforms.py | /usr/local/miniconda3/envs/flake8/bin/flake8 --format default -

SublimeLinter: #5 linter.py:1155      flake8: output:
  stdin:34:100: E501 line too long (117 > 99 characters)

The environment has flake8==3.7.8 installed.

kaste commented 5 years ago

Interesting new feature. You could try on the command line if using flake8 --stdin-display-name could work here.

kaste commented 5 years ago

Just in a shell. If it works there we can think about how we could make this work in SublimeLinter.

willcroft commented 5 years ago

Yup, that seems to do the trick:

cat transformations/transforms.py | /usr/local/miniconda3/envs/flake8/bin/flake8 --format default --stdin-display-name transformations/transforms.py -

(No output returned, as in, the linting is passing. Removing the file exception from my .flake8 config results in linting errors, as expected.)

kaste commented 5 years ago

Well, then you can just tell SublimeLinter to do the same using the args setting.

Ref http://www.sublimelinter.com/en/stable/linter_settings.html#args and http://www.sublimelinter.com/en/stable/settings.html#settings-expansion

"args": ["--stdin-display-name", "${file_path:stdin}"]

🤞

file_path holds the absolute path to that file. Hope flake8 is okay with that. :stdin means: for unsaved, new files default to 'stdin'.

willcroft commented 5 years ago

Yup, just found the same thing for myself, thanks! A slight tweak as ${file_path} seems to hold the directory only? So I added to my settings:

"args": [
    "--stdin-display-name=${file}"
]

It provides a full file path, as I didn't see a Sublime variable for a project-relative path, though flake8 seems to be just fine with this. (And thanks for the :stdin trick, will add that too.)

kaste commented 5 years ago

Sure file it is 🤦

willcroft commented 5 years ago

Final working solution, for reference: "args": ["--stdin-display-name", "${file:stdin}"]. Thanks @kaste!

kaste commented 5 years ago

I now see that we have something similar in the Readme. But we have a def better snippet now with the fallback.

The Readme reads a bit rough as well after the last contributions so I would appreciate a small review PR from a native writer. 😏

willcroft commented 5 years ago

@kaste if you’d welcome the contribution, I’d be happy to take a look! Unless you already have someone on the team who would prefer to take this on?

kaste commented 5 years ago

Would love a contribution!

willcroft commented 5 years ago

Submitted #114! Hope it's useful!