Tip: Use keyword-only arguments in Python dataclasses

AI-generated summary of reporting by Simon Willison’s Weblog. Read the original for the full story.

Why it matters

Python developers can add fields to dataclasses and subclasses without breaking existing constructor calls, reducing refactoring risk in shared code.

Read this as

Simon Willison shares a tip from developer Christian Hammond on a Python feature called dataclasses, which are shortcuts for creating classes that hold data. By adding a setting called kw_only=True, the class only accepts arguments written by name, not by position. This means new fields can be added later, including in subclasses, without breaking existing code. The tip applies to anyone writing Python.

A short link post from Simon Willison's blog passes along a tip by Christian Hammond: Python teams should define dataclasses with @dataclass(kw_only=True). This forces all constructor arguments to be passed by keyword, so adding new properties later, including in subclasses, no longer risks silently breaking code that passed arguments by position. It is a free, zero-cost coding practice, not a product announcement; no adoption data, benchmarks, or tooling is provided.

Willison links a tip from Christian Hammond: creating a Python dataclass with @dataclass(kw_only=True) makes its constructor accept only keyword arguments. This prevents positional-argument coupling, so new fields can be added to the dataclass or subclasses without breaking existing call sites. The post is a brief link share dated 29th June 2025 with no code examples, version requirements, or caveats discussed.

Read the original

This summary exists to save you time, not to replace the source.

Key terms

Python dataclass kw_only Simon Willison Christian Hammond Datasette Ladybird

Was this summary useful?

One click, anonymous. It tells us which summaries to re-run.

Related reading