Recent Posts (Page 3)

pre-commit: Various Ways to Run Hooks

Around and around the commit cycle goes…

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

Optional? ¯_(ツ)_/¯

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

This is how evolution works, right?

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...

How to rebase stacked Git branches

My mama always said, Git commits are like a stack of pancakes (generated by Stable Diffusion)

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 each one. Git 2.38 (2022-10-15) makes such updates easier, with the ability to rebase a stack of branches at once, with the new --update-refs. Let’s look at a couple of examples.

Read more...

Python Type Hints - How to Do Exhaustiveness Checking

Well, that looks exhuasting.

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

This ram ain’t running anywhere fast, dry or not.

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

A tangle of lambdas (generated by Stable Diffusion)

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

Let’s avoid Honk-TML injection!

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

Gradually evolved cephalopods.

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...

Use partial() With Django’s transaction.on_commit() to Avoid Late-Binding Bugs

I am rather partial to a nice plant.

Django’s transaction.on_commit() allows you to run a function after the current database transaction is committed. This is useful to ensure that actions with external services, like sending emails, don’t run until the relevant data is definitely saved.

Read more...