Flake8 is a command-line tool for linting Python projects. By default, it includes lint checks provided Pyflakes, pycodestyle, and McCabe But what does all of that mean? Anthony Sottile is a maintainer of flake8 and has kindly offered to explain it to us.

Transcript for episode 156 of the Test & Code Podcast

This transcript starts as an auto generated transcript.
PRs welcome if you want to help fix any errors.

00:00:00 Placate is a command line tool for Linting Python projects. By default it includes Lint checks provided by Pi, Flakes, Pi Code Style and McCabe. What does all that mean? Anthony Sotilly is a maintainer of Placate and has kindly offered to explain it all to us. Anthony Config Cat is a feature flag service. Easily used flags in your code with Config Cat libraries toggle your feature, flags visually on a dashboard, hide or expose features in your application without redeploying code, set targeting rules to allow you to control who has access to the new features. It allows you to get features out faster, test in production, and do easy rollbacks. With Config Cat simple API and clear documentation, you’ll have your initial proof of concept up and running in minutes. Whether you’re an individual or a team. You can try it out with their forever free plan or get 25% off any paid plan with the Code Test and Code 2021 release. Features faster with less risk with ConfigCat. Check them out today at configcat.com.

00:01:15 Welcome to Test and Code.

00:01:25 So today on Test and Code, I am thrilled again to have Anthony So Tilly on, and I just practiced his last name again before we had came on because for some reason, I don’t know, it’s a blocker for me, but it’s a cool name.

00:01:37 It’s tricky.

00:01:38 Anthony, thanks for coming back.

00:01:40 Yeah, good to be on here.

00:01:41 I was reminded recently that you maintain or help maintain, or at the very least are involved with a whole bunch of different cool projects.

00:01:51 Just a few.

00:01:52 And then we’re just going to Zoom in to just a few of these.

00:01:56 So we’re going to talk about Blake Aid and a bunch of related tools. What’s Flake Eight?

00:02:03 So the idea behind Flake Eight is to take some common well, first provide a common substrate to build Linting tools on top of, but it also combines the three, well, two plus one other that’s optional.

00:02:19 The two most popular printers in Python. Those are Pi Flakes and Pi Code Style, which used to be called Pepe. But Guido said please Rename and So man, that was a pain.

00:02:31 It was renamed to Pi Code Style and not Pepa.

00:02:36 So what Flaky does is it basically takes it provides a plugin interface such that anyone can write whatever Linter that they want on top of it. It provides easy exclusion and inclusion and Recursion like file system. Recursion also has no QA, so you can ignore Lint failures as configuration so that you can set up your particular set of tools and stuff. And it’ll work for anyone that clones that and sets up the right tools.

00:03:10 And it also provides metadata to plugins and pre commit so you can use it with that.

00:03:16 But the idea behind Flight Gate is to be a plug in framework for Renters, okay.

00:03:24 It actually doesn’t implement any of its own checks. All of the checks for it come from the underlying tools.

00:03:31 Pyflix provides kind of programmatic correctness checks, things like missing variable names or extra imports or stuff like that, whereas Pico style tends to lean more towards the white space sort of checks. Did you indent this properly? According to Pepe, always.

00:03:51 Pepe is a living and breathing document that has flip flopped on quite a few issues back and forth over the years. So Pine Code Style’s view of the world is some snapshot of some parts of Pep bait. I guess in a way, like renaming it away from Pepa is a good idea. But yeah, it’s gone back and forth on several rules several times, so it has a particular set of opinions that are encoded most of the time. When Pep eight flipflops, we just disable both rules and then you can pick one.

00:04:24 Okay, now I’m curious. Can we talk about at least one rule that got flip flopped?

00:04:29 Sure. The one that people run into the most is W 504 and W 503, which on their own mean nothing. But it is the placement of a binary operator when you have an expression split across multiple lines.

00:04:43 The Pep originally said that the operator should go at the end of the line so that all of the expressions line up, but it has been changed recently so that the operators are supposed to be at the beginning of mine so that all the operators line up.

00:04:57 I know which style I prefer, and I impose that on people, but I actually don’t like the change that was made there.

00:05:04 I like to line up all the operands personally so that you can see each expression in a row. And this also happens for boolean expressions too. So if you have like comparison and comparison and comparison. And of course the and would be at the beginning of the line if you’re adhering to the new Papa’s style and the and would be at the end of the line if you’re adhering to the old paid style.

00:05:26 Okay, and I’m just going to guess that operand is not the operator. So operand is the thing that’s not the operator.

00:05:36 The thing that’s really annoying about this is because Pi code style has both of these lent rules. It’s really easy to misconfigure and enable both of them at the same time, and then have it be impossible to satisfy the winter.

00:05:52 What’s the most common way or preferred way to enable a different rule then? Like if I wanted to enable one of those.

00:06:01 Yeah. So Flake Eight has a few settings for enabling and disabling particular rules. The easiest or the most common one is at the top level. There is a Flakegate configuration in either setup, CFG Flake Eight or talks Ini depending on which file you want to put it in. And it has include exclude, include an excluded for files there’s select and ignore and extend ignore for particular Lint rules.

00:06:33 And usually you don’t have to touch select. Usually select is on its own, it will enable your plugins and enable the default set of checks for you.

00:06:43 Mostly I suggest using Extend Ignore instead of Ignore. And the difference between the two is Extend Ignore will keep the default Ignore set, which includes all of those mutually exclusive rules. Both of them are off. Of course, if you want to turn one of them on and not the other one, you have to enumerate all the specific things you want to ignore, which is kind of annoying. It would be nice if there was an Extend Select, but we haven’t implemented it yet because the configuration story in Flakate is not great.

00:07:14 It really needs a big refactor to be much more accessible to users.

00:07:19 Okay, so I can do an Extend Ignore exists, but Extend Select doesn’t, right?

00:07:25 There’s a patch to add it, but it needs a little bit of work to get landed.

00:07:29 So is there a way to just find out what all the default selects are?

00:07:34 Yeah, if you do click a help, it will print it out in the little help list.

00:07:39 Okay, so if I wanted to select my own stuff, then a similar thing to Select Ignore would be to dump that and then start from there and then add to that list.

00:07:50 Yeah, that would totally work. Okay, the other thing is, anytime you add a plugin, it will automatically get added as well, assuming the plugin has set up their metadata correctly. There’s a few buggy plugins which claim they support one Lint rule and then output a different one.

00:08:06 Okay, I’ve been meaning to fix that as well, because that should produce an error so that the plugin author knows to fix it, but really doesn’t do that.

00:08:22 This episode is sponsored by Data Dog, the monitoring and security platform for cloud scale applications. Datadog integrates seamlessly with more than 450 technologies, so you can track every layer of your complex microservices architecture in one place. Data Dogs, distributed Tracing and continuous code profiling for Python applications provide end to end visibility into every request down to the line of code across hosts, containers, services, and databases. With rich dashboards, machine learningbased insights and alerts, and automated actionable insights, Datadog lets you build, test, and ship your applications with peace of mind. See for yourself. Start a 14 day free trial today and Datadog will send you a free T shirt. Visit Test And Code. Comdatedog to get started.

00:09:15 Tell me again, the default ones. There’s Pi Flakes. There’s Pi Code Style.

00:09:20 Yeah, there’s Pi Flakes, which is correctness checking. There’s Pi Code Style, which is white space and Pep paint checks. And then there’s McCabe, which is enabled by default. But it doesn’t error by default. And that is a complexity checker, right?

00:09:35 Basically, Cyclomatic complexity based on if statements and try statements and loops in functions.

00:09:43 And I occasionally know what that means.

00:09:47 I usually turn it off because I find the Cyclimatic complexity. So here’s my experience with Cyclomatic complexity on every repository that I’ve worked on that has the Cyclomatic complexity checker at work or whatever. If somebody exceeds the cyclonetic check, like, say, the threshold is set at ten.

00:10:06 I’ve seen only two responses to this, and one is to bump the number to eleven and the other is to no QA that function. I’ve never seen somebody actually split up a function that meets that fails a cyclonetic check, and if they do split up the function, it often makes the code less readable because you’re not actually taking it and moving it into reusable parts. You’re just splitting the code to satisfy a lender. So in my opinion, Cyclomatic complexity checkers are not actually useful tools in the tools, but to each their own.

00:10:37 Okay, so we’ll have to have another Anthony on sometime to take the other opinion because.

00:10:47 They can be useful for identifying complex pieces of code and like providing potential points to see, refactors or recognize that, oh, this piece of functionality does way too much. It might be really hard to maintain, but I think the common, quick, easy fixes for it actually make the code worse.

00:11:08 So I always get confused with the flight gate thing because I think it’s a collection of eight tools. Why is the eight there? Is it because of Pep eight?

00:11:16 Yeah.

00:11:17 So the name Flake used to be a mashed together of Pi, Flakes and Pepe. Okay, but now that Pepe is called Pi Coat style, maybe it should be called Flake Coatstyle or something ridiculous, but yeah, it no longer bears the eight in its name.

00:11:36 Okay, so that’s where the eight comes from.

00:11:39 You can add eight tools to it. You can add as many plugins as you want.

00:11:42 Sometimes I forget that that’s one of the nice things is to look at all the different plugins that are around. How do I find plugins for it?

00:11:50 There’s a classifier on pipe. That’s one way to find it. I usually just search Flight gate in the Pipe search. Of course, you can’t use Pip Search because it’s disabled right now, but you can still search on the pipe page.

00:12:04 That’s how I usually do it anyway.

00:12:08 Is it normally Flaked something?

00:12:12 Yeah, it’s usually Flaky something. However, there are a few others that don’t quite fall into that. Like there’s an open stack project called Packing. There’s the we make style guide, which is, as far as I know, the largest and most complicated collection of plugins. It’s something like 30 or 40 plugins in one sort of mega package.

00:12:34 That’s what we make.

00:12:37 Yeah, right.

00:12:40 Okay.

00:12:43 They have a very particular way that they like it to be written, which is good. If you want a very strict style guide, that’s one pick up.

00:12:51 You could also use it as an example of how to specify a very picky style.

00:12:57 Yeah, for sure.

00:12:59 So that’s cool.

00:13:02 Do you know if you can put the configuration in the pipe project Tumble?

00:13:10 There’s a very contentious issue about this currently.

00:13:17 So I currently said no because the configuration story and Flick, it’s significantly complicated at the moment and plugging it in. Plugging it in and properly supporting it is not easy or it’s not trivial to add it.

00:13:32 Okay.

00:13:34 Plus it adds another dependency and other complexity. You have to deal with package managers and other stuff like that, so it’s not easy to add it. So at the moment I’ve said no.

00:13:44 Also, it doesn’t have a clear value prop.

00:13:48 I mean, I kind of understand the benefit, like jam everything into one file, but it adds a bunch of work for me and doesn’t seem to add much from that perspective. I do plan to add it at some point. However, before we get to that point, there’s a few things that need to get solved. One of them is like it currently discovers configurations in like four places, and I would like to eliminate most of those and get it down to just project based configuration, because right now you can put up like a config eight and it’ll just work, which I don’t think anyone should do that because it makes it really hard to clone a repository to another machine and have a consistent set of Lint rules. It also finds the flicker configuration in your Hub directory. If it’s there, there’s a whole bunch of weird edge cases with configuration and ordering and layering and stuff.

00:14:40 Until that is simpler and easier to extend, it’s hard to add another configuration file and configuration format. Okay. There’s also the problem of real types in Tumult, so the current configuration is entirely Ini and everything’s a string there. So it’s easy to say like take a string person to this.

00:15:02 When you involve Tommel, you suddenly have structured data and like it doesn’t have a way to consume structured data at the moment.

00:15:08 Okay.

00:15:10 And doing everything as strings would kind of go against the idea of tumbles. I don’t want to introduce a version where everything strings and then be like JK, we’re going to switch to structured data now and just have two layers of breaking changes.

00:15:25 I think that’s a good call.

00:15:27 I’d rather do it right the first time.

00:15:28 Yeah, that’s good.

00:15:30 There’s also another weirdness with Pipe project.com that would break some users.

00:15:37 What I would want to do is say Piprote AutoML is the way to configure Flickate and deprecate all the other configuration methods. The problem is the presence of a pipeline of Tamil file changes. How Pip works, just the presence. You don’t have to have any Pip metadata in there. It just changes how Pip processes your project. And this can break people set up top pies.

00:16:01 Yeah.

00:16:02 So I want to avoid painting people into a corner based on a configuration change.

00:16:07 And that happens sometimes just when people try to start using black.

00:16:11 Yes, there’s a big locked thread on black about exactly the opposite issue of the one on which is, hey, can we have non Pipe project that Tom will config because it breaks me.

00:16:24 I don’t want to paint my users into the same corner.

00:16:26 Yeah. Okay.

00:16:29 What are some things? So Pipe code style is the sort of style guide thing that people often think about.

00:16:37 What kind of things does Pipe like? Catch?

00:16:41 Yeah. So Pi Flakes, for the most part is trying to catch correctness things. So things like unused imports or a name is referenced but it doesn’t exist, or what else does it check? Those are the most common ones that I see from it. Let me see.

00:17:05 Open it up right here and just look at the messages and scroll through that. Oh, I’ve added a bunch of checks recently around string formatting. Correctness. So, like, if you call format and you’re missing a placeholder, you have extra placeholders, it’ll warn you on that. I’ve actually seen it’s very common in error messages to call format and then forget to put the placeholders in there and just you end up with an error message that doesn’t have anything useful in it.

00:17:29 Yeah.

00:17:30 Let’s see. Unused import, redefinitions import, star used undefined names, syntax errors, duplicate arguments and functions, repeated keys and dictionaries things that would cause programming mistakes at Runtime. Errors at runtime, usually.

00:17:51 And where can I find this list of things that you’re looking at?

00:17:55 I’m actually looking at Pipelixmessages Pi, but there is a list in the Flakegate documentation of the PyFlakes error codes because Pipelix actually doesn’t have a concept of error codes.

00:18:07 Flake Eight translates them for they’re part of the Flakegate error codes. Okay, so if I go to Flake Eight homepage and somewhere.

00:18:16 Yes.

00:18:17 If you search like F 401 or Pi Flakes on Flakes documentation, you should be able to find it and just hot off the press. I have just moved Flakegate from GitLab to GitHub, so if you are searching for the source code, go to GitHub instead.

00:18:34 Okay.

00:18:37 It was a lot of work, but it’s finally done. Unfortunately, I broke a bunch of links, but there’s not really much I can do about that.

00:18:45 Well, I guess we’ll find it.

00:18:48 And then you said that if you do help.

00:18:52 Wait, help doesn’t show you all the error codes, does it?

00:18:55 No. Help will show you the default set of options. The error codes either come from the underlying tools. So you’ll look at the tools documentation or for the special case of Pi Flakes, it will be in the Flake documentation.

00:19:06 Okay. All right.

00:19:10 Yes.

00:19:11 You can plug in a whole bunch of different tools, and there’s a lot of different things you can plug into Flickate, but if you just grab it by itself, it’s very useful. Just by default, I often run it without any changes at all.

00:19:22 Yeah, I use the default configuration set and I add very few plugins, if any at all. I think the only plugin that I use somewhat frequently is on projects which provide a Typed API. I will use a plugin that I wrote called Flakegate Typing Imports because the typing module has changed a surprising amount in patch versions of Python. And it’s really easy to accidentally import something that doesn’t exist because it might work in your patch version of Python, but not in like three, six one, for instance, or 3520kay.

00:19:55 Now, does Flakate run things that can it run things that change your code, or is it just things that check your code?

00:20:04 So currently it is only checkers, and I kind of want to keep it that way. There was an Alpha quality code formatter interface, but it never really worked correctly and it didn’t provide useful primitives to write formatters. It didn’t make it easier to run the formatter, so it wasn’t really doing anything.

00:20:23 Okay. It never actually worked and no one ever actually tried to write a format or plug in. So it got deleted.

00:20:28 So like, for instance, Flake a Black is a thing, but it doesn’t change your code, it just checks to see if it would change it if you ran it. Right.

00:20:39 So I don’t actually remember there was a while where Flakey Black had a side effect of changing your code as you ran it.

00:20:49 Flakey Black is kind of one of those weird plugins that I don’t think should exist. In my opinion. Flake Black is kind of this weird Gray area of plugins that I don’t think should exist. There’s also a pytest Black, too, which I also think is kind of weird in my opinion.

00:21:05 Black is good enough at running Black that you should just run Black directly and plugging it into Flake. It seems to be like it works, but it doesn’t like a weird Daisy chaining of tools together. Yeah.

00:21:18 Pyte Black is also kind of odd. And then also there’s a I think there’s a flight Cape pytest, maybe.

00:21:25 Yeah. So what you can do is you can use pytest to run Flake Eight to run Black if you really wanted to. I don’t know why you would, but you could if you wanted to.

00:21:33 Okay. So don’t do that if you want to run a whole bunch of different stuff. Well, one of the things you can do is you can talks.

00:21:40 Yeah. Talks is another good choice.

00:21:42 Or other tools like talks. I think it’s Knox.

00:21:46 Yeah. Knox is the other one. And then we’re kind of designed to run a bunch of tools. That’s their job.

00:21:53 Yeah. All right. Well, thanks a lot for joining the show, and I hope you have a great day.

00:21:57 Yeah. Thanks for having me on. And I hope your day is great as well.

00:22:01 Thanks.

00:22:09 Thanks, Anthony. It’s always a pleasure to have you on the show. Thank you. To Patreon Supporters Join Them at testandcode.com Support thank you to Configure for sponsoring release features faster with less risk with Config cat. Check them out at configcat.com, try for free or use the code testing Code 2021 for 25% off. Thank you Datadog for sponsoring modern end to end monitoring and security get started. The free trial@testandcode.com Datadog and Datadog will send you a free T shirt. Those links are in the show notes@Test And Code.com onefive. That’s all for now. Now go out and test something.