Recent Posts (Page 11)

Python type hints: upgrade syntax with pyupgrade

A couple of recent PEPs have made writing type hints easier:

Read more...

Python type hints: what’s the point of NoReturn?

Sometimes functions never return, for example by always raising an exception. For such functions’ return types, we can “get away” with using None, but it’s best to use the special NoReturn type). This allows Mypy to better find unreachable code (as covered previously). It also shows future readers that the lack of return is intentional.

Read more...

Python type hints: use Mypy’s unreachable code detection

I recently discovered Mypy has a secondary function as an unreachable code detector. This feature is a great way to highlight places bugs may be hiding, as code paths that can’t possibly run normally show a logical error.

Read more...

Python type hints: duck typing with Protocol

Duck typing says “if it quacks like a duck, treat it like a duck.” Type checking seems at odds with duck typing: we restrict variables to named types (classes), allowing only that type or subtypes. This seems like it would stop us from passing in arbitrary objects that can “quack the right way”. But since PEP 0544, we can declare “duckish” types with typing.Protocol.

Read more...

Python type hints: narrow types with isinstance(), assert, and Literal

Type narrowing is the ability for a type checker to infer that inside some branch, a variable has a more specific (narrower) type than its definition. This allows us to perform type-specific operations without any explicit casting.

Read more...

Python type hints: specify a class rather than an instance thereof

In a type hint, if we specify a type (class), then we mark the variable as containing an instance of that type. To specify that a variable instead contains a type, we need to use type[Cls] (or the old syntax typing.Type).

Read more...

Python type hints: enable postponed evaluation with __future__.annotations

Python 3.7 added a new “postponed evaluation” mode for type hints. In this post we’ll cover how it changes the behaviour of type hints, how to activate it, and the outstanding problems.

Read more...

Python type hints: debug types with reveal_type()

When working with type hints, it’s often useful to debug the types of variables. Type checkers allow you to do this with reveal_type() and reveal_locals().

Read more...

Python type hints: fix circular imports

Circular imports are always annoying when they arise in Python, and type hints make them more common. Thankfully, there’s a trick to add circular imports for type hints without causing ImportErrors.

Read more...

Python type hints: Create a TypedDict with non-identifier keys

Using TypedDict is a great way to type code that passes around dictionaries. The default way to create a TypedDict is with its class-based syntax:

Read more...

Python type hints: *args and **kwargs

When I started writing type hints, I was a little confused about what to do with Python’s variable argument operators, * and ** (often called *args and **kwargs). Here’s what I figured out.

Read more...

Python type hints: How to Use TypedDict

Take this function:

Read more...

How to Build a Webhook Receiver in Django

A common way to receive data in a web application is with a webhook. The external system pushes data to yours with an HTTP request.

Read more...

Using Django Check Constraints to Limit the Range of an IntegerField

Another way to use database constraints via Django’s CheckConstraint class.

Read more...

Python type hints: Use object instead of Any

When starting out with Python type hints, it’s common to overuse typing.Any. This is dangerous, since Any entirely disables type checking for a variable, allowing any operation.

Read more...

How to List All Time Zones in Python

Python 3.9 introduced zoneinfo into the standard library. This module reads your operating system’s copy of the tz database. On older versions of Python, you can install the backports.zoneinfo package to use zoneinfo without upgrading.

Read more...

Three uses for functools.partial() in Django

Python’s functools.partial is a great tool that I feel is underused.

Read more...

Disabling FLoC, Google’s new advertising technology

Google has started rolling out FLoC, currently to 0.5% of Chrome users, and some sites are already disabling it. In this post we’ll cover what FLoC is, who’s disabling it, why, and how to do so on a Django site.

Read more...

How to set the new COEP, COOP, and CORP security headers in Django

Here are three new security headers on the block:

Read more...

Introducing the heroicons Python Package

heroicons is a free SVG icon set for your websites, from the creators of tailwindcss. SVG icons are great - they’re small, they sit inline in your HTML, and you can scaled and colour them with plain HTML and CSS. And heroicons is a great icon set - minimal, clear, and consistent.

Read more...

django-feature-policy is now django-permissions-policy

I created django-feature-policy in 2018 allow Django projects to control the draft security header Feature-Policy. Feature-Policy allows your site to restrict which origins can use some sensitive browser features, such as the the payments API or access to the webcam. This is valuable if you’re using any third party JavaScript. Whether such JavaScript comes from npm or an external script tag, you can protect against it doing some bad things with your users.

Read more...

Reindexing all tables after upgrading to PostgreSQL 13

I recently upgraded my client ev.energy to PostgreSQL 13. The first feature listed in this version’s release notes is “Space savings and performance gains from de-duplication of B-tree index entries”. I reindexed all tables after the upgrade to take advantage of this deduplication and saw index storage savings of up to 90%.

Read more...

Django: convert a TestCase from using setUp() to setUpTestData()

Django’s TestCase class provides the setUpTestData() hook for creating your test data. It is faster than using the unittest setUp() hook because it creates the test data only once per test case, rather than per test.

Read more...

Using Django Check Constraints to Prevent Self-Following

Another way to use Django’s CheckConstraint class to ensure your data is valid. Based on my answer to a question on the Django forum.

Read more...

Improve your Django experience with IPython

IPython is an improved Python shell for interactive use. It has many great features such as syntax highlighting, autocomplete, and powerful “magic commands”. I love it, use it on every project, and use the IPython prompt for examples on my blog.

Read more...