Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Requesting a Code Quality Analysis

Resource mentioned in this lesson: pypi.org/project/ruff

Music: Living Voyage by Kevin MacLeod is licensed under a Creative Commons Attribution 4.0 license. creativecommons.org/licenses/by/4.0

00:00 In the last lesson, you use Gemini to create documentation. In this lesson, you will use AI for another nice thing where AI can be very helpful, and that is spotting bugs in your code.

00:12 Now, in order to make Gemini efficient in spotting bugs, I want Gemini to use Ruff.

00:19 Ruff is a linter and code formatter that’s written in Rust. It means it’s super fast, and it works with Python. And that is actually one of the few packages that I have globally installed on my system.

00:31 However, it’s totally fine to install Ruff in your virtual environment.

00:38 And to install Ruff in your virtual environment, you could either now open a new terminal and make sure that the virtual environment is activated there, or you can use the shell mode while being in Gemini, and you can switch into the shell mode by pressing the exclamation mark.

00:57 And now you can, for example, type which python in order to check if your virtual environment is activated. So here it points to the Python runtime inside of my venv/ folder.

01:09 So that means I’m in the virtual environment, and now I can run pip install ruff knowing that it installs Ruff into the virtual environment.

01:21 Once Ruff is installed, you can press Escape to exit the shell mode, and you are back in the Gemini prompt. Let’s ask Gemini to analyze the code in this project and identify any potential bugs or, quote, quality issues.

01:39 And please use Ruff.

01:46 In order to use Ruff, Gemini needs to execute the ruff command. And this is a command where I’m somewhere between one and two, meaning: should I allow it once, or should I allow it always?

01:59 If you are in doubt, lean into the allow once, and I don’t know, like once you have allowed it once three times, you can move into the ‘Yes, allow always …’ mode.

02:09 But for now, let’s be safe and ask Gemini to ask every time.

02:17 Gemini is first running ruff check to check the project with the standard Ruff checks. And now it’s asking to use --select ALL, which is a more in-depth review of the codebase.

02:36 But instead of letting Gemini go ahead and fix those issues right away, I want a summary, and I want Gemini to tell me about the most critical issues. So here I select ‘No, suggest changes’ by typing: “Please give me a short summary of the most critical issues”.

03:01 And now it doesn’t take too long because it already had the output from Ruff, and it actually found 83 issues, but gives you a summary of the critical ones.

03:13 And what I like about an approach like this is that you are not just blindly following a linter and your code is magically fixed in the background, and you have a big diff maybe about the changes that happened, but you can actually use the AI to guide you a bit more into the findings that it has.

03:32 And then you can start asking questions about certain things, like why is this an error? etc. So here, from all the issues it finds four potential bugs, and four code quality and maintainability issues.

03:47 And in the potential bugs there is the third point that I find very interesting because it’s a common Python gotcha. And it says a mutable dictionary is used as a default function argument, which can lead to unexpected behavior between function calls.

04:05 That is something I want to have more information about. So let’s copy that line,

04:12 and ask Gemini, “Please go more into detail for this point”. And then we’re pasting the line: ‘A mutable dictionary is used as a default function argument, which can lead to unexpected behavior between function calls, which was found as a potential bug’.

04:36 And Gemini comes back with a nice rundown of what the issue here is. Let’s have a closer look at this output in the next lesson.

Become a Member to join the conversation.