DEV Community

Cover image for 5 Python 3.15 Features Worth Adopting Before the Year Ends
Mango Developer
Mango Developer

Posted on Originally published at mangodeveloper.com

5 Python 3.15 Features Worth Adopting Before the Year Ends

Python 3.15 is the release where several long-standing workarounds finally get real syntax. If you have a function that imports pandas inside its body so the command-line tool starts faster, or a module-level _MISSING = object(), this version has something for you.

Python 3.15.0 landed on October 9, 2026, and the docs list its biggest changes as lazy imports, the frozendict and sentinel builtins, UTF-8 as the default encoding, unpacking in comprehensions and a stable ABI for free-threaded builds. Two maintenance releases have followed. Python 3.15.2, dated October 25, is described on python.org as an expedited security release, so that is the version to install.

We haven't tested these features ourselves. This is based on the official What's New in Python 3.15 documentation, Astral's release post and Python Morsels, with each point attributed where it matters.

If you do only one thing, run uv python upgrade 3.15, which is the command Astral gives, or install 3.15.2 from python.org. Then work through the five items below in the order that fits your code.

1. Lazy imports, for startup time

Python 3.15 adds a soft keyword, lazy. Write lazy import json or lazy from pathlib import Path, and Python creates a lightweight proxy instead of loading the module. The real import runs the first time you use the name.

The docs explain why this exists. Large applications can take seconds to start because the import system has to find, read, compile and execute every module, even ones a given run never touches. Developers moved imports into functions or used importlib to dodge that, which scatters import statements around the code. A lazy import lets you keep all imports at the top of the file.

Three details are worth knowing before you convert anything:

  • If a lazily imported module fails to load, the exception appears at first use, not at import time. The traceback includes both the access point and the original import statement.
  • To try it without editing source, set the -X lazy_imports option or the PYTHON_LAZY_IMPORTS environment variable. Both accept all and normal, and normal is the default and respects the lazy keyword.
  • Libraries that must still support older Pythons can set __lazy_modules__ = ["typing"] before the import, which Astral says has the same effect as lazy import typing without new syntax.

The pitfall is side effects. Anaconda's guide to 3.15 warns that anything that registers a backend, plugin or codec when it loads should keep a plain import, and a module imported only for its side effect, with the name never referenced, will never load at all. I would add a rule for code review: any import whose only purpose is a side effect stays eager.

Astral's tools have caught up. Ruff 0.17 stabilizes lint rules TID254, which enforces lists of imports that must or must not be lazy, and TID255, which flags a lazy import that is resolved immediately. On the uv side, a preview flag makes build-backend imports lazy, and Astral reports wheel builds up to 18 percent faster in its own benchmarks. Treat that as a vendor number.

2. frozendict, for config and cache keys

frozendict is a new built-in immutable mapping. The docs say it is hashable as long as its keys and values are, keeps insertion order, and compares equal regardless of order. Assigning to a key raises a TypeError.

The catch that will bite first: a frozendict is not a subclass of dict. It inherits directly from object. The docs suggest changing isinstance(arg, dict) to isinstance(arg, (dict, frozendict)), or checking against collections.abc.Mapping, which also accepts other mapping types such as MappingProxyType.

The standard library has been updated to accept it in copy, decimal, json, marshal, plistlib (serialization only), pickle, pprint and xml.etree.ElementTree. eval() and exec() accept one for globals.

Good first uses are configuration objects you want to be unchangeable after startup, and arguments to cached functions, where a dict previously had to be turned into something hashable. Be careful with anything that does type(x) is dict; it will now say no.

3. sentinel, for the argument that can be None

Anyone who has written MISSING = object() knows its repr is useless in a traceback. The new sentinel built-in takes a name, so MISSING = sentinel("MISSING") prints properly. Python Morsels shows the old behaviour as an anonymous object address.

The docs say sentinel objects keep their identity when copied, work in type expressions with the | operator and can be pickled when importable by module and name. The use case is the usual one: a function like find() that could legitimately return None needs a distinct placeholder, and you compare it with is.

It is a small feature and a nice one. I would convert module-level object() sentinels in libraries first, because their repr shows up in other people's logs.

4. Unpacking inside comprehensions

List, set and dictionary comprehensions and generator expressions now accept * and ** unpacking. Python Morsels shows [*x for x in lists] flattening a list of lists, and {**d for d in [{"a": 1}, {"b": 2}]} producing {'a': 1, 'b': 2}. The docs call it a direct alternative to nested comprehensions and itertools.chain.

I like it for readability. One warning from the dict form: later keys win, as they do in ordinary dictionary merging, so the order of your inputs still matters.

5. Tachyon, a sampling profiler you can point at production

Python 3.15 adds a profiling package. It holds profiling.tracing, the deterministic profiler moved from cProfile, and profiling.sampling, a new statistical profiler named Tachyon. The cProfile module stays as an alias, and the older profile module is deprecated with removal planned for Python 3.17.

The attraction is that Tachyon can attach to a running process by PID without a code change or restart. The docs say it samples at up to 1,000,000 Hz with virtually zero overhead, and call it the fastest sampling profiler available for Python at the time of its contribution. That is the project's own claim.

It offers modes for attaching to a process, running a script, running a module and taking a one-shot dump of every thread, which is useful for hung processes. You can measure wall-clock time, CPU time, GIL-holding time or exception handling time. Output formats include pstats, collapsed stacks, an interactive HTML flame graph, Gecko format for Firefox Profiler and a line-level heatmap.

Interactive flame graph produced by the Tachyon sampling profiler in Python 3.15

Expand

Interactive flame graph produced by the Tachyon sampling profiler in Python 3.15

Line-level heatmap view from the Tachyon profiler showing sample counts per source line

Expand

Line-level heatmap view from the Tachyon profiler showing sample counts per source line

A related change helps the same tools: CPython is now built with frame pointers by default, which the docs say makes native stack unwinding faster and more reliable for system profilers, debuggers and eBPF tools. If you build native extensions, the docs add a warning: a single component built without frame pointers can break unwinding for the whole process.

Before and after, in one table

This table puts the old workarounds next to what 3.15 offers, using only the documented behaviour above.

Job The old way In Python 3.15 Watch out for
Faster startup Imports inside functions, or TYPE_CHECKING blocks lazy import Side-effect imports never run
Missing-argument marker object() with an anonymous repr sentinel("NAME") Compare with is
Immutable config Convert a dict to something hashable frozendict Not a dict subclass
Flatten inputs Nested comprehension or itertools.chain [*x for x in lists] Later dict keys win
Find a slow function cProfile, which traces every call profiling.sampling (Tachyon) Sampling can miss very short calls

The last warning in the table is a general property of sampling profilers rather than something the docs say about Tachyon, so check it against your own workload.

Upgrade notes that will save you an hour

Python 3.15 uses UTF-8 as the default encoding. Code that relied on the locale's encoding when opening files without an explicit encoding= argument can change behaviour, so grep for bare open() calls on Windows machines. We covered the Windows side of this release in our Python 3.15 on Windows on Arm piece.

The release is also the one where the profile module starts its path to removal, so update any scripts that still import it.

Troubleshooting: if lazy is a syntax error, you are running an older Python, which is why the __lazy_modules__ fallback exists. If isinstance checks start failing on config objects after moving to frozendict, you have found the subclass catch above.

Frequently Asked Questions

Is it safe to turn on lazy imports for the whole app?

You can try it with PYTHON_LAZY_IMPORTS=all, but modules that register things as a side effect may not load when you expect. Test with the environment variable in staging first.

Does frozendict replace MappingProxyType?

Not exactly. A frozendict is its own type that is hashable when its contents are, while MappingProxyType is a read-only view. The docs mention both as mapping types you can accept with collections.abc.Mapping.


Originally published on Mango Developer.

Top comments (0)