Recent Posts (Page 9)

Git: An alias to speed up fixing merge conflicts

There you are, minding your own business, rebasing your branch and… surprise! A merge conflict!

Read more...

Shell: search your virtualenv for mystery error messages

Sometimes error reporting loses details, and you don’t know where an error message comes from. In such cases, I have often found that the fastest way to track down the source is to search through the project and its virtualenv.

Read more...

pre-commit: How to run hooks during a rebase

pre-commit uses Git’s hook system to run tools when you commit. Unfortunately, Git doesn’t run any hooks when making a commit during a rebase. This can lead to you rebasing a branch and not realizing some code needs fixing, at least not until your CI system runs pre-commit (say, with pre-commit.ci).

Read more...

Git: How to set up a global ignore file

Have you ever accidentally committed a bunch of junk created by your OS, like Thumbs.db files from Windows, or .DS_Store files from macOS? Or, have you joined a project, and for one of your first commits, added rules to the .gitignore file for your text editor’s project files? If so, this post is for you! You can avoid such pain or busywork by making a global ignore file.

Read more...

Git: automatically stash while rebasing or merging with autostash

Imagine you’re part way through making some changes, and you realize that you need to rebase or merge. You can:

Read more...

Why does Python log a DeprecationWarning saying “invalid escape sequence”?

Take this code, which prints a shruggy man:

Read more...

Git: How to undo commits

To errr is human, to undo computer can help.

Read more...

Git: How to automatically create upstream branches

You started a new branch, worked hard on initial commits, and you’re ready to send it for review. You try to push and:

Read more...

Git: How to clean up squash-merged branches

Git hosts offer a “squash merge” option that merges a reviewed branch by combining all its commits into one. This leads to a linear Git history on your main branch, which is easier to understand and refer back to code reviews.

Read more...

Git: How to alias “master” as “main”

The Git community is gradually moving the default branch name from “master” to “main”, because the term “master” is hurtful to some people as it refers to slavery.

Read more...

Git: How to enable autocorrect

By default, if you mistype a Git command, it will list similar commands that you might have meant:

Read more...

django-upgrade Mega Release 1.11.0

I just released version 1.11.0 of django-upgrade, a tool for automatically upgrading your Django project code. This release contains a lot of new features and fixes, thanks to new contributors including those at the Djangocon Europe sprints. Let’s look at the top changes.

Read more...

Python type hints: use Mypy’s always-true boolean check detection

Sometimes code uses boolean checks on variables that can only be true. This is normally a sign of a mistake, either in the type hints or the implementation. Mypy has an optional check that can find such problematic boolean usage with its truthy-bool error code.

Read more...

Git: Show and copy commit SHAs

You can use git rev-parse to show the SHA of the current Git commit:

Read more...

Use WARMED to evaluate software engineering practices

In a lecture titled Where Does Bad Code Come From?, Casey Muratori introduced the acronym WARMED for categorizing the costs of code:

Read more...

Migrate PostgreSQL IDs from serial to identity after upgrading to Django 4.1

The Django 4.1 release notes feature this short, innocent-looking note:

Read more...

pre-commit: Various ways to run hooks

pre-commit’s main mode of operation is to run hooks against changed files when you commit. But you can also run hooks without committing, using pre-commit run.

Read more...

Python type hints: make Mypy disallow implicit optional types

The original type hint proposal, PEP 484, initially allowed implicit optional types in function signatures. That is, a parameter with a default value of None would have its type automatically interpreted as optional. For example, this signature:

Read more...

Python type hints: old and new ways to write the same types

As type hints have evolved, Python has added simpler, more succinct syntaxes. But you still need to know the older forms, since Mypy uses them when reporting types.

Read more...

Git: Rebase stacked branches with --update-refs

When working on a feature, you might split it into several stacked branches, so you can merge each one separately. But updating such branches can be annoying since you have to manage them independently. Git 2.38 (2022-10-15) made this management easier with the new --update-refs option, which can rebase a stack of branches at once. Let’s look at a couple of examples.

Read more...

Python type hints: exhaustiveness checking

Exhaustiveness checking is a very handy type checker feature. It ensures that all possible types of a variable are handled. If your code changes to add another possible type, you can guarantee that exhaustiveness-checked code paths handle the new case.

Read more...

How to implement a “dry run mode” for data imports in Django

In data import processes it’s often useful to have a “dry run” mode, that runs through the process but doesn’t actually save the data. This can allow you to check for validity and gather statistics, such as how many records already exist in the database. In this post, we’ll look at how to implement a dry run mode in Django by using a database transaction and rolling it back.

Read more...

Python type hints: lambdas don’t support type hints, but that’s okay

Python has no syntax to add type hints to lambdas, but that doesn’t mean you can’t use them in type-checked code. In this post we’ll look at how Mypy can infer the types for lambdas, based on where they’re used.

Read more...

How to Safely Pass Data to JavaScript in a Django Template

You want to pass your data from your Django view to JavaScript, in your template. And, you want to do it securely, with no risk of accidentally allowing malicious code injection. Great, this is the post for you!

Read more...

Python Type Hints: How to Gradually Add Types for Third Party Packages

Hynek Schlawack recently described graduality as Python’s super power: the ability to prototype in the REPL, and gradually add linting, type checking, and other practices to refine your code into maintainable, production-ready software. You can also apply graduality within tools, activating checks one at a time and fixing the resulting errors as you go.

Read more...