archive2026

Python 3.15: what changes for everyday code

Python 3.15.0 was released on 9 October 2026. The changes I think matter most for working code: UTF-8 by default, lazy imports, the new sampling profiler, frozendict and sentinel, typing updates, the JIT, and the removals that can break things. Plus my upgrade plan.

8 min read#dev#python

Python 3.15.0 was released on 9 October 2026 (release page). The final was planned for 1 October and moved one week later, after some last-minute release blockers around lazy imports. Today I installed it and read the full “What’s New In Python 3.15” page.

That page is long. These are the changes I think a working engineer should know about. I skip the C API and most of the small module changes.

1. UTF-8 is the default encoding (PEP 686)

This is the change most likely to change behaviour in old code without any error. When you call open() without encoding=, Python 3.15 uses UTF-8. Before, it used the locale encoding. On Linux and macOS that was usually UTF-8 already, so nothing changes there. On Windows it was often a legacy code page, so scripts that read or write text files there can now produce different bytes.

# 3.15: UTF-8, on every OS
with open("notes.txt") as f:
    text = f.read()

# Still the best style: say what you mean
with open("notes.txt", encoding="utf-8") as f:
    text = f.read()

# If you really need the old behaviour for one file
with open("export.csv", encoding="locale") as f:
    text = f.read()

encoding="locale" exists since Python 3.10. To switch the old behaviour back for a whole process, set PYTHONUTF8=0 or run with -X utf8=0. I would use that only as a short-term fix.

To find the places in your code that depend on the default, run your tests on your current Python with the encoding warning turned on:

python -X warn_default_encoding -m pytest

Every EncodingWarning points to an open() (or similar call) without an explicit encoding. I fixed these in my own scripts by adding encoding="utf-8", which works the same on all versions.

2. Explicit lazy imports are final (PEP 810)

I tested lazy imports in the beta and wrote it up in Python 3.15 beta: lazy imports for faster CLI startup. That post covers the syntax, the rules, and how to measure. Two notes for the final release:

  • The global switch now accepts all and normal (the default). -X lazy_imports=all or PYTHON_LAZY_IMPORTS=all makes every import lazy. In the beta post I also mentioned none. The final docs only list the two values, so check the docs before you put this in a script.
  • There are runtime functions too: sys.set_lazy_imports(), sys.get_lazy_imports() and sys.set_lazy_imports_filter(). I have not used them yet.

__lazy_modules__ is still the way to stay compatible with older Python.

3. A sampling profiler in the standard library (PEP 799)

This one I like a lot. There is a new profiling package:

  • profiling.tracing is the deterministic profiler that was cProfile. cProfile still works as an alias.
  • profiling.sampling is a new statistical profiler (the docs call it Tachyon). It reads the stacks of a process at a fixed rate, so the overhead is low.

The old pure-Python profile module is deprecated and planned for removal in 3.17.

Basic use:

# Run a script under the sampler and write a flame graph
python -m profiling.sampling run --flamegraph -o profile.html script.py

# Profile a module, with arguments
python -m profiling.sampling run -m mytool.cli report data.csv

# Attach to a running process for 30 seconds
python -m profiling.sampling attach --flamegraph -d 30 -o profile.html 12345

# Print the current stacks once (useful when a process hangs)
python -m profiling.sampling dump 12345

There are more modes and outputs: --mode wall (the default), cpu, gil and exception, and output formats like --pstats, --collapsed, --gecko and --heatmap.

The attach mode is the useful part for servers. You can look inside a slow worker without restarting it. It needs permission to read another process’s memory. On Linux that means root, CAP_SYS_PTRACE, or a relaxed ptrace_scope. On macOS it means root or a signed debugger entitlement. In a container you will probably need to add CAP_SYS_PTRACE. The profiler and the target must also run the same Python minor version. I have not tried attach inside Docker yet.

For quick one-off questions I used to install py-spy. For 3.15 projects I will try the built-in one first.

4. frozendict and sentinel built-ins (PEP 814, PEP 661)

Two new built-in types. Both replace small helpers that many projects write themselves.

frozendict is an immutable mapping. It is hashable if its keys and values are hashable, so you can use it as a dict key or put it in a set. It is not a subclass of dict.

DEFAULTS = frozendict(host="raspberrypi.local", port=1883)

DEFAULTS["port"] = 8883        # TypeError
cache = {DEFAULTS: "client"}   # fine, it is hashable

I will use it for module-level config constants, where I used types.MappingProxyType before.

sentinel creates a unique marker object with a readable repr:

MISSING = sentinel("MISSING")

import os

def read_setting(name: str, default: str | None | MISSING = MISSING) -> str | None:
    value = os.environ.get(name)
    if value is not None:
        return value
    if default is MISSING:
        raise KeyError(name)
    return default

Here None cannot be the marker, because a caller may want to pass None on purpose.

Compare with is. A sentinel keeps its identity when copied and can be used in type hints with |, for example int | MISSING. That last part is the real improvement over MISSING = object(), which type checkers could never describe well.

5. Typing: closed TypedDicts and TypeForm

From the typing changes, two are useful in application code.

PEP 728 adds closed and extra_items to TypedDict. A normal TypedDict allows extra keys. Now you can say that it does not, or what type the extra values must have:

from typing import TypedDict

class Reading(TypedDict, closed=True):
    sensor: str
    value: float

class Labels(TypedDict, extra_items=str):
    host: str

I want closed=True for JSON payloads, for example MQTT messages, where an unknown key usually means a bug.

PEP 747 adds typing.TypeForm, so a function that takes a type expression as an argument (a validator, a cast-like helper) can be typed correctly. Most of us will only see it in library signatures.

Before you use these, check that your type checker supports them. Runtime support in 3.15 does not mean mypy or pyright already understand every new form.

Also new, and small: unpacking in comprehensions (PEP 798). [*xs for xs in lists] flattens a list of lists, and {**d for d in dicts} merges dicts. Nice, but I will not rewrite working code for it.

6. JIT and free threading

The experimental JIT got faster. The release notes give a 7 to 8% geometric mean improvement on x86-64 Linux over the standard interpreter, and the What’s New page says 11 to 12% on AArch64 macOS over the tail-calling interpreter. The same page says single benchmarks range from about 15% slower to more than twice as fast. It is still experimental and not on by default, so for most of us nothing changes yet. If you want to try it, test your own workload; the average says little about one program.

For free threading, two practical changes:

  • The official macOS installers now install free-threading support by default.
  • There is now a stable ABI for free-threaded builds (PEP 803, with PEP 793 and 820). For users this means that, over time, C extensions can ship one wheel that works across free-threaded versions. It will take time for packages to adopt it.

7. Removals that can break code

Most removals are of things deprecated since 3.12 or 3.13. The ones I think people actually hit:

Removed Use instead
sre_compile, sre_constants, sre_parse modules re
NamedTuple("Point", x=int, y=int) keyword form class syntax, or a list of fields
TypedDict("TD") with no fields TD = TypedDict("TD", {})
CGIHTTPRequestHandler, python -m http.server --cgi a real web server
load_module() on import loaders exec_module()
importlib.resources.files(package=...) pass the package as first argument
PurePath.is_reserved() os.path.isreserved()
codeobject.co_lnotab co_lines()
module attribute __cached__ __spec__.cached

Some behaviour changes are also worth a look:

  • datetime.strptime() now raises ValueError when the format has %d but no year. Parsing "29 Feb" alone was ambiguous anyway. Add a year to the string and the format.
  • AST node constructors raise TypeError for missing required fields or unknown keyword arguments. This matters if you have code generators or linters.
  • importlib.metadata raises MetadataNotFound instead of returning empty metadata.
  • tarfile extraction filters are stricter.
  • array.typecodes is now a tuple, not a string.
  • ByteString is gone from __all__ in typing and collections.abc and warns on access. Full removal is planned for 3.17.
  • import lines in .pth files are deprecated. PEP 829 adds .start files as the replacement for code that needs to run at startup. This matters if you maintain tools that hook into interpreter start.

The error messages also improved again. [].push(1) now suggests append. Small, but helpful for people who switch between languages all day.

How I will upgrade

I will not move production services in the first week. My plan:

  1. Install 3.15 next to 3.14 with uv (see my uv notes) and run each project’s test suite on it: uv run --python 3.15 pytest.
  2. Before that, run the tests on the current Python with -X warn_default_encoding -W error::DeprecationWarning. Encoding warnings show where the UTF-8 change can matter. Deprecation errors show code that uses things 3.15 removed. Fix these first, because the fixes also work on the old version. Turning all deprecation warnings into errors can also stop on warnings from third-party packages; I add -W ignore filters for those, one by one.
  3. Check compiled dependencies. If one has no 3.15 wheel yet, pip or uv will try to build from source. That usually fails on a server without compilers. For most popular packages wheels arrive within a few weeks.
  4. Update CI to test 3.14 and 3.15 together. A matrix entry costs little.
  5. Move small internal tools first, the CLIs where lazy imports help. Services move after the first bugfix release, 3.15.1.
  6. Replace MISSING = object() and MappingProxyType constants with sentinel and frozendict only in code that already requires 3.15. In libraries that support older versions I keep the old helpers.

The JIT stays off until I have measured it on my sensor data scripts, which are mostly small loops and JSON parsing.