john-hen / Flake8-pyproject

Flake8 plug-in loading the configuration from pyproject.toml
https://pypi.org/project/Flake8-pyproject
MIT License
218 stars 10 forks source link

Override `flake8` script? #5

Closed woile closed 2 years ago

woile commented 2 years ago

Hello, thanks for the project, it looks good! I was wondering if this app could take over the flake8 script. This way everything with work in integration with pyproject.toml Currently one has to update scripts to use flake8p over flake8, then update the IDE to use flake8p instead of flake8, and then document and expect all other team members to do the same on their IDEs (which if they use the same as me I can give support, but if they use another it's harder).

Let me know what you think of this feature, I can also submit a PR.

Regards!

john-hen commented 2 years ago

I like the idea, I just don't know how we would make it work. If two packages define the command as an entry point, here flake8, then wouldn't it depend on install order which package gets called?

woile commented 2 years ago

That's a very good question that I don't know the answer haha I think ppl from flake9 are directly modifying the flake8 source 🤔

john-hen commented 2 years ago

Yeah, flake9 seems to have vendored flake8, that's how they do it.

john-hen commented 2 years ago

Maybe there is a way to do this if we get Flake8 to run the code here as a plug-in. Then we could patch it live whenever the regular flake8 entry point is invoked. I think. I haven't quite figured out yet how to do it and if it'll actually work.

vcalvert commented 2 years ago

Maybe there is a way to do this if we get Flake8 to run the code here as a plug-in. Then we could patch it live whenever the regular flake8 entry point is invoked. I think. I haven't quite figured out yet how to do it and if it'll actually work.

This caused me to do a bit of code-diving, the result is in #8.

john-hen commented 2 years ago

@vcalvert As far as I can tell, your code does not work as is.

I played around with this a bit more, but I think (and I could be wrong) that as of Flake8 5.0, plug-ins are loaded after the configuration files, whereas it was the other way around before. (Again, I could be wrong.) So by the time the plug-ins are loaded, it might be too late to monkey-patch the loading of the configuration files. See Application.initialize().

But there's another step that happens after the plug-ins are loaded, when Flake8 calls [parse_configuration_and_cli()], which ultimately calls options.aggregator.aggregate_options(). Since that happens after the configuration files are loaded, I think we could hook in there with a plug-in.

However, the behavior may be somewhat different then. This "merges" options with whatever was previously loaded. So that could mean configuration files not named pyproject.toml in parent folders of the current working directory. Which doesn't sound so bad, but is different from what the flake8p entry point currently does: It just looks at pyproject.toml if it finds it in the working directory, but then disregards all other files. Might not be a deal-breaker, just a difference. And also, as you mentioned in the PR, this might be the behavior Flake8 would implement eventually, via "configuration plug-ins". So maybe this is the right approach. I just haven't made up my mind yet.

[parse_configuration_and_cli()]: https://github.com/PyCQA/flake8/blob/6027577d325b0dd8bf1e465ebd29b71b5f0d005b/src/flake8/main/application.py#L142