- Podcasts
- Python Bytes
- #337 Backtracking For a Package
About this episodeTopics covered in this episode: Ruff PyCharm plugin Writing Python like it's Rust Pip 23.1 Released - Massive improvement to backtracking…
- Ruff PyCharm plugin
- Writing Python like it's Rust
- Pip 23.1 Released - Massive improvement to backtracking
- Markdown Code Runner
- Extras
- Joke
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#337 Backtracking For a Package
Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds. This is episode 337, recorded May 23rd, 2023. And I am Brian Okken. And I'm Michael Kennedy. I want to thank everybody that's watching, but anybody that's listening, I encourage you to at least once in a while drop by YouTube and see the upcoming list of events at pythonbytes.fm/live to be part of the audience.
It's usually fun. It's usually Tuesdays at 11 Pacific time. Occasionally we switch. If you'd like to connect with us, we're all on Fosstodon, or at least the three of us, Michael and I on the show. It's mkennedy at Fosstodon and @brianokken and at Python Bytes. And with that, let's jump into our first topic.
Let's jump into it. It's not going to be a rough one, is it? So the first topic is rough, which is obviously, you know, Charlie Marsh's project. It's very successful. People know that. It's not exactly rough. It's a way to use rough. And this one comes to us from John Hagen. John runs this project called Python Blueprint.
So he's been playing around a lot with that project and PyCharm and rough and realized that now there's a plugin for all the JetBrains IDEs, most notably PyCharm, called just rough. And the idea is that, you know, PyCharm has all these little squigglies and warnings and maybe even more importantly, the auto corrections.
And it will actually run, you could run ruff-fix as an action. And you can even run that when a file is saved just automatically. Like, hey, if there's stuff wrong with it, just fix it rough. Just do that for me. You can configure which version of rough is running. So basically the plugin lets you specify, do you use a global one, maybe managed by pipx, do you use a local one in a virtual environment?
I sort of feel like isolating that to a per project basis is the right thing. So that's what I'm doing, playing with this. And yeah, you can run it as a new process. You can specify a config option, like a YAML. I'm not sure what the format that is, but whatever the rough config file format is so that you can say, you know what?
I don't really care about the line length for this thing. So just ignore that and don't ever run that and so on. So yeah, you can even run it out of a WSL Windows subsystem for Linux. And there's some nice screenshots, as all UI things should have in there. You can even see some of the settings if you want to try it out.
So it has 4.8 out of 5 ratings and looks pretty new. But yeah, anyway, I think it looks like a good option. So people can check that out. Also, yeah, I'm really excited about it. I've installed it and going to give it a try. You can go check out the, it's open source, obviously. So you can go check out basically the repo for the plugin.
And John sent over to us and I'm linking to this thing where it says you can add additional PyCharm specific instructions for both black and rough. So he's got this section that shows you basically how to integrate both black and rough at the same time as automatic code formatters and PyCharm using the file watchers plugin.
So just follow along with the steps there. And even has NOC support. But yeah, cool. So if that sounds interesting to you, if you want to have kind of auto rough just built into PyCharm and you use PyCharm, then check this out. It looks cool. Thanks, John. Yeah, that's nice. And rough, of course, is written in Rust.
And I'd like to talk a little bit more about Rust. So there was an article I ran across called, it's from Koblo's blog. Don't know who, that's a first name or last name, but thanks, Koblo's. Writing Python like it's Rust. And what I haven't written Rust yet, but a little bit. Anyway, the thing I liked about this article was really basically he's going from Rust back to Python programming in both.
The obvious one, of course, is for function signatures. So we really want to, if it's not obvious, like there's an example with a find item with records and check. But what is that? What are those types of those things? So it's really helpful to your call, the people using your API to specify what the parameters look like or what you expect them to look like.
And there's so many options within Python now. And also that, too, return value. And here is an example. It's optional item. So I actually hadn't thought about that, of like using optional as a return type. That's pretty cool. Which means you can either, and you could probably do like item or none to say, it can return none or something.
That's a, it is good to, especially if it's something, if it's possible to return none, good to have that in the types. So that's the low hanging fruit. That's, I think a lot of people have gotten there yet already. The other thing that I found recently is I've got some types that are like tuples or dictionaries or specifically a specific kind of dictionary, like a string to any or even I use name tuples a lot, which are a little bit better than just your normal types.
But his recommendation is to go ahead and use data classes because they're more descriptive. even if it's similar information, you can write up a little data class. And then like in the example, instead of saying that you're returning a dictionary, go ahead and return like a person item, specific types.
And I'm kind of used to that from C++ of like writing little types because they're more, they're more descriptive and they're easier to read sometimes. So bringing this into Python is kind of a cool idea. The other, one of the things I thought was really awesome is this idea. There's an example using the, where he's using like these packet types.
And since I work with communications a lot, this, this really hit home of you have, you have, you can, a packet might be either a header or payload or a trailer. This is a Rust example, but you can do the same sort of thing within Python to say, I can have different data types using data classes to specify what kind of information you're going to get from these.
Yeah. Yeah. That is pretty interesting. I like that. It's, so is that if you're going to receive say a packet off a network and it had first the header bit and then the payload and then the trailer or something like that? Yeah. Or I'm like a function that takes, that can like count the bits in there or something like that.
And it can either be a header or a payload or a trailer. You could do something different with those and having a union type is pretty, pretty cool. Now within it, I'm not, and here's an example of what, how do you do with that? Deal with that. You'd still with inside, you'd have to either do pattern matching or if clauses.
I've been using a lot more pattern matching lately to be able to just, you know, this is, this is great for you. You take the packet type, but then you can match it against either a header or payload or trailer. This is a really clean way to have union types and then unpack them with the pattern matching.
It's a pretty cool way to deal with that. So. Yeah, that is actually pretty clean. That's one of the better examples of pattern matching that I've seen lately. And this is just like a third of the article, but the rest of it does talk about different ways to basically, we have a lot of these tools within our within Python now to make it more to utilize data types that we are used to in other languages.
And you can kind of write some really clean looking code and easier to read. So it's nice. Yeah, very nice. I like it. So Kim out there has a question in the audience. Ryan, is there possibly some overlap with writing Python after writing a bunch of C as you do? As in same way, Rust leads to some good ideas in Python.
Yeah. Well, I mean, that's, this, this is a, this person's writing it from the experience of Rust, but I'm looking at it going, this is a lot of the C++ stuff that I use. So it is, yeah, I'm looking at it. We're, we're actually, my team is being, is having a lot more fun with Python with the pattern matching statement because the, I know it can do a lot more stuff, but it really does.
Shall we move on? Sure. All right. Well, go ahead. Go ahead with your first. I know you had an announcement as well. You wanted to give out. No, I was just wondering, do we want to, today's episode is not brought to you by an external sponsor, but it's brought to you by us. It is brought to you by us.
And I'd like to hear some of the news from Michael. Well, the big news for me, which I have sort of put in as an extra now and then is the mobile apps. So people go out there, download the mobile apps, talkpython.fm/apps. And in there, if you create an account or log in with your account, at a minimum, you'll find like six different courses that are free.
They're a great way to take it. They have some free ones and they also are the best way to take ones that you paid for as well. So four months of work. Finally, really, really nice apps that we got out there. I'm listening to a couple, watching and listening to a couple, a couple of courses right now.
So my book, Python Testing with pytest, second edition. I've had a lot of great feedback of saying I didn't. People saying they read the first one and it was good. But the second one really nailed. They really got some of the concepts in. So I'm glad that I wrote the second. So the news, I just got this morning, an email from Pragmatic saying that there is a sale right now.
Well, that's us. How about a new topic? Yeah, thank you everyone for supporting the show by supporting our work. Let's talk about pip. So this is about a month old. I've been looking to grab a slot to talk about it, but there's been other sort of higher important stuff. But I do want to point this out.
And backtracking is an algorithm that is part of the requirements resolver. So if you say, you know, pip install requests Flask and, you know, something else. PIP has to say, okay, well, we know what those are. But both requests and Flask may depend on some library. I don't think that's true, but I don't think there's an intersection there.
Like that, that shared, potentially shared library itself has another thing that it depends on. So the article or not article, Reddit post says, you know, for example, let's say you require package A and B. The latest versions of A and B are downloaded. PIP checks their requirements and it finds that A depends on C version 2, but B depends on C version 1.
We couldn't find one. That was a good fit. But that's no longer an option. I have very mixed feelings about that. I love that it tries to be more strict and correct. But at the same time, the alternative or the tradeoff is we guarantee correctness 100% of the time in terms of a version match for all the transitive dependencies across all the different things.
And instead of saying, well, install something that doesn't work or may not work because there might be some functionality that you may or may not interact with that is incompatible version-wise, we're just going to say your application cannot run. It's impossible for your app to use these dependencies and run.
Personally, I would prefer to say, huge warning, this is not a great idea, but at least you can take a shot at it rather than it's impossible for you to run this application. But, you know, that's the tradeoff pip made. And because of that, it has to be even better at tracking down these version matches as best it can because the alternative is your application cannot be installed and run.
So there's a lot of interesting history here, but it talks about some of the algorithms that have been used and points out that pip now separates out the resolution logic into a library called ResolveLib. And it was discovered that there was an error, logical error that both for performance and for correctness as well, you know, better backtracking technique called back jumping and an actual error that were fixed and implemented in ResolveLib and now come out in 23.1.
Yeah. Yeah. Just this morning, I got a message. Sorry, we cannot install your website because some, you know, Sentry requires this version of EuroLib 3 and Request requires a different version of EuroLib 3. So you can't run your app. Goodbye. It's like, come on. I'm sure that it's not actually using the conflicting.
And one thing that this does point out here is traditionally people haven't had to worry about this. So you'll see things like one of the dependencies will say it has to be exactly like 1.2.3. When really what it means is it's got to be lighter than 1.2, you know, and they've just pinned it overly tight.
And you end up with this crash where, you know, for sure that it wouldn't actually be a problem in your situation. Like, how do you know? Well, because before pip 20.1, it was running, you know, those kinds of things. And so there's this little bit of, you know, I know it's great that it's trying to be more accurate and precise, but sometimes I'd rather have at least an option than none.
And so, I don't know, there's a whole interesting discussion down here. People can check it out in the comments. It devolves into a debate about Python 2.3 for a little while, which is weird. And then just skip that. That's not productive reading. But there's some interesting conversations going on there.
And also, before we move on really quick, Brian, there is, I think it's Damien, as I said, points out that there is a PEP 658, which is accepted. Or what version of Python is this? I don't know, 2021. So a couple versions ago. That allows the metadata. It used to be that pip would actually have to download and install a package just to see what the dependencies were.
Yeah. A couple of comments, which I kind of agree with. It'd be from Grant. It'd be nice if there was a worn unresolvable or something to keep the old behavior. I agree with Grant on that, yeah. So I guess this is a reminder to library authors as well that your dependencies, you might know that you have a lower limit on some dependency that you need.
You need version like 1.2 of this library or above. Think about it. I prefer to have libraries in their dependencies, the transitive dependencies in a lower bound and not upper bound version because, I mean, you don't know what the upper bound is unless you do. Unless there really is, there was a breaking change and you really know that there's a break.
And in that scenario, this other library does something crazy. It crashes unless it's high enough. If I'm never using that authentication mechanism, I'm never going to. So like, yes, it is actually a break and change, but it's not a break and change for me. Oh, yeah. In my use case of that, the combination of those two things, right?
Like that's, that's kind of where I've been thinking about this. Oh, yeah. Interesting. Yeah. Because there's a lot of like a lot of Swiss Army knife libraries out there and you're not using all of them. You probably, it's very unlikely you're hitting 100% surface area of a thing and its dependencies, right?
Exactly. Yeah. Interesting. Yeah. Yeah. All right. Well, anyway, this is, this is an improvement. I feel like we're kind of like debating the 20.3 debate. This is an improvement on the stuff that's already been decided to be done. So 23.1 is a good way to go. Yeah. It's good. Last item was a kind of a cool tool called Markdown Code Runner.
So this, this did remind me of a tool that I've known before. So the Markdown Code Runner is a package that automatically executes code blocks within a Markdown file. It's, it can include hidden code blocks. So you can have, the code blocks can be in comments. And so you can't see it in the Markdown file.
It just runs it. So it runs the snippet and then you can have the output show up somewhere. So this, in the example, let's run to an example. You've got a little, a code block that says Python, but instead of just Python, you say, you know, you got your back ticks and then you say Python space Markdown dash code dash runner.
Like if you want to show a, I was thinking if you wanted to show what the directory looked like with the tree command, you can go ahead and run tree within, within a Markdown file and run it or other stuff. Has, has, has several examples that they think would be neat. Things like, oh, I don't know, like diagrams or tables or various things that you might want to output with Python visualizations.
You can use that. This is really cool. Yeah. It did kind of remind me of cog, but the syntax is a little different. So cog from Ned Batchelder has, is a similar sort of thing. You throw, you throw some code in, in place with some special tags. He uses like a three bracket tags instead of an actual code block.
So the thing I kind of like about this is a lot of times I actually do want to show the code block. So you can go ahead and show the code block and then run it. So that's neat. So some, some, it's fun. I tried it a little bit. There's an example of using rust. So you can have actually running rust also.
I couldn't get the rust example to work, but I got the markdown and the, or the Python and the bash examples to work. So actually pretty cool. The, within the documentation, it also talks about running this as a Git lab or a, yeah, get, get, get, GitHub, a CI snippet. So, and that, that'd be a great place to do it is to just rerun your, rerun, like reproduce the code output from your readme on the new code.
So kind of a cool idea. Yeah. My first thought was, Oh, I have a code example and it says the output is this, like rather than trying to maintain those, just let it put the output there. But it's also could be useful for just, I have a little Python bit of code that generates some other output that is, is useful, right?
That's all of it, right? That's all the main topics. That is our main topics. Yes. All right. I have a few extras to throw out here. Let's jump over there real quick. So same person who pointed out the rough plugin, John Hagan also pointed out, you know, I had talked previously about how I wish pip would update itself when I create a virtual environment by default.
So if I say Python 3.0. So if I say Python 3.0. So if I say Python 3.0.m. V and V and V, the very next thing after activating it is your pip is out of date. My God, geez. Okay. So hence, that's partly why my first thing to do is always upgraded is just to not see the warning more than anything, honestly.
But pointing out that since Python 3.0.m. But pointing out that since Python 3.9, there's an additional option you can pass to the virtual environment creation story. So instead of just dash M, V, and V and V directory, you can also pass --upgrade depends, which will automatically do that upgrade of pip and dependencies as well.
Not quite. Because I also want to install things like pip-tools and a couple other things that this doesn't include. So I'm like, I still would have to write that line. So I'll just leave it. But anyway, still, it's really nice to have --upgrade depths. You want it to have a shiny new virtual environment all the time.
Yeah. And one of the things I really love that came in like a few versions ago is the if you do the --prompt and give it a dot. It uses the directory name. Yeah, that's really excellent. It names the virtual environment containing directory names. So it has like the name of the virtual environment is the name of your project often, which is great.
I didn't really get any feedback. So I'm going to instead pay it forward to other people out there who might be listening to my ride. So some really cool apps on X off-road. You can go through and find, you just click a spot on the map, and it'll show you, like, here's all the public legal trails for you to go tear around on.
And they're even rated, like, 5 out of 10 or 7 out of 10 with pictures and distance and challenges. So you can decide up front whether or not you want to go down that path, I suppose. There's also GAIA, G-A-I-A, which is a similar thing. And then backcountry discovery roads, which allow you to find, like, how do I traverse my state, at least in the U.S., or similar things in Europe with the Tet.
Neat. Cool. Yeah. Cool. All right. A joke. You got a joke for us. Yeah. I can't remember where I found this, but there's a website called userinyourface.com. So instead of user interface, userinyourface.com. So you just have to fill it out. So... It's like an anti-pattern, a whole combination of anti-patterns into one UI.
To play the game, simply fill in the form as fast and accurate as possible with a button that says, No. Big green button. So it says, Please click, underline, score, underlined click here to go to the next page. And the next page is highlighted. And the trick is, the only thing that you can click on is the here button.
It's not a button. It's just, that's it. Okay. And then it... Okay. The site uses cookies. Is that a problem for you? Yes. Yes doesn't do anything. Okay. No, not really. That goes away. Can we help? There's a help field with no enter. Just send the bottom. One of those dreadful chat messages that pops up.
Next. Oh, it says already selected. I do not accept. So you have to uncheck it to accept the terms and conditions. What are the terms? Yeah, okay. Good luck getting out of that dialogue. You got to scroll down to accept them now to get out of it. Okay, well, we're stuck now. Because you can't even...
Okay, this is terrible. Anyway, I did get through it. The fastest time I have so far is like five minutes. People are going to speed run the in-your-face interface. Yeah. I'd love to see a speed run to see how fast... If somebody can really get through it in like a couple minutes, please do a video.
