Latest / Talk Python To Me / #178: Coverage.py
Transcript
- 0:00You know you should be testing your code, right? How do you know whether it's well-tested?
- 0:03Are you testing the right things? If you're not using code coverage, chances are you're guessing.
- 0:08But you don't need to guess. Just grab coverage.py maintained by our guest this week,
- 0:12Ned Batchelder. This is Talk Python To Me, episode 178, recorded September 10th, 2018.
- 0:18Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem, and the personalities.
- 0:38This is your host, Michael Kennedy. Follow me on Twitter, where I'm @mkennedy.
- 0:43Keep up with the show and listen to past episodes at talkpython.fm,
- 0:47and follow the show on Twitter via at Talk Python.
- 0:49This episode is brought to you by Brilliant.org and Manning.
- 0:53Please check out what they're offering during their segments. It really helps support the show.
- 0:57Ned, welcome to Talk Python.
- 0:59Hi, thanks, Michael. It's great to be here.
- 1:01It's great to finally have you on the show. I cannot believe we are at episode 178,
- 1:06and you have not been a guest on the show. How did this happen?
- 1:08I know. You're doing something wrong over there. I don't know. You're doing something great over there.
- 1:12You've got to episode 178, which is astounding.
- 1:15Anyone who says, I'm going to do a thing and then does it 178 times, clearly is doing something right.
- 1:19Yeah, we're coming up on, I think on three years.
- 1:22Yeah, actually, maybe over three years. So I got to do some quick math.
- 1:25But yeah, it's been going for a while, and it's really fun. I'm just absolutely loving it.
- 1:29And we're going to dig into a project that you've been actually working on more than three years, right?
- 1:34Yes. So coverage.py is a project that I've been maintaining for 14 years, which seems crazy.
- 1:40That is really amazing. And kudos to you for doing that. That's great.
- 1:44I think what I realized about myself is that I'm very inertial.
- 1:47It's hard for me to start new things, and then it's also hard for me to stop old things.
- 1:51Once you get them rolling, they just keep going.
- 1:53That's right.
- 1:53That's right.
- 1:54It's not a bad trade at all.
- 1:56So before we get into all the details of code coverage and so on, let's just get a little background on you.
- 2:02How did you get into programming in Python?
- 2:03Well, so I've got kind of an unusual story that way.
- 2:06So I'm fairly old for the Python world.
- 2:10I'm 56 years old, and I got into programming because my mother was a software person, too.
- 2:15She was a programmer in the 1960s and 70s, 80s and 90s, I guess, until she retired.
- 2:20Back when programming was really hard.
- 2:22There was no internet, not many books.
- 2:25Programming was different back then.
- 2:26Yeah, there was definitely no internet.
- 2:28The books were in a big, huge three-ring binder at the other side of the room, et cetera, et cetera.
- 2:33But the cool thing was that she would bring home some of those books.
- 2:36So I remember as a kid looking through the IBM 360 programmer's manuals and puzzling over what this stuff might mean.
- 2:42So I sort of come by programming naturally.
- 2:45I've been doing it for a while.
- 2:46I joke that it's the only skill I've got, so it's a good thing that people will hire me to do it.
- 2:51And I got into Python probably in the year 2000, maybe 1999.
- 2:56I'd been working at Lotus on Lotus Notes, which is a collaboration environment.
- 3:01Was that in C++ before?
- 3:03Well, Lotus Notes is written in C.
- 3:05But the reason, the way I got to Python was that Lotus Notes, with its access controls and collaboration controls, someone said, oh, you should look at this thing called Zope.
- 3:13It also does stuff like that.
- 3:15And I looked at Zope, and I thought, you know, Zope's kind of cool.
- 3:17I don't really need that.
- 3:18But this Python thing it's written in seems kind of interesting.
- 3:21And so basically from that point on, when I had a choice of tools for writing some little tools or some scripting or some automation, I would reach for Python.
- 3:29And that's just grown and grown since then.
- 3:31Now, I guess I've been using it for 18 years or so.
- 3:34That's really cool.
- 3:35And Python itself has grown with you, right?
- 3:38I mean, Python of 18 years ago is not Python of 2018.
- 3:41Yeah, and it's funny.
- 3:42I don't even know what version of Python that was.
- 3:45It might have been a 1.x, I guess.
- 3:47Yeah, and Python has definitely grown.
- 3:50I feel like I sort of made a technology choice there, and it's worked out very well.
- 3:55I've watched Python grow into at least two new major niches since then, into web dev and now into data science.
- 4:02I feel much more comfortable in web dev.
- 4:04I feel a little bit like I'm getting left behind with the data science and machine learning.
- 4:09And even just hanging out where people ask questions about things, it's very clear that the center of interest is outside my expertise now.
- 4:18So I've got a lot to learn.
- 4:20It's interesting that you can be an expert in Python after 18 years and be a beginner at the things that people want to use Python for.
- 4:27Yeah, that's super interesting.
- 4:28Yeah, like if somebody said, hey, Michael, go do this plot with Matplotlib and get this data loaded up with Pandas.
- 4:34I'm pretty sure I could not do that without documentation or examples in front of me because I spend most of my time writing web and database code.
- 4:41Yeah, and I have done Matplotlib in a little bit of notebooks.
- 4:45And I'm, you know, I am like the typical, I'm on Stack Overflow.
- 4:48I'm just searching for stuff.
- 4:50I see a chunk of code.
- 4:51I don't know what it means.
- 4:52I paste it in.
- 4:52It seems to work.
- 4:53We're done, you know.
- 4:55And I would love to have a deeper foundational understanding.
- 4:58But I don't have day-to-day problems that need those tools.
- 5:01So there's not much chance for me to really get that learning.
- 5:04Yeah, I feel like it's sort of bimodal now.
- 5:08There's these two big areas that Python is really being used a lot in, at least.
- 5:13And, you know, did you catch Jake Vander Plaas' keynote at PyCon 2017 about Python being a mosaic?
- 5:20I didn't.
- 5:212017 was the PyCon I missed of the last decade of PyCons.
- 5:25That was a good one.
- 5:27Well, basically it was, look, there's all these different ways and people are using Python and their goals.
- 5:32And their entire purpose of using Python may be very different than the person you're sitting next to.
- 5:38But if you learn to appreciate it, it just makes it richer.
- 5:41And I thought it was a really great way to sort of say, like, look at all these things people are doing.
- 5:45They have different motivations and whatnot.
- 5:48But it's just as valid Python in style.
- 5:51It's for a different use case.
- 5:53Right, exactly.
- 5:53Yeah, I feel like we're definitely there.
- 5:55Yeah, one of the things that I do is organize the Boston Python user group.
- 6:00And we have project nights every month.
- 6:02And we just get a big room with a lot of round tables.
- 6:05And we put labels on each table.
- 6:07So there's a table labeled beginners and a table labeled web.
- 6:09And then we get data and we get science and we get hardware.
- 6:12And it's just really interesting to see the variety of uses that people are putting Python to.
- 6:18And there was a woman who came last month, was sitting at the beginners table.
- 6:22And towards the end of the night, I was asking her more about what she wanted to do.
- 6:25And she mentioned biology.
- 6:27And I said, oh, you should have at the beginning of the night, you should have stood up and said, I'm doing biology.
- 6:31We could have found you some biologists to talk to.
- 6:33And she laughed like I was joking.
- 6:35But then I introduced her to the four or five biologists across the room who were doing biology with Python.
- 6:41So it's really a very rich ecosystem of expertise and individual domains, which is fascinating.
- 6:49One of the things I love about Python is a lot of people seem to come to it with another expertise, kind of like you were just saying, right?
- 6:56Like if you're a C++ developer, there's a good chance you may be a developer first, right?
- 7:02But if you're doing Python, you may be something else first that uses Python.
- 7:05And I think that just makes us a richer community.
- 7:08That's why I think it's doing so well in data science is that it's, for whatever reason, it's the kind of language and environment that those types of people can succeed in.
- 7:17Yeah, absolutely.
- 7:19So you mentioned the Boston user group.
- 7:21This is a global podcast.
- 7:22The internet doesn't have a zip code or whatever.
- 7:25But for people generally in the Northeast, like you want to just tell them really quickly about it so they can find it if they don't know?
- 7:31Sure.
- 7:31So the Boston Python user group is a big group.
- 7:33We run events twice each month, generally, a project night, which is basically a two and a half hour unstructured hackathon with some sorting by topic, like I just described.
- 7:43And then most months we also run a presentation night where we try to find people to give talks.
- 7:47We did lightning talks for August.
- 7:49I'm working on grooming a web scraping talk for September, and we're going to have science talks for November.
- 7:56And we're very friendly.
- 7:58We're big and open.
- 7:59We're on meetup.com or bostonpython.com.
- 8:01And if you're anywhere around, come and see us.
- 8:04We've had people travel as long as three hours to get to events.
- 8:08So all of New England is kind of in scope.
- 8:11Yeah, absolutely.
- 8:12Well, I guess it depends on the time as well, right?
- 8:14Rush hour and all that.
- 8:15Three hours could be not far away in certain parts of Boston.
- 8:18That's true.
- 8:19The really memorable one was the father and son who took a three-hour bus ride down from Maine.
- 8:23And that kid was 13, and he was one of the smartest people I've ever met.
- 8:27And they were going to leave and get home at like 2 in the morning based on the bus schedule to have attended.
- 8:32They only came once.
- 8:33And I mean, I don't blame them, but it was very impressive.
- 8:36No, that's cool.
- 8:36Is there any way to remotely attend?
- 8:38Any streaming options?
- 8:39We've never managed to be routine about videoing the presentations, which is really unfortunate.
- 8:46Because even in Boston, we have 8,000 people on the meetup group, and we can only fit 120 people in the room.
- 8:52And we always have a waiting list.
- 8:53So lots of people would like to see video.
- 8:55But we have just never managed to find the staff to make it a regular thing.
- 9:01It's almost got to be somebody's, their responsibility, their role to just do that, right?
- 9:05Absolutely.
- 9:06Yep.
- 9:06And they've got to show up every time, et cetera, et cetera.
- 9:09Yeah.
- 9:09So another thing that you do, and I'm also super passionate about, has to do with online education, right?
- 9:14Right.
- 9:15Yeah, my day job is at edX.
- 9:17EdX.org is the website that was founded by Harvard and MIT and puts university-level courses online.
- 9:23We've got, I don't know, 2,000, 3,000 courses from 130 different institutions at this point.
- 9:30And it's all Python and Django, and it's all open source, which is the thing that really appeals to me because I'm an open source guy.
- 9:35I actually work on the open source team here at edX.
- 9:38So we are encouraging and enabling other people to use our software to do online education.
- 9:43There's about 1,000 other sites besides edX.org that use OpenEdX to do their education, which is thrilling.
- 9:50Because as great as Harvard and MIT courses are, there's all sorts of other kinds of education that those institutions will never provide.
- 9:59We just recently discovered there was a website in Indonesia which has something like 150 different courses all very, very focused on specific skills that might lift someone out of poverty.
- 10:11You know, how to be a maid, how to do hairdressing, how to raise chickens, how to fix small engines, how to catch fish, like just all sorts of things.
- 10:19Super practical vocational type things.
- 10:21Super practical vocational in Indonesian for Indonesians.
- 10:25And edX.org, as many courses as we're going to get.
- 10:29We're never going to deliver those courses.
- 10:30So having the software be open source, you know, we give away education on edX.org and we give away the software to give away education to the rest of the world.
- 10:38And there's 1,000 sites out there that are using it, which is really, really gratifying.
- 10:43Oh, that's awesome.
- 10:44It sounds like a great project.
- 10:45And it's mostly Python and Django?
- 10:47Yeah.
- 10:48It's almost, I mean, JavaScript, of course, too.
- 10:50But yeah, it's all Python and Django.
- 10:52And it's almost all open source.
- 10:54Very cool.
- 10:55And we're hiring, if anyone, you know, tell them Ned sent you.
- 10:57There's a referral bonus.
- 10:58Do you guys have remote positions or it's got to be in Boston?
- 11:03The easiest thing to say is, let's say it's got to be in Boston.
- 11:06Yeah.
- 11:07We're not super good at remote, which is something I wish we could get better at.
- 11:11But that's the reality of the situation today.
- 11:13Yeah.
- 11:13So if people are listening and they want a cool Python job in the general Boston area or they're willing to get there.
- 11:19Yeah.
- 11:20Yeah.
- 11:20Awesome.
- 11:20Get in touch.
- 11:22Let's see what we can do.
- 11:22Yeah.
- 11:23That's really great.
- 11:24It sounds super fun.
- 11:25Okay.
- 11:25So let's talk about this brand new project that you just started called Coverage PY.
- 11:30That's right.
- 11:31This is a podcast from December of 2004.
- 11:33Exactly.
- 11:34All right.
- 11:36So let's talk about what is code coverage and what is this project?
- 11:38Okay.
- 11:39Well, and let me just start with one thing, which is I didn't actually start this project.
- 11:43This project was started by a guy named Gareth Reese.
- 11:45And back in 2004, I was working on a different Python thing and I wanted to use some code coverage on it.
- 11:53And I found this thing called Coverage.py and it worked almost exactly the way I wanted.
- 11:57And the way it didn't, I tried to make a change and I tried to get it to Gareth and Gareth didn't seem to be reachable.
- 12:03So I just sort of published it with my change.
- 12:05And 14 years later, I'm the maintainer of Coverage.py.
- 12:10That's how open source works, right?
- 12:12That's how open source works.
- 12:14Yeah.
- 12:14I'm mulling the idea of doing a lighting talk called Lies People Believe About Coverage.py, one of which is that I started it.
- 12:22Yeah.
- 12:23Okay.
- 12:24But to answer your question, so what is code coverage?
- 12:26So the idea of code coverage is you've got some product code that you've written, meaning the code you actually want to write.
- 12:33And then to make sure that that code works, you write some tests.
- 12:36And I'll give the entire audience the benefit of the doubt and say you have written tests.
- 12:41But now you need to know, are the tests actually doing their job, which is proving that your code works?
- 12:47And one way to test your tests essentially is to observe the tests running and see if all of the lines of your product code were executed by the tests.
- 12:59Because if there's a line of code that isn't executed when you run your entire test suite, then there's no way that line of code can be tested.
- 13:06The converse isn't true.
- 13:08If the line of code is run, it still might not be properly tested.
- 13:11But if it isn't run, then it's definitely not properly tested.
- 13:14Absolutely.
- 13:15If it was never executed, you know nothing about it.
- 13:18Absolutely.
- 13:18That's right.
- 13:18There's no way you know how that line of code works.
- 13:22So the code coverage in general is the automation of that process, which is it is a tool that can observe a program being run and can tell you what lines of code were run in the program.
- 13:34And notice in that sentence, I didn't say anything about tests.
- 13:36Coverage doesn't know anything about what a test is.
- 13:39It's just that typically the program you want to watch is your code while the tests are being run.
- 13:44But you could run coverage for any reason to know what parts were run or not.
- 13:48Right.
- 13:49This is typically most spoken about in terms of unit testing and other types of tests.
- 13:53But one example that comes to mind right away is I've got some app.
- 13:57It's been handed down from person to person and somehow it arrives in my lap and they're like, Michael, you've got to now add a feature or maintain this thing.
- 14:07And it's a big scrambled mess.
- 14:09And the person who knows all about it is gone.
- 14:11Maybe I just want to know when it does its job, does it ever even call this function?
- 14:15Right.
- 14:16Like there could be all sorts of code in there that is just nobody wanted to remove it because they didn't know for sure it was OK.
- 14:22But if you can run the coverage and say, actually, no, it's never executed.
- 14:25Let's delete it.
- 14:26That's right.
- 14:26That's right.
- 14:27So long as you are sure that you know how to fully exercise.
- 14:31Yes.
- 14:31Yeah.
- 14:32You got it.
- 14:33That is another another thing.
- 14:36But I've spent hours trying to understand what a particular function does and how it influences like a big program just to realize that actually the reason any changes I'm making to this section or try to make it do a thing have no effect because it's not being called.
- 14:51Right.
- 14:51Yeah.
- 14:51It's super frustrating.
- 14:53Right.
- 14:53So coverage can be used for that.
- 14:54But like you say, the 99.9% use case for any code coverage tool, including coverage.py, is for it to observe your test suite being run and then to tell you about your product code, which lines were run and which lines weren't.
- 15:07Right.
- 15:08And the idea being that the lines that weren't, that's what you focus in on.
- 15:12And you think about how can I write a test to make that line of code be run.
- 15:15And you'll gradually increase the coverage.
- 15:17And then you're right.
- 15:18Or you make a conscious decision.
- 15:19This part we don't care to test.
- 15:21Right.
- 15:21That's right.
- 15:22You can also decide that.
- 15:24Yes.
- 15:24But I think the important thing is, even if you're in that place, there is a core reason your application exists.
- 15:31There is a thing that it does and there's stuff that supports it doing that.
- 15:35Right.
- 15:35If you're writing a stock trading application, the stock decision engine had better have a good bit of coverage on it.
- 15:41Yes.
- 15:42Or you failed with your test.
- 15:43Right.
- 15:44Testing the login like crazy doesn't help the core engine do anything better.
- 15:48Right.
- 15:48You could decide you're going to increase coverage on the parts you care about.
- 15:53And coverage doesn't really have any opinions about this.
- 15:55It's designed to just tell you something about your code.
- 15:59I've found over the years I am drawn to projects that are all about helping developers understand their world better.
- 16:06And coverage is one of those ways.
- 16:09Right.
- 16:09You wrote something and you wrote something to test it and you thought it was testing it.
- 16:13And, oh, is it?
- 16:14Well, coverage can tell you whether it is.
- 16:16Yeah.
- 16:17I really love that, the way it works.
- 16:19So I guess maybe we could talk a little bit about how you run it.
- 16:22Like, is this something you put in continuous integration?
- 16:24Is it a command line tool?
- 16:26Like, where does it work?
- 16:27Right.
- 16:28So the simplest thing is it is a command line tool and you can run it in your continuous integration on Travis or something.
- 16:34Or you can just run it from the command line.
- 16:35The design is that the coverage command has a number of subcommands, one of which is run.
- 16:40And when you type coverage run, anything you could put after the word Python, you could put after coverage run.
- 16:46So if you used to run your program by saying Python prog.py, then you can say coverage run prog.py.
- 16:53And it will run it the same way that Python would have run it, but under observation.
- 16:56That will collect a bunch of data.
- 16:59And then if you type coverage report, it will give you a report.
- 17:02And for every line, every file that got executed, it will tell you things like how many statements there are, how many got executed, how many didn't get executed, and therefore what percentage of them were executed.
- 17:14And then a big total at the end.
- 17:16Yeah, that's really cool.
- 17:17So I could say like coverage-m unit test in some file or something like this?
- 17:23Yeah, coverage run-m.
- 17:25Yeah, okay, gotcha.
- 17:26Right, dash-m unit test.
- 17:27Yes, I'll just leave it at that, yes.
- 17:29And there's also a pytest plugin, right?
- 17:32There are plugins for pytest and for Nose.
- 17:34And I'll be perfectly honest with you, I'm not a huge fan of the plugins because it's just another bunch of code between you and me, sort of.
- 17:44Like, I don't understand exactly how those plugins work, so it's hard for me to vouch for them doing what you want them to do.
- 17:52But yes, there are plugins for pytest.
- 17:55So there's pytest-Cov, and when you install it, you now have dash-dash-cov, a few, about half dozen dash-dash-cov options to pytest to say things like, I want you to only look at these modules, or I want you to produce this kind of report at the end.
- 18:09So it's much more convenient in that you don't, you can get all of the coverage behavior in one pytest run, rather than having coverage run pytest, and then coverage producing a report as two separate commands.
- 18:20But like I said, there's a trade-off there in that if the plugin isn't doing what you want, then it's a little bit trickier.
- 18:26Now you've got to figure out, is it the plugin, or is it coverage, or is it my script that's doing it?
- 18:31It's one more variable in there.
- 18:33Yeah, it just makes it a little more complex.
- 18:34Okay, interesting.
- 18:35Another place that I really like to run coverage.py is from PyCharm.
- 18:41I don't know if you've ever seen the integration there, but that is just incredible.
- 18:44You right-click on some part of your code and say, run this with coverage, and then you get a report in PyCharm.
- 18:50But PyCharm, the editor itself, actually colors each line based on the coverage, which I think is a really nice touch.
- 18:56Yeah, PyCharm is really an amazing IDE, which I don't actually use because I'm old.
- 19:01There's also a Vim plugin, I believe, to do similar things to get the coverage data and to display it to you in Vim and probably for Emacs as well.
- 19:09Oh, that's cool.
- 19:10Yeah, the more convenient we can make it for people to see the information, the better off it's going to be, the tighter feedback loop you've got.
- 19:17And right there in the editor is the best place to see it because that's where you're going to have to be dealing with the code anyway.
- 19:23You're not trying to correlate some report back to some file.
- 19:26You just look at it like, oh, why is that red?
- 19:28That should be green.
- 19:29What's going on here?
- 19:29Right, exactly.
- 19:30And coverage will produce HTML reports that are colored red and green and actually have a little bit of interaction if you want to focus in on things.
- 19:37But if other IDEs or whatever can produce displays that make it more convenient for people, then more power to them.
- 19:45Coverage has an API, Python API, which is, I assume, what PyCharm is built on, although they could be also just doing subprocess launches and things like that.
- 19:53So, yeah, I'm happy to have people get access to the power of that coverage measurement, however they're most happy with.
- 20:01Yeah, that's really great.
- 20:03So, one of the things that I kind of hinted at before with the core trading engine and certainly with, say, like your test code, you probably don't care about looking at the analysis of the coverage of your test code.
- 20:15You would like to see the analysis of your code under test.
- 20:19So, how do you exclude some bits of code from being analyzed?
- 20:22All right.
- 20:23So, I'll answer your question first, and then I will challenge the premise of the question.
- 20:26Okay.
- 20:27Sure.
- 20:27Good.
- 20:29So, coverage gives – there's a bunch of controls that you can get used with coverage to, say, basically to focus its attention on the code you want.
- 20:37If you run coverage just the way I started with, it will tell you about a lot more than what you care about because it's going to tell you about every library you've imported, even if it's not your code.
- 20:47And that's partly because that's the way it used to be and partly because it's hard to know what counts as a third-party library versus your code if you're running your code in an installed setting and so on and so forth.
- 20:59So, there are controls to focus coverage in on the code you want.
- 21:05The simplest is the source option.
- 21:07And the source option says, I'm only interested in any source code that you find from this tree downwards.
- 21:12And, for instance, source equals dot is often a very good choice.
- 21:16choice because it means I'm in the current working directory.
- 21:19Here's my code.
- 21:20Don't tell me about any code you find anywhere else.
- 21:22But even more than that, you can say things like omit these file paths or only include these file paths.
- 21:30So, there's a lot of controls there to focus coverage in.
- 21:34And that's because an automated tool is great unless you're constantly knowing more than the automated tool tells you.
- 21:41Like, if the automated tool is like a noisy kindergartner yammering at you and you have to tell it, keep thinking to yourself, shut up about that.
- 21:51I know that's not what I'm concerned about.
- 21:53Then the tool is not useful.
- 21:56Yeah.
- 21:59This portion of Talk Python To Me is brought to you by Brilliant.org.
- 22:02Many of you have come to software development and data science through paths that did not include a full-on computer science or mathematic degree.
- 22:09Yet, in our technical field, you may find you need to learn exactly these topics.
- 22:14You could go back to university.
- 22:15But then again, this is the 21st century and we do have the internet.
- 22:19Why not take some engaging online courses to quickly get just the skills that you need?
- 22:24That's where Brilliant.org comes in.
- 22:26They believe that effective learning is active.
- 22:29So master the concepts you need by solving fun, challenging problems yourself.
- 22:33Get started today.
- 22:35Just visit talkpython.fm/brilliant and sign up for free.
- 22:39And don't wait either.
- 22:40If you decide to upgrade to a paid account for guided courses and more practice exercises,
- 22:45the first 200 people that sign up from Talk Python will get an extra 20% off an annual premium subscription.
- 22:51That's talkpython.fm/brilliant.
- 22:55People will stop using it, right?
- 22:57Like if it becomes too noisy and too annoying, then you're like, well, it would have been helpful.
- 23:00But it's just so much noise.
- 23:02Forget this thing.
- 23:03Right.
- 23:03Or there's actually useful information in it that they can't see or whatever.
- 23:08So it shouldn't be that the way you use coverage is you run it, you get a report, and then you skip over all the stuff you know you don't care about to hopefully see the stuff you do care about.
- 23:19So there's a lot of controls and coverage to let you be the smart one in the room and let it be the savant about the one thing that it is smarter than you about, which is what code got run.
- 23:31So in addition to being able to exclude or include file paths and modules, you can actually put comments in your code to tell coverage, this line isn't run, but I don't care.
- 23:43Like, don't tell me about this line anymore.
- 23:45I'm okay with it not being run.
- 23:47Yeah.
- 23:47One of the examples you gave was the repper method, dunderrepper.
- 23:50Right.
- 23:51Do you really want to write a test that instantiates an object and just prints it just to get that function?
- 23:56Right.
- 23:57Like, no.
- 23:57And I've got lots of dunderreppers in the coverage.py code, and they're only ever run when I'm debugging coverage.py.
- 24:03I'm in a debugger, and I want to see what that is.
- 24:06You want to see the string representation better than this type at that address.
- 24:09That's right.
- 24:10So I write a dunderrepper, and I don't want to be told that it's not being executed in my test suite because I don't run it in my test suite.
- 24:17So – and in addition to putting comments on lines, what you can actually do with coverage is there's a coverage.rc file to configure coverage, and you can actually specify a list of regexes, and any line that matches one of those regexes will be excluded from coverage measurement.
- 24:32And that's how the comment works.
- 24:34The comment is a regex pattern by default in that setting.
- 24:37But, for instance, when I run coverage, I put, like, def space dunderrepper as one of the regexes.
- 24:44And that will match all my reppers, and then they'll all be excluded from coverage, and I don't need to worry about them anymore.
- 24:49Yeah, that makes a lot of sense.
- 24:50So you could put the comment hash, pragmical, and no cover, but you might not want that in your code all over the place.
- 24:58Right.
- 24:58You'd have to remember to put it in and et cetera.
- 25:01Yeah.
- 25:02People who don't care about it, they're like, why is this in this code?
- 25:04Or, you know, they write their code and they don't add it.
- 25:06Yeah, it's just – sometimes it's better to have it separate.
- 25:09Yeah.
- 25:09Right.
- 25:10And what I actually have done, because I'm a little obsessive about it in coverage.py itself, is I have a half dozen or ten different comment syntaxes that I'll use to exclude lines from coverage because they're being excluded for different reasons.
- 25:24Like a line that only runs on Jython, I will exclude from coverage because I'm not doing coverage measurement under Jython, even though I want to have a little bit of Jython support in the code.
- 25:34And so I'll have a comment that says only Jython.
- 25:36And then I know why it's been excluded.
- 25:38That's cool.
- 25:40Because I can just make a list of a dozen regexes and it's all there.
- 25:44You put it into that configuration file.
- 25:46Yeah.
- 25:46Yeah.
- 25:46Nice.
- 25:47Another thing that I thought was interesting was sort of the converse of this is to explicitly include some files.
- 25:56Because if you say, look here, and you run this one particular file, it only looks at the actual files that were loaded, the modules that were loaded, not the stuff laying next to it that maybe should have been reported on but nobody ever touched.
- 26:09Right.
- 26:10And that was one of the failings of early versions of coverage.py was that the only thing it knew about your code is code that got run.
- 26:19And so, for instance, if there was a particular file in your source tree that was never executed at all, I mean, forget about lines not being executed.
- 26:26The entire file was never executed.
- 26:28It wouldn't show up in the coverage report because coverage had never heard about it.
- 26:32Right.
- 26:33If you run a line of code in a file, coverage knows about the file and then it can see all the lines that weren't run.
- 26:37But if you never ran any of them, coverage never heard of the file.
- 26:41So that feature is now in coverage.py.
- 26:43If you give it a source option, then it has a tree to search and it can look for all of the importable Python files that it never heard of and tell you about those 0% files.
- 26:52Right.
- 26:52That's kind of like the example of me saying, here's this method that was never run.
- 26:56And, like, not even the module is imported in the main bit of code.
- 26:59Right.
- 27:00Right.
- 27:00Right.
- 27:00And, by the way, there's a really cheap, low-tech way to do file-level coverage, which is you delete all your .pyc files and then you exercise all your code.
- 27:13And any .py file that doesn't have a .pyc file was never imported.
- 27:16Oh, right.
- 27:17Yeah.
- 27:17Okay.
- 27:17That's pretty interesting.
- 27:18Very low-tech.
- 27:19It's quite low-tech.
- 27:21I forgot to challenge the premise of your earlier question.
- 27:23Yeah.
- 27:23Yeah.
- 27:23Let's get back to that.
- 27:24Yeah, so you said you don't want to do coverage measurement of your tests, but it actually can be very useful to do coverage measurement of your tests, if only because the way test runners work, it's really easy to make two tests that accidentally have the same name.
- 27:39You know, oh, I like that test.
- 27:41I want to do one kind of like it.
- 27:42I'll copy it and I'll paste it.
- 27:43I'll forget to change the name.
- 27:44And now I actually only have one test.
- 27:47If I look in the code, it looks like there's two, but there's really only one.
- 27:51Yeah, yeah.
- 27:51It's so easy to do that.
- 27:53And so if you do coverage measurement of your test files also, then you'll see those cases.
- 27:58Interesting.
- 27:59Okay.
- 27:59I guess, yeah, I never really thought about that.
- 28:01That's pretty valid.
- 28:02You know, I was thinking more of like you might say like ignore a particular test and you don't want that to say break the build because it drops it below some percentage or something like that.
- 28:12But yeah, that's a very – because I feel test code is probably some of the most copy and pasted code there is.
- 28:18Exactly.
- 28:19Exactly.
- 28:19And you never actually use the function name directly.
- 28:22So you'd never know.
- 28:24Right.
- 28:24The name of the test function doesn't matter.
- 28:26Yeah, exactly.
- 28:28It's really easy to lose a test.
- 28:30All right.
- 28:30I accept defeat on this one.
- 28:33No, that's a really good point.
- 28:34All right.
- 28:35Well, spur one for me.
- 28:36The other thing, when people say they want to exclude their tests from coverage, often it's because they've set a goal for their coverage measurement.
- 28:44Like we need 75% coverage.
- 28:47And those goals are really completely artificial.
- 28:50Like how did you choose 75%?
- 28:52Like why – like a quarter of your code doesn't need to be tested.
- 28:55Why is that okay, right?
- 28:56So the way I look at it, the number – the coverage number has no meaning except that lower numbers are worse.
- 29:04That's the only meaning to the number.
- 29:06So if someone says like how much coverage should I have, there's no right answer to that.
- 29:11Yeah.
- 29:12You know, I guess probably your test code has a pretty high coverage rate relative to your other codes.
- 29:18Yes.
- 29:18You're only helping yourself in that number.
- 29:20That's right.
- 29:21That's right.
- 29:21You can gain the system.
- 29:22Exactly.
- 29:23I guess one of the reasons I was thinking about excluding it is I just don't want to see it in the report.
- 29:27Like I don't need to see the test coverage.
- 29:29But your example of this copy and paste error actually is pretty valid, I think.
- 29:33Well, another option in the coverage.py reports is to exclude files that are 100%.
- 29:40Right.
- 29:41Which, again, lets you focus in on where the problems are.
- 29:43Like you don't need to think about files that have 100% coverage.
- 29:46What you need to think about is the ones that are missing some coverage because you need to go and look at those lines and write some tests for those files.
- 29:52So if all your tests have 100% coverage, then include them and exclude the 100% files from the report.
- 29:58Right.
- 29:59And hopefully that does a smaller list.
- 30:01Yeah.
- 30:02Yeah.
- 30:03Yeah.
- 30:04You can always use the pragma stuff on, say, your test code to say,
- 30:09Well, these three parts I'm having a hard time getting to run for whatever reason.
- 30:12And just, you know, tell it to not report on that.
- 30:14It'll hit it to 100 and it drops out of the list, right?
- 30:16Right.
- 30:17Exactly.
- 30:17Yeah.
- 30:18The other thing about test code I find is that in a full, mature test suite, you've got a significant amount of engineering happening in your test.
- 30:27If not in the tests themselves, in the helpers that you have written for your tests.
- 30:30Yeah.
- 30:31And not that there's code in there that isn't being run and should be run, but there might be code in there that isn't being run and therefore you can delete it.
- 30:40Right.
- 30:40Yeah.
- 30:40That same conversation of like, let's, I do feel like a lot of times people treat their test code with less, what's the right word?
- 30:48Kind of professionalism or attention.
- 30:50They're like, well, this is test code.
- 30:52So it doesn't matter that this big block of code was repeated 100 times.
- 30:57Why would I ever extract the method to that?
- 30:59Like there's just less attention to the architecture and patterns there.
- 31:02And I feel like that would help.
- 31:04Right.
- 31:04And so I agree with you that repetition should be removed from tests as it is from elsewhere.
- 31:10But I've also heard people feel passionately that tests should be repetitive, that each test should be readable all by itself.
- 31:18So I'll, you know, give them the benefit of the doubt that maybe that's what they want.
- 31:22And that's fine too.
- 31:24Yeah, sure.
- 31:25And if that's a conscious decision, then that's fine, I think.
- 31:29But a lot of people just do it because, well, they wrote one test and then they copied it and then they edited it and they copied it and they edited it.
- 31:35You know what I mean?
- 31:36Yeah, exactly.
- 31:37Right.
- 31:37And they don't have time to make the tests nice because who cares about the tests?
- 31:40Yeah, exactly.
- 31:41Until you change the thing under test and all of a sudden it's so hard to get it to run because you had poor decision making around writing the test.
- 31:50Then you claim unit testing is too hard because I don't want to do it and so on.
- 31:55Yeah, writing tests is real engineering with different problems than writing your product code.
- 32:01And those problems need to be paid attention to, which that's probably a whole other episode.
- 32:06Yeah, I've certainly heard people make statements like that they don't really understand, you know, things like object oriented programming and other proper design patterns until they started writing tests and trying to make their code more flexible.
- 32:19Like, how do I actually get in between the data access layer and my middle tier logic and test that without having a database and things like that?
- 32:28Yeah, they really make you think.
- 32:29Yeah, they do because it's a second use of your code.
- 32:32And your code is going to be way better designed if you consider more than one use of the code.
- 32:37Yeah, absolutely.
- 32:38Testability is a great topic.
- 32:39Yeah, I actually, yeah, I totally agree.
- 32:42And I think that, what you just said right there is one of the main reasons to test is the architecture that comes out of it.
- 32:48All right, so you spoke a little bit about these config files.
- 32:50And in these config files, I can put regular expressions, which will, you know, limit these sections.
- 32:55Oh, and one point I did want to make really quick about that.
- 32:59When you put one of those comments, if you put that on like a branch or some kind of structure, like a class, like everything underneath that will be blocked.
- 33:07You don't have to like put that on 100 lines, right?
- 33:09You put it at the sort of root node.
- 33:10Yeah, exactly.
- 33:11And it's, so it's basically if you put it on a line with a colon, then that entire clause is excluded.
- 33:18The one thing that sounds like it might be a generalization of that that doesn't work is to put one of those comments at the top of a file.
- 33:23It doesn't exclude the entire file.
- 33:25There's a request to do that.
- 33:27That seems like a good idea, but we've never, haven't gotten to that yet.
- 33:30Okay, cool.
- 33:30So, but back to config files, there's more stuff that you can put in there.
- 33:34Like what are useful things that are people are doing there beyond just exclusion?
- 33:38Yeah, so there's some basic options.
- 33:40Like one of the things that coverage.py can do is to measure not just which statements got executed, but which branches got taken.
- 33:46So, for instance, if you write an if statement, it can tell you whether both the true case and the false case were executed.
- 33:53And you might say, well, I can tell that by looking at the code in the true case and the code in the else clause, but not every if statement has an else clause.
- 34:01So, if you have an if with a statement in it, but there's no else clause, you can't tell just by looking at individual lines that are executed whether the false case of the if was executed.
- 34:12Right.
- 34:12Did you effectively skip that if block?
- 34:15Right.
- 34:15Have you ever actually skipped that statement in the if?
- 34:18Yeah.
- 34:19And so, and branch coverage is what can do that.
- 34:21And so, that's one of the options you can set in the config file as well as on the command line.
- 34:25I mean, basically, just to go back to the plugins question, the original reason I wrote support for the coverage RC was because I was adding features to coverage and people were using the test runner plugins as their UI.
- 34:39So, I didn't have any way to give them to try new features and coverage until the plugins were updated.
- 34:45I see.
- 34:46So, I added support for an RC file so you could control coverage even under a plugin from a thing that I could actually add features to.
- 34:53So, all of the features of coverage are controllable from the RC file.
- 34:58A big thing that gets put in there, one of the complicated scenarios for coverage is if you have tests that run in parallel.
- 35:06Either because you're running them on separate machines or you're just running them in separate processes or because you ran the 2.7 tests separately from the 3.6 tests.
- 35:15And then you want to take all that data and combine it back together to get one coverage report.
- 35:19And under those scenarios, there's often cases where, oh, on my CI system, the code was in this directory.
- 35:25But back home where I'm going to write the report, it's all in this directory.
- 35:30And coverage has to somehow know that those two directories are the same in some way, that they're equivalent.
- 35:35And those file path mappings go into the coverage RC file, for instance.
- 35:40I see.
- 35:41Okay, that's cool.
- 35:41So, that during the combination process, it can remap file names to get everything to make sense.
- 35:47Yeah, that makes a lot of sense.
- 35:48So, speaking of stuff in parallel, what's the story around like threading, async.io, multiprocessing, or even if I just want to start a subprocess?
- 35:56Yeah.
- 35:57Does it have support for that?
- 35:58Do I have to do things special for that?
- 36:00Yeah, so that gets complicated, too.
- 36:02So, there are four kinds of concurrency that coverage.py supports right out of the box.
- 36:08Threading, multiprocessing, gevent, and green.
- 36:12I forget which one of the other ones.
- 36:14I forget what it's called.
- 36:14And there's code in coverage.py that's specifically designed to see that that's happening and do the right thing.
- 36:22You have to tell coverage which one you're going to use, but once you do that in your coverage RC, it knows how to do that.
- 36:28If you're running your own subprocesses, it gets a little bit trickier because you've got Python code in your main process, and then you're going to spawn a subprocess, which is a whole new Python process that's sort of jumped outside of what coverage is watching.
- 36:41There's a little bit of support for coverage getting started on that subprocess.
- 36:46You have to do some manual setup, and that's covered in the docs.
- 36:49I would like to make that more automatic, but it feels a little intrusive to sort of have coverage start on every Python process you ever start in the future.
- 36:58I'd rather be conservative about that than suddenly be in the middle of something where people didn't expect it.
- 37:05So, we try to support all those different concurrencies.
- 37:09I just got a report that QT threads don't work, which doesn't surprise me because they're C threads, and so how do I get involved in that?
- 37:16Right, it doesn't carry that process across, yeah.
- 37:18Right, and it gets very fiddly.
- 37:19Some of the worst code in coverage.py is in the way we support those kinds of concurrencies because we need to get involved at the very beginning of a thread or the very beginning of a process,
- 37:31and there isn't always support for me to just say, hey, next time you start a process, why don't you run a little bit of my code first?
- 37:37No, that's not your process.
- 37:39Yeah, we do some very invasive things there.
- 37:42If you're interested, go and look at multiproc.py.
- 37:44Yeah, how interesting.
- 37:46This portion of Talk Python To Me is brought to you by Manning.
- 37:51Python has become one of the most essential programming languages in the world with a power, flexibility, and support that others can only dream of,
- 37:58but it can be tough learning it when you're just starting out.
- 38:01Luckily, there's an easy way to get involved.
- 38:04Written by MIT lecturer Annabelle and published by Manning Publications,
- 38:08Git Programming, learn to code with Python, is the perfect way to get started with Python.
- 38:12Anna's experience as a teacher of Python really shines through as you get hands-on with the language without being drowned in confusing jargon or theory.
- 38:20Filled with practical examples and step-by-step lessons, Git Programming is perfect for people who want to get started with Python.
- 38:26Take advantage of a Talk Python exclusive 40% discount on Git Programming by Annabelle.
- 38:32Just visit talkpython.fm/manning and use the code belltalkpy.
- 38:37That's belltalkpy, all one word.
- 38:41One thing that the documentation talks about is this thing called sitecustomize.py.
- 38:46Right, exactly.
- 38:47I had never even heard of this.
- 38:49You're like, oh, you just put it in here.
- 38:50I'm like, what is this?
- 38:51Tell them about this.
- 38:53It's all sort of the start of the initialization of Python itself, right?
- 38:56Right, exactly.
- 38:57So this is about how do, if you're going to run a Python program, how can I make it so that my code runs before your program even starts?
- 39:05Right, because if you're going to run a subprocess and it's a Python program, I want coverage to start before your code starts.
- 39:12One way to do that is for you to change your code so that instead of launching your Python program, you launch it with coverage.
- 39:18But that gets very invasive.
- 39:20No one wants to do that.
- 39:21You're not going to run your product by launching coverage, so your test would have to be different.
- 39:26Probably not.
- 39:26Product works.
- 39:28Yeah.
- 39:28So I looked around for ways to have code run before your main program.
- 39:34And there's no sort of built-in support for it.
- 39:38Perl actually has a command line switch that says run this program, but first run something else.
- 39:44Python doesn't.
- 39:45So there are two ways to do it.
- 39:46One is that when you run a program in Python, if it finds a file called site, customize.py, it will run it.
- 39:52And I don't know exactly what that file is for.
- 39:54It sounds like the kind of thing that would be recommended against these days.
- 39:59Seems weird.
- 40:00Like if you need something for your program, right?
- 40:02Don't put it into a weird file in your site packages.
- 40:05Somehow put that in your main program.
- 40:07But it's there.
- 40:08So one of the ways to get coverage to run before your subprocess starts is by putting something inside
- 40:13customize.py.
- 40:14And can you make that just like a file alongside your working directory, the top level of your
- 40:19working directory?
- 40:20Or has it got to go somewhere else?
- 40:21Honestly, I'm not quite sure.
- 40:22When I have to do this, I use the second technique for doing it because changing a file is scarier
- 40:30than creating a new file.
- 40:32And so the second way to do it only involves creating a new file, even though it's perhaps
- 40:38an even more obscure way to get code to run before the start of Python.
- 40:42Now, we don't have to go through all the details.
- 40:44But if you go into your site packages directory and you look for .pth files, path files, they
- 40:50have this very bizarre semantic, which is if a line in that file starts with the word import,
- 40:56then it will execute that line, even if the line has way more stuff than just an import
- 41:01statement.
- 41:01Weird.
- 41:01Yeah, it's super weird.
- 41:03It's super weird.
- 41:04And you can name it anything?
- 41:05Just like?
- 41:06Yeah, anything .pth.
- 41:07Yeah.
- 41:07This is all part of how site packages and the path gets set up.
- 41:11Just a little bit of backstory.
- 41:13Working at Coverage is fascinating because just programming technology is interesting,
- 41:17but you also get to discover all sorts of really weird, dark corners of the Python world.
- 41:22And I've gotten in the habit of every time a new alpha of CPython is announced, I get it
- 41:29as soon as I can, and I build it, and I run the Coverage test suite.
- 41:33And some of the core developers are used to, like, we better get the alpha out there so that
- 41:38Ned can run the Coverage test suite and tell us what we broke.
- 41:40So I think 3.6 RC3 was because of me.
- 41:44Oh, wow.
- 41:45Because of a bug I reported from the Coverage.py test suite.
- 41:47Yeah, there's a lot of weird, dark corners and a lot of kind of gross hacks to make things
- 41:52work.
- 41:52It's very difficult.
- 41:53So for instance, I said Coverage run works just like Python.
- 41:56Well, it's not easy to make a program that will run your Python program just the way Python
- 42:02runs your Python program.
- 42:03And there's some extensive tests in the Coverage.py test suite that actually do run the two side
- 42:08by side and then do a bunch of comparisons of the environment in which the code finds itself
- 42:12to try to assert that it does run it the same way.
- 42:15Yeah.
- 42:16I don't envy the job of keeping that compatibility working.
- 42:19Yeah.
- 42:20Well, luckily, we have a good test suite.
- 42:22Yeah.
- 42:23Well, I bet it has good code coverage as well.
- 42:26Yeah.
- 42:26Well, the sad thing is that there's code inside Coverage.py that because it's at the very
- 42:31heart of how coverage measurement gets done cannot itself be coverage measured.
- 42:35So my code coverage is at about 94%.
- 42:38That's still pretty good.
- 42:39But yeah, that's still pretty good.
- 42:40That's quite ironic.
- 42:41It is ironic.
- 42:42You'd think like me of all people, I'd have 100% test coverage.
- 42:45But and I've thought about really weird, hacky ways of trying to get at that stuff.
- 42:49But it's just not worth it.
- 42:50Yeah, I totally hear.
- 42:51So you spoke a little bit about the complexities and challenges of making it work.
- 42:55Let's dive in a little bit to how this actually works, right?
- 42:57Yeah.
- 42:58It seems like magic that I type your command line thing and then numbers all spit out of,
- 43:05well, here's exactly how your code ran.
- 43:07Like, how does that work?
- 43:08Yeah.
- 43:09So at the very heart of it is a feature of CPython called the trace function.
- 43:13So if you look in the sys module, there's a function called set trace.
- 43:16And you give it a function and it will call your function for every line of Python that gets
- 43:22executed.
- 43:23And that function can do whatever it wants.
- 43:25It is the basis for debuggers, for profilers, and for code measurement tools.
- 43:31And for some other things like tools that will let you run your program and have it just print
- 43:36every line of code that gets run.
- 43:37So you can sort of get a like a global log of everything that happened.
- 43:41Right.
- 43:42Some of those like sort of inspectors that will print out like an execution of your code as
- 43:47it runs, like the actual lines that are running.
- 43:49Yeah, those are pretty neat.
- 43:50Yeah, exactly.
- 43:50Yeah.
- 43:50Doug Hellman wrote a cool one called Smiley.
- 43:52Smiley.
- 43:53Okay.
- 43:53It does some cool things.
- 43:54Yeah.
- 43:54It spies on your code.
- 43:56So at the heart of it is that trace function.
- 43:59And for instance, if you've ever had to break into a debugger from your code and you type
- 44:03import PDB, PDB.setTrace, the reason it's called .setTrace instead of what it should be called,
- 44:08you know, break into the debugger, is because that's the function where PDB calls setTrace to get its trace
- 44:15function in place so that it will get told as lines get executed and then you're debugging.
- 44:20So that's the core CPython feature.
- 44:22And you can go and write, you know, the 10 lines of code that do interesting stuff with the trace function.
- 44:28It's actually pretty simple.
- 44:29Coverage.py has way more lines of code than that.
- 44:32But, you know, at its heart, that's what it's doing.
- 44:35It sets a trace function and then it runs your code.
- 44:38And as its trace function gets invoked with, you know, this line got executed, this line get executed, etc., etc.,
- 44:44it records all that data and dumps it into a data file.
- 44:47And that's the run and out phase of Coverage.py.
- 44:50All that raw data then gets picked up when you say Coverage report.
- 44:54It reads that data.
- 44:56It looks at your source to analyze the source to figure out what could have been run.
- 45:00Right in that first phase, all we hear is what got run.
- 45:03The analysis phase is let's look at the source code.
- 45:06How many lines are there?
- 45:07What could get run?
- 45:08And then essentially it just does a big set subtraction.
- 45:11Here's the lines that could have been run.
- 45:13There are the lines that did get run.
- 45:14What's left over are the lines that didn't get run.
- 45:17I mean, conceptually.
- 45:18One thing that you did talk about in how it works is in the documentation is that you actually use the PYC files as part of this, right?
- 45:27You've got to look in there for the line numbers and stuff, which is kind of interesting.
- 45:32And that's one of the things that's actually changed a number of times over the course of Coverage.py's life is, well, how do we know what could have been executed?
- 45:42The question of what did get executed is kind of straightforward because we get it from the trace function.
- 45:46There's not much we can do there.
- 45:48I mean, there's a lot to do there.
- 45:50But there's essentially only one way to know what got run.
- 45:53Figuring out what could have been run, there's a bunch of different ways.
- 45:56As you mentioned, .pyc files compiled into them are a line number table that tells Python which lines are executable.
- 46:05And that's how Python actually decides when a next line has been run.
- 46:10The trace function, it's executing bytecodes.
- 46:12It calls the trace function when the line number table says this bytecode is the first bytecode on a new line.
- 46:17Right, because if I write like print, you know, quote, ned, comma, quote, batch elder, that actually is like multiple steps of the instruction.
- 46:28If I disassembled that, that would be like load the first string onto the stack, load the second string, execute the function, get the return value, all that type of stuff, right?
- 46:36That's right.
- 46:36So the trace function only gets called for lines.
- 46:39And so we're looking at the same information the Python interpreter is for deciding where the lines are.
- 46:43It gets more involved in that because, for instance, for branch coverage, we have to decide where the branches are.
- 46:48And we do that by analyzing the abstract syntax tree.
- 46:52We used to do it by analyzing the bytecode back when I thought, oh, well, the bytecode will have all the ifs and jumps I need.
- 46:58But it doesn't, and it never did.
- 47:01And when the async keywords got added, they were completely different than everything else.
- 47:06And I said, OK, fine, we're getting rid of all this bytecode analysis.
- 47:09And I'm going to do an AST analysis because I understand the AST when I don't understand the bytecode.
- 47:13All right.
- 47:13So it does support async and await?
- 47:15Yeah.
- 47:16You know, I have to be honest.
- 47:17When that first came out, I didn't know anything about async and await.
- 47:20I didn't understand how to write programs that use them.
- 47:24And I sort of took the low-tech, lazy maintainers approach, which is I'll wait for the bug reports to come in about how it doesn't work for those things.
- 47:31And no one has ever said it doesn't work.
- 47:33So I guess it works.
- 47:34Yeah.
- 47:35All right.
- 47:35That'll do it.
- 47:36Yeah.
- 47:37Cool.
- 47:37So I guess one question is calling functions in Python is not one of the fastest things that Python does relative to, say, other languages.
- 47:46So if you're calling this set trace function on every single line, how does that not just stop the execution, basically?
- 47:52Yeah.
- 47:52So, well, so the really simple answer is that if you write a Python function and set it as your trace function, then, yeah, you're going to have a significant performance hit.
- 48:01Because it's going to call a Python function for every line of your code.
- 48:05And if that Python function does anything interesting at all, then it's going to do a lot of work for every line of your code.
- 48:11Inside Coverage, we actually have two implementations of the trace function.
- 48:15One in Python and one in C.
- 48:17And the one that's in C is there to make it go faster.
- 48:19So there's a bunch of work to try to do as little work as possible on each call.
- 48:25So, for instance, when we get told that we're calling a new function, we do a bunch of work to figure out is this function or is this file, rather, a file that we're interested in at all.
- 48:37Have you already figured out how much coverage it has?
- 48:39Like if it's 100, right, then don't do it again.
- 48:41Well, we don't know.
- 48:42At this point, we're in the run phase.
- 48:44We have no idea about numbers like that.
- 48:46All we know is this file is getting executed.
- 48:49Is our current configuration telling us that that file is interesting or it's not interesting?
- 48:53And if it's not interesting, then we try to quickly get out of that function and set some bookkeeping so that the function doesn't get called again until we return from the function, for instance.
- 49:03So there's a lot of work to try to make the trace function as fast as possible.
- 49:07Because especially for mature test suites for the types of people that are running coverage.
- 49:13The one thing I can tell you about all of those test suites is that developers wish they ran faster.
- 49:18Like I don't need to know anything about your test suite.
- 49:20If you've got a lot of tests, you want them to run faster.
- 49:23So the last thing I want to do is make them go even slower.
- 49:27And I don't know what the typical overhead is of coverage on a test suite because it's going to vary so much depending on what else those tests are doing.
- 49:35But the C code is there to try to make it go as fast as possible.
- 49:38I see.
- 49:39Yeah, that makes a lot of sense.
- 49:40The pure Python implementation is there for, for instance, PyPy, which can't run C extensions.
- 49:45Right.
- 49:45But after a while, it'll start to speed up as they maybe consider it has to be jitted after like a thousand lines or something.
- 49:52I guess so.
- 49:53I haven't.
- 49:54I don't know.
- 49:54PyPy is still under the magic category for me.
- 49:57And I don't know whether a trace function will actually get the jit applied.
- 50:02That's the thing I am used to is that a trace function is not run like regular code.
- 50:08And I don't know if PyPy can jit a trace function, for instance.
- 50:13I have no idea either.
- 50:15Yeah, I don't know.
- 50:16Interesting.
- 50:17There are a few speed ups in there specifically for PyPy.
- 50:20Basically, when a PyPy developer tells me, hey, why don't you say this magic thing that you don't understand?
- 50:25And PyPy will go a little bit faster.
- 50:26And I'm like, great.
- 50:27Sure, I'll put it in.
- 50:28Yeah, PyPy is interesting.
- 50:32So I guess, you know, that's a good segue over to which runtimes are supported.
- 50:36Like, you mentioned jiton, you mentioned PyPy, obviously cpython.
- 50:40What versions of cpython?
- 50:42What else?
- 50:42Yeah, so the current released version of coverage.py is 4.5.1.
- 50:47And that supports Python 2.6 and Python 3.3 and up.
- 50:53So 2627, 33, 34, 35, 36, 37.
- 50:58The tip of the repo now, I'm already working on coverage 5.0 alphas.
- 51:03And I've dropped support for 2.6 and for 3.3.
- 51:06But the released version can do 2.6.
- 51:09For a while, I was very proud of supporting 2.3 through 3.3, which is kind of an interesting trick.
- 51:16But I don't have to do that anymore.
- 51:18So PyPy, both 2 and 3, are supported.
- 51:21I have run tests on jiton and IronPython.
- 51:24They can't do all of it because they can't do the analysis phase because they don't have AST.
- 51:28But you can run your code under jiton and IronPython for the measurement phase.
- 51:34And then you use cpython to do the analysis phase.
- 51:36I see.
- 51:37I don't know how well that works.
- 51:38I haven't heard a lot from people about it.
- 51:40I don't know how much those jiton and IronPython are getting used these days.
- 51:44There's another one called python.net.
- 51:45I actually haven't used it for anything.
- 51:47But I think it's a successor type of thing.
- 51:49A little bit different than IronPython, but similar.
- 51:51Is it?
- 51:52I need to look into this.
- 51:53I need more complication in my life.
- 51:55Yeah, I haven't done a lot with it.
- 51:58But python.net seems like a more maintained, different take on what IronPython was.
- 52:04But I pretty much have exhausted my knowledge of it now.
- 52:07All right.
- 52:07Cool.
- 52:07My question earlier was, what are your plans on supporting old versions?
- 52:11So if cpython stops supporting 2.7, are you going to stop supporting it?
- 52:17Yeah, that's certainly going to happen soon enough.
- 52:19Or say 3.4, what's the timing?
- 52:23Do you try to stay farther behind?
- 52:24What's the thing?
- 52:26Like I said at the top of the show, I'm pretty inertial.
- 52:29So I tend to support things for longer.
- 52:31So my philosophy about coverage.py development is that I want coverage.py to be available
- 52:37for anyone who's trying to make their code work better for Python.
- 52:42And the environments they care about, I will care about.
- 52:46So for instance, I first ported coverage.py to 3.0 in 2009, which was pretty early.
- 52:54And it was pretty rough around the edges.
- 52:55But I thought, you know, if people are going to start porting their libraries to Python 3,
- 52:59it'd be super useful if coverage.py were there to help them understand how their tests were doing.
- 53:04So I tried very hard to get ahead of it.
- 53:06And for that reason also, coverage.py has no installation prerequisites.
- 53:10Because if I prerequire a library, then that library has to get into a new environment before coverage.py can.
- 53:17And then we've got a chicken and egg problem.
- 53:19Yeah.
- 53:20It also gives me a principled reason to have written my own HTML template engine, which is fun.
- 53:26That sounds like a pretty challenging thing.
- 53:28Also challenging.
- 53:30Another runtime, if you want to call it that, I guess, that seems interesting is Cython.
- 53:36Does this have any...
- 53:37But it seems to me my first guess is it wouldn't work with Cython.
- 53:40But what's the story there?
- 53:41I should really know more about Cython.
- 53:43And in fact, it's been proposed to me that instead of writing C code for the trace function,
- 53:47I should write Cython for the trace function, which I've just been resistant to because, like I said, I'm inertial.
- 53:54So my understanding of Cython is that it compiles to C code.
- 53:57And then when you're running, you're running C code.
- 53:59So you need a C coverage measurement tool in order to understand what's happening there.
- 54:04Yeah.
- 54:05It compiles to a C file, which then compiles, like at least on Mac, to a .so file.
- 54:10And the .so file is running.
- 54:12Yeah.
- 54:12Right.
- 54:12Coverage.py won't be able to see any of that execution.
- 54:15A C coverage tool could.
- 54:17But then you'd have to figure out how to map that back to the Cython code that you're actually interested in.
- 54:24Which actually brings up an interesting point.
- 54:25One of the things that coverage.py has gotten in its 4.0 releases is plugins so that you can do coverage measurement of things that are not directly Python but result in Python.
- 54:39So, for instance, there's a Django coverage plugin that can measure the coverage of your Django templates.
- 54:46Because Django templates themselves have if statements and for loops in them.
- 54:51So that's code.
- 54:53You'd like to know if there's coverage there.
- 54:55And so there's a plugin, the Django coverage plugin, that works with coverage.py to basically understand how Django executes the templates.
- 55:04And take the raw Python information about the template execution and map it back to the lines in the Django template.
- 55:12So that you can get sort of a red-green Christmas tree of your Django template.
- 55:18Oh, that's cool.
- 55:19Yeah, it's very cool.
- 55:19And it was getting a little stale, but I've just gotten a new maintainer to take it over.
- 55:24And so they just had a release today, actually.
- 55:26Oh, great.
- 55:27Yeah.
- 55:27All right.
- 55:28That's interesting.
- 55:28It seems to me like there's probably enough information there, but that it's really not super straightforward.
- 55:33For Cython.
- 55:34But maybe plugins would do it.
- 55:36Yeah.
- 55:37I mean, the C file is a huge, massive thing relative to the size of the Python code.
- 55:41But I do believe it has line numbers that map back to the Python.
- 55:44So it's possible, I guess.
- 55:46The reason that reminded me of the plugins was because I wanted to also make a Mako plugin.
- 55:51So Mako is another HTML templating engine.
- 55:53And Mako and Django work completely differently.
- 55:56Mako actually compiles your template to a Python file and then runs the Python file.
- 56:01Whereas Django templates, there's Python code that's actually running on an abstract syntax
- 56:06tree of the template.
- 56:07It's sort of.
- 56:08So in that sense, Mako is kind of compiled to Python and Django is kind of interpreted in
- 56:12Python.
- 56:13And so the plugin technologies had to be completely different.
- 56:16And the Mako template has pointers back to the lines in the template, but they're a little
- 56:23bit inaccurate.
- 56:23And so I was frustrated at not being able to sort of close that loop because of a limitation
- 56:29in Mako.
- 56:30And Mako seems kind of unmaintained at this point.
- 56:33So it kind of didn't go anywhere.
- 56:34Yeah.
- 56:35Yeah.
- 56:35Interesting.
- 56:35It's cool that those are there, though.
- 56:37People want to use them.
- 56:38Yeah.
- 56:38Yeah.
- 56:39It's very gratifying.
- 56:40Yeah.
- 56:40So last question before we run out of time, because that's where we are.
- 56:44OK.
- 56:44What's next?
- 56:44What are the upcoming features?
- 56:45Right.
- 56:46So I mentioned that I've got some alpha.
- 56:48I've already released 5.0 alpha 2.
- 56:50The big feature that's coming up is something that I colloquially call who tests what, which is
- 56:56instead of just telling me that that line got run, tell me which tests actually
- 57:00ran that line.
- 57:01And people are interested in this for all sorts of reasons.
- 57:04But it can require some significant changes to the core of coverage.py.
- 57:09It's going to present some challenges in that if you have 1,000 tests, then you have to collect
- 57:14roughly 1,000 times as much data now because you're essentially – it's as much data as
- 57:18if you did a separate coverage run for every single one of your tests.
- 57:22And there are some tools out there that kind of do this already by doing exactly that, running
- 57:26coverage independently around each test.
- 57:29But coverage will do it differently because it's all bundled together.
- 57:32So there's an alpha out already now, alpha 2, which has switched the data format from a
- 57:38JSON file to a SQLite database.
- 57:40And I needed the SQLite database because if I'm going to dump 1,000 times more data, I want to
- 57:45dribble it into a database.
- 57:46And I want to give you a database you can query because I don't know how people are going to use
- 57:49the data.
- 57:49Do indexes on it and all that stuff.
- 57:51I have no idea how to report on this data because I'm not going to make an HTML file that for every
- 57:56line of your source code has the names of the 200 tests that ran it.
- 58:00Yeah.
- 58:01So we need to get this out there and into people's hands and start seeing what people
- 58:04do with it.
- 58:04That's the big feature that's coming up.
- 58:06That's cool.
- 58:07One of the most interesting things I think you could do with that, just listening to you
- 58:11describe it, would be if I have 1,000 tests and I change a little bit of my Python code and
- 58:15only three of those tests should actually interact with those lines, theoretically, I could just
- 58:21run those three tests to retest it, not all 1,000, which would be dramatically awesome.
- 58:26Right.
- 58:26Exactly.
- 58:27There are also some tools out there that do that now, too.
- 58:30Kirk Strausser has one, and the name of the tool escapes me at the moment, but he's got
- 58:36his own trace function that essentially does a mini coverage measurement of his own to get
- 58:41at that information.
- 58:42The thing I'd be interested to experiment with that information is, what if my code is over
- 58:48tested?
- 58:48If my test suite is taking too long, maybe that's because I've got 100 tests all getting
- 58:55at the same information, and if I could reduce it to 10 tests, then it would take a tenth
- 58:58of the time.
- 59:00And I honestly have no idea whether who tests what will give me the information that I'd
- 59:04need for that, but it'd be interesting to play around with.
- 59:07Yeah, it sounds really positive.
- 59:08Yeah, I'm looking forward to having the data, at least.
- 59:10Yeah, cool.
- 59:11All right, let me ask you the two final questions before you get out of here.
- 59:15Okay.
- 59:15You hinted at this at the beginning.
- 59:17If you're going to write some Python code, what editor do you use?
- 59:20I use Vim.
- 59:20I mentioned earlier that I am old, and I do not use Vim because I'm old.
- 59:26I've only been using Vim for about 10 years.
- 59:28So I got to it late in life, but it really suits my low-tech mentality, I think.
- 59:34Beautiful.
- 59:34And beyond coverage.py, what is a notable Python PyPI package that maybe people haven't heard
- 59:44of being like, this is awesome, you should know about this?
- 59:46Yeah, so I'll tell you the one.
- 59:47So this is the very last thing that happened to me at PyCon this year.
- 59:51I was literally almost dragging my suitcase out of the convention center to go catch a
- 59:55plane when I stopped by the PyLint sprint and heard about a package called checkmanifest.
- 1:00:03And it only does one little thing, but it's a thing that no one really cares about well
- 1:00:08enough to get it right themselves.
- 1:00:09So it's great to have a helper, which is it tells you whether the manifest.in that you wrote
- 1:00:14for your setup.py has all of the files from your working tree or not.
- 1:00:19Okay.
- 1:00:19So really sort of a check on your package before you ship it off.
- 1:00:24Exactly.
- 1:00:24And packaging is one of those things that everyone hates.
- 1:00:27It's no one's first love.
- 1:00:29No one wants to think about it.
- 1:00:30It's very confusing.
- 1:00:31What's a manifest.in?
- 1:00:32Why is that different than package data?
- 1:00:34I don't get it.
- 1:00:35And so checkmanifest, it just does one little thing and it's beautiful.
- 1:00:38And I had never heard of it before.
- 1:00:40And it seems like people should be screaming it from the rooftops.
- 1:00:43Yeah, awesome.
- 1:00:43Well, that's exactly the thing I'm looking for.
- 1:00:45Thanks for sharing that.
- 1:00:46That's right.
- 1:00:46A second library is tqdm, tqdm, which is a progress bar library, which is very cool.
- 1:00:52Yeah, I really like that one.
- 1:00:53I've been using those types of progress bars lately and they're pretty cool.
- 1:00:56Yeah.
- 1:00:57Nice.
- 1:00:57Okay.
- 1:00:58Final call to action.
- 1:00:59People are excited about coverage.
- 1:01:00Maybe even they're excited about edX.
- 1:01:02What do you want to leave folks with?
- 1:01:03Read about coverage.
- 1:01:04I've got some docs that I think are good, but that's because I wrote them.
- 1:01:07So I can confirm that.
- 1:01:08They were very good.
- 1:01:09I went through the docs to do a lot of research for this show and they were at the right level.
- 1:01:13What I wanted to know, but not so much.
- 1:01:15I couldn't get through them.
- 1:01:16It was perfect.
- 1:01:16Good.
- 1:01:17That's good to hear.
- 1:01:18I hang out.
- 1:01:19I hang out on the Python IRC channel and I love to see people there.
- 1:01:21I think it's a great way to connect with people.
- 1:01:24I like to think of it as a nice IRC channel.
- 1:01:27So if you've been to IRC and didn't like it in the past, try the Python IRC channel on Freenode.
- 1:01:32It's openedx.com.
- 1:01:34Openedx.org.
- 1:01:35I'm nedbatt on Twitter.
- 1:01:37You can follow me.
- 1:01:38I've got a blog that I've been running for far too long.
- 1:01:41If you want to read what I thought about 16 years ago, get in touch.
- 1:01:44You know, I like hearing from people.
- 1:01:46Yeah, that's awesome.
- 1:01:46The internet is written in ink, right?
- 1:01:48All that stuff's still there.
- 1:01:49So I definitely find your blog interesting.
- 1:01:52There's some topics in there I would love to have you back on to talk about.
- 1:01:55But for this one, we're going to have to just leave it here, I think.
- 1:01:58So thanks for being on the show, Ned.
- 1:01:59Sure.
- 1:01:59Thank you, Michael.
- 1:02:00This is great.
- 1:02:00You bet.
- 1:02:01Bye.
- 1:02:01Bye.
- 1:02:01This has been another episode of Talk Python To Me.
- 1:02:05Our guest on this episode has been Ned Batchelder, and it's been brought to you by Brilliant.org
- 1:02:10and Manning.
- 1:02:11Brilliant.org wants to help you level up your math and science through fun, guided problem
- 1:02:17solving.
- 1:02:17Get started for free at talkpython.fm/brilliant.
- 1:02:21Learning Python doesn't have to be overwhelming or intimidating.
- 1:02:25Check out Get Programming by Annabelle from Manning.
- 1:02:28Just visit talkpython.fm/manning and use the code belltalkpy to get 40% off.
- 1:02:33Want to level up your Python?
- 1:02:35If you're just getting started, try my Python jumpstart by building 10 apps or our brand new
- 1:02:41100 days of code in Python.
- 1:02:42And if you're interested in more than one course, be sure to check out the Everything
- 1:02:46Bundle.
- 1:02:46It's like a subscription that never expires.
- 1:02:48Be sure to subscribe to the show.
- 1:02:51Open your favorite podcatcher and search for Python.
- 1:02:53We should be right at the top.
- 1:02:54You can also find the iTunes feed at /itunes, Google Play feed at /play, and
- 1:03:00direct RSS feed at /rss on talkpython.fm.
- 1:03:04This is your host, Michael Kennedy.
- 1:03:05Thanks so much for listening.
- 1:03:07I really appreciate it.
- 1:03:08Now get out there and write some Python code.
- 1:03:10I'll see you guys next time.