Recent Posts (Page 10)

New Testing Features in Django 4.0

Django 4.0 had its first alpha release last week and the final release should be out in December. It contains an abundance of new features, which you can check out in the release notes. In this post we’ll look at the changes to testing in a bit more depth.

Read more...

Truncating my blog posts with Python’s HTMLParser

I recently converted this blog to Pelican, a Python powered static site generator. On the way I added a few customizations. One customization is a Jinja template filter to truncate a post’s HTML as a summary, using Python’s HTMLParser class. Here’s how I wrote it.

Read more...

How to Fix Pip “Yanked Version” Warnings

Sometimes pip install will flag a warning saying “The candidate selected for download or install is a yanked version”. For example, if we install attrs version 21.1.0:

Read more...

Does Python support semicolons?

Many languages follow the syntax of C, and use semicolons to indicate the end of a statement. For example, in JavaScript:

Read more...

Why does Python log a warning for “invalid decimal literal”?

Take this function:

Read more...

Introducing django-upgrade, a tool for upgrading your Django projects

Django deprecates a small list of features with every feature release, requiring us to update our projects, which can be monotonous. Today I’m announcing a new tool I’ve created, django-upgrade, that automates some of this drudgery for us all.

Read more...

I converted my Lambda@Edge Function to CloudFront Functions

When Lambda@Edge first came out, I added it to my blog’s CloudFront distribution in order to add security headers. Then, when Lambda@Edge added Python support, I converted my function from JavaScript to Python.

Read more...

Three Cheers for blacken-docs

Black is the de facto standard code formatter for Python, and these days I use it on all my projects. blacken-docs is a tool that also allows you to apply Black to code samples in your docs. I recently rolled it out on my projects to great effect.

Read more...

This Blog Is Now a Pythonic Pelican-Powered Publication

I started this blog in 2014 using the popular Jekyll. Whilst it served me well, I’ve wanted to migrate to a Python-based tool for a while now, for a few reasons:

Read more...

Python type hints: use cases for the types module

Writing type hints gives us some familiarity with the typing module. But Python also includes the similarly-named types module, which can also come in handy. Let’s look at the history of these two modules, some use cases of types, and one way in which it’s not so useful.

Read more...

Python type hints: types for regular expressions

Python’s re module lets us search both str and bytes strings with regular expressions (regexes). Our type checker can ensure we call re functions with the correct types, thanks to some parametrized classes.

Read more...

Python type hints: vary return type based on an argument

Here’s a recipe that combines typing.Literal with @overload to define a function that switches its return type based on the value of an argument.

Read more...

Python type hints: how to avoid “the boolean trap”

“The Boolean Trap” is a programming anti-pattern where a boolean argument switches behaviour, leading to confusion. In this post we’ll look at the trap in more detail, and several ways to avoid it in Python, with added safety from type hints.

Read more...

Python type hints: how to use typing.Literal

To put it tautologically, type hints normally specify the types of variables. But when a variable can only contain a limited set of literal values, we can use typing.Literal for its type. This allows the type checker to make extra inferences, giving our code an increased level of safety.

Read more...

Django 3.2 Update for “Speed Up Your Django Tests” Released

I released my book “Speed Up Your Django Tests” over a year ago, in May 2020. Since then, we’ve seen two major Django releases, including a whole bunch of test-related changes, some of which I worked on as part of the book.

Read more...

Python type hints: how to use typing.cast()

Python’s dynamism means that, although support continues to expand, type hints will never cover every situation. For edge cases we need to use an “escape hatch” to override the type checker.

Read more...

Recent Updates to time-machine

time-machine is my library for mocking the current date and time in Python tests. It’s now over a year old, and I just released its version 2.2.0, so I thought it would be nice to summarize recent changes.

Read more...

Python type hints: types for a context manager

Python’s context manager protocol has only two methods, with straightforward types. But when it comes to adding accurate type hints to a context manager, we still need to combine several typing features. Let’s look at how we can do this for the two different ways of making a context manager.

Read more...

How to use Python’s HTTPStatus with Django

A “magic number” is the anti-pattern of using a number directly rather than storing it in a descriptive variable name. In web code HTTP status codes are often used as magic numbers, perhaps because web developers memorize common codes such as 200 and 404. In Python, we can avoid such magic with descriptive references from the standard library’s http.HTTPStatus enum.

Read more...

Python type hints: three somewhat unexpected uses of typing.Any in Python’s standard library

When we add type hints, we can find our desire for strictness in tension with Python’s flexibility. In this post we’ll explore three groups of functions in the standard library that I naïvely expected to use narrow types, but due to some edge cases, instead use Any.

Read more...

Python type hints: narrow types with TypeGuard

I previously covered type narrowing using isinstance(), assert, and Literal. In today’s post we’ll cover TypeGuard, a new special type that allows us to create custom type narrowing functions.

Read more...

DjangoCon Europe 2021 Sale of “Speed Up Your Django Tests”

I released my book Speed Up your Django Tests (SUYDT) just over a year ago, on the 18th May. It’s had a great reception, with 379 customers so far, many writing in to say how it has improved their test suites’ performance and readability.

Read more...

Python type hints: how to use @overload

Sometimes the types of several variables are related, such as “if x is type A, y is type B, else y is type C”. Basic type hints cannot describe such relationships, making type checking cumbersome or inaccurate. We can instead use @typing.overload to represent type relationships properly.

Read more...

Python type hints: manage “type: ignore” comments with Mypy

It seems inevitable that large projects need some # type: ignore comments, to work around type checking in tricky cases. I’ve found Mypy has a few options to make such ignore comments more precise and manageable.

Read more...

Python type hints: Mypy doesn’t allow variables to change type

Mypy does not allow variables to change type. I found this a little bit of a shock at first, but after I adapted, I found my code was more readable.

Read more...