r/Python • u/treyhunner Python Morsels • Oct 09 '26
News Python 3.15 Released
https://www.python.org/downloads/release/python-3150/
Release announcement: https://discuss.python.org/t/python-3-15-0-final-is-here/109415
What's New: https://docs.python.org/3.15/whatsnew/3.15.html
Some of the major new features and changes in Python 3.15:
Interpreter improvements
PEP 661:
sentinelbuilt-in typePEP 686: UTF-8 is now the default encoding
PEP 798: Unpacking in comprehensions
PEP 810: Explicit lazy imports for faster startup times
PEP 814:
frozendictbuilt-in typePEP 829: Package startup configuration files
The experimental JIT compiler got a significant upgrade (7-8% faster on x86-64 Linux, 11-12% on AArch64 macOS)
Improved error messages
Standard library
PEP 799: A dedicated
profilingpackage, including Tachyon, a high-frequency sampling profilerNew
math.integermodulere.prefixmatch()as a clearer name for the now soft-deprecatedre.match()pprintgets anexpandoption for JSON-style outputcollections.Countersupports^(symmetric difference)unicodedata.iter_graphemes()for iterating over grapheme clusterspython -m http.servergains-H/--headerand--content-typetimeitgains--target-timeMore color
Typing
PEP 728: TypedDict with typed extra items
PEP 747: Annotating type forms with TypeForm
PEP 800: Disjoint bases in the type system
Release changes
Official Windows 64-bit binaries now use the tail-calling interpreter
Official macOS binaries now install free-threading support by default
And for fun, Barry Warsaw made a text adventure through the new features: uvx --python 3.15 whatsnewt
Thanks to Hugo van Kemenade and the release team! Hugo live-posted the whole release process if you're curious what goes on behind the scenes: https://mastodon.social/@hugovk/117411399858939022
6
u/chalbersma Oct 09 '26
10
u/treyhunner Python Morsels Oct 10 '26
If
Nonewould never be valid data, useNone.If
Nonemight be valid data, you need a different sentinel value.An example: imagine you're implementing something like the dictionary
popmethod.Specifying a default value, like
Noneor0will return that value if the key is missing:>>> d = {} >>> d.pop("missing", None) >>> d.pop("missing", 0) 0But if no value is specified, it raises a
KeyErrorif the key is missing:>>> d.pop("missing") Traceback (most recent call last): File "<python-input-3>", line 1, in <module> d.pop("missing") ~~~~~^^^^^^^^^^^ KeyError: 'missing'If you were implementing this method, you'd need to make that second argument optional but you need to somehow know whether a value was supplied.
The usual solution is to define your own sentinel value that represents "no value was given" and use an identity check (with
is) to check for it so you can distinguish whether a value was given.I have an article on sentinel values in Python that's now in need of an update thanks to Python 3.15.
15
u/aes110 Oct 09 '26
The idea is to use sentinels specifically where None is also an acceptable value, but you want to differentiate between None and Missing for example
Lets say you have
def get_user_items(user, exclude)
Where exclude is an optional param for any value youd want to not include in the final list.
None is a valid value you might want exclude, so you can't check
if exclude is Noneto know the user decided not to supply the optional paramIn that case youd use a sentinel
MISSING = sentinel('MISSING')
Use it as the default value, and you can checkif exclude is MISSINGto know that no value was suppliedLike they said in the PEP, it can also be used to indicate a MISSING return value where None is a valid one
In theory Enum can be used for it, but its not fully compatible for this use case.
3
u/latkde Tuple unpacking gone wrong Oct 10 '26
In theory Enum can be used for it, but its not fully compatible for this use case.
An enum can do everything that sentinel does, it's just much less obvious.
The trick is to define an enum with a single value. That value can then be used as a sentinel-like singleton, and type annotations can use
Literal[MyEnum.INSTANCE].In contrast, sentinels only require a single line to declare, and a sentinel is both a value and a valid type annotation (just like
None). Much more convenient to use.14
u/MarchewkowyBog Oct 09 '26
I thino it's more about giving developers a standard way of doing this. There are a lot of ideas in rejected ideas. So I presume those were inspired by live code. One person would make a const, another an enum, yet another an empty class etc. Now there is a standard way to do that so code is less confusing in the future. Especially with comparison using
is.value is EMPTYreads better thenvalue == EMPTY. I don't see much more to it9
u/chalbersma Oct 09 '26 edited Oct 09 '26
Are "Empty", "Missing" or other Null like primitives what are desired to be represented by
sentinal? Is it just a fancyNone? Like I'm reading some of the discussions around it and saw this explanation:Edit---
Sentinel would be similar to None, but the main property would be that "Sentinel is None" is false :-) source
I guess that makes some sense. So if I had a sensor that had some initialization I might use a
Initializationsentinal to indicate that the system is still starting up and then aNonevalue to indiciate that no reading has come back. Allowing me to differentiate on the metrics between an expected Null and an unexpected Null.4
u/M4mb0 Oct 10 '26
Even more abstractly, you may have some generic type
G[T]with an optional attribute of typeT. But here you can't /shouldn't useNoneto represent a missing value, ifTlegitimately could beNone. Hence, one needs a private sentinel. Usually people did this by doing something like_MISSING = object(), but this has quite a few disadvantages (see rationale section of the pep).3
u/SeniorScienceOfficer Oct 10 '26
I agree, along with handling a None provided as an explicit argument versus not providing an argument at all. I’d prefer using Value/None/Sentinel over Value/False/None to accomplish the same thing, but the use cases are more along the lines of contract and code readability.
2
u/Rodot github.com/tardis-sn Oct 11 '26
Also, simple case
def apply_fiter(data, func=None): if filter is None: return data return filter(func, data)But I want to be able to pass "None" as the argument to filter as well, since it filters out None from the data.
So making a sentinel allows me to put a default value that doesn't prevent me from passing a value intentionally.
4
u/pkkm Oct 10 '26 edited Oct 10 '26
Sometimes it's handy to have a special value that won't accidentally compare equal to anything else. I mostly use them in highly general wrappers where almost any input can be valid, even
None._MISSING = object() def my_function(arg=_MISSING): ...I also like to use them when an argument can take a range of values plus one or two special cases.
Nonemay not read very well when the actual meaning is very different from "nothing", and using an enum feels weird when it's not actually a closed set of choices. For example, I have a brightness setting function that takes a float percentage or aMINIMUMsentinel, meaning the lowest non-zero level.I used to put
STOPsentinels into async queues too, but that pattern is obsolete now that queues have ashutdownmethod.1
2
u/ogMasterPloKoon Oct 09 '26
So now i not have to use PYTHONUTF8=1 anymore ?
3
u/treyhunner Python Morsels Oct 10 '26
That's right. Everyone who wants to maintain the existing behavior will need to start setting
PYTHONUTF8=0instead.1
u/TheRNGuy Oct 10 '26
Not just omit it?
2
u/treyhunner Python Morsels Oct 10 '26
Instead of "maintain the existing behavior" I should have said "maintain the behavior before Python 3.15".
Anyone who wants to new behavior on 3.15+ should just omit it. If the old behavior is desired, it'll likely always be there behind an environment variable.
129
u/frankieinaprius Oct 09 '26
Unpacking in comprehensions will be so nice. 🙏🏽
17
u/NeilGirdhar Oct 10 '26
I've wanted this since I helped implement PEP 448 13 years ago! At the time, there was a lot of opposition to it as "unintuitive", so we set it aside. I figured that eventually people would come around to finding it intuitive.
10
u/frankieinaprius Oct 10 '26
Because [item for sublist in nested_list for item in sublist] is so much more intuitive 😅
1
u/warden127 Oct 10 '26
That variant would be more intuitive if the for's were the other way around.
2
u/frankieinaprius Oct 10 '26
It resembles a typical nested for loop. Outer first, then inner. So I get why it is the way it is. But for some reason it feels like it should be more natural to go inside out when it’s in a comprehension. So it’s just confusing no matter what. At least with unpacking now I can just look at it and recognize the pattern that it’s being flattened.
22
u/Competitive_Travel16 Oct 09 '26 edited Oct 09 '26
Yes, folding a list of dicts into one is now something that looks nice and somewhat more clear. It's also something I do often. I don't think AI does it much though; they tend to plan ahead more than I do.
5
1
u/forgotten_vale2 Oct 10 '26
I always tried to do this and complained WHYY doesn't it work? Very convenient to have it now.
-38
u/AstroPhysician Oct 09 '26
Y'all are still writing code manually?
3
u/_Denizen_ Oct 11 '26
Well, yeah. AI writes bloated code and often misses the point. After all, a machine can't contain as much context as a human mind.
Many people work at companies that don't write blank cheques for AI tokens - so using coding assistants where it adds most value to human logic is the key to maximising output, job satisfaction, and quality control.
On code that I've designed, I can find bugs faster than AI, and can implement refactors faster by strategically writing critical logic myself then using AI to finish implementation steps that would be tedious for a human to do.
If you want to spend all your working hours outsourcing your work be my guest. Personally, I find it vibe coding about as fulfilling as doing a test by copying all the answers - sure, on paper you pass, but you don't learn how to solve problems yourself.
-1
u/AstroPhysician Oct 12 '26 edited Oct 12 '26
Bloated code is a skill issue or old models, or not defining the prompt with. The architecture should be decided and planned before any code is written
Also newer models are much better at that. Ponytail skill too
2
u/_Denizen_ Oct 12 '26
Newer models are marginally better but still subject to the same faults.
Developing a prototype and productionising it implies architectural changes. No coding product architecture completely survives unchanged when it comes to implementation, because requirements change as the product evolves. That's why a good architecture design is not one which predefines all possible features and user flows, but instead is designed such that it can be easily updated with minimal breaking changes.
And that is what even the latest AI models are bad at. They oversolidify parts that need to be malleable, because it's nearly impossible to comletely write all constraints and design intentions for the entire product to implement small changes whilst keeping the prompt low-code.
Sure, you can write thousands of lines of prompt harnesses, agents, skills, etc. But then you end up maintaining those to make sure the AI can maintain your code instead of directly maintaining the code. You're shifting the problem, not solving it.
All the problems with overeliance of AI are multiplied when working in a team, as the pace of change is too fast and the team lacks the exposure to eech others vibed changes to be able to collaborate as effectively. I think there's a good reason why most vibe-first coders I've met work solo.
If you can't recognise where human intervention is better that AI, then it means you either lack the experience to be an effective manager of AI (because you understate its limitations), or have been using it so heavily that it's become a crutch and your own skills have eroded. The problem with a crutch, is that it's addictive and you fall over if it gets knocked away.
2
29
u/Drewdledoo Oct 09 '26
A lot of nice QoL, looking forward to trying it out!
Particularly the explicit lazy imports, a lot of the projects I work on should see nice benefits from that.
29
u/NeitherEntry6125 Oct 09 '26
Hugo deserves our thanks, praise, and his beverage of choice for the next year
19
u/spinwizard69 Oct 10 '26
Improved error messages! Often improvements here get ignored in favor of fancy new programming features, however anything that improves error messages and debugging should be celebrated. I just want to make sure the hard work is not ignored, thus a celebration.
7
u/IAmASquidInSpace Oct 09 '26
Some really neat stuff in there! Honestly, I was surprised to find that extra_items wasn't supported yet, I've been looking forward to that for a while now. And TypeForm will come in handy every now and then, too!
4
u/latkde Tuple unpacking gone wrong Oct 09 '26
The
typing_extensionsmodule (GitHub, PyPi, Docs) can be used as a backport for these feature for older Python versions. Some of the Python 3.15 typing features have been available for months or years.The real blocker is type checker support, e.g. Mypy doesn't support
extra_items. But it did recently shipsentinelsupport, and partialTypeFormsupport has been around for a couple of releases.1
1
u/freshtechs Oct 12 '26
Kind of getting to know python 3.13+ and the “macOS binaries now install free-threading support by default” does this has something to do with GIL are python versions from 3.13+ all “multi/free thread”?
1
u/treyhunner Python Morsels Oct 12 '26
From 3.13 onward, it's possible to build a free-threaded version of Python (the no GIL version), but that's not the default build.
On my machine, `python3.13` is the default build and `python3.13t` is the free-threaded build.
The free-threaded build was experimental in 3.13, is officially supported [in 3.14](https://docs.python.org/3/whatsnew/3.14.html#free-threaded-python-is-officially-supported), and [3.15 brings even more support](https://docs.python.org/3/whatsnew/3.15.html#whatsnew315-abi3t).
2
u/OddsRelay Oct 10 '26
frozendict as a builtin is the one that'll get used most. The usual workaround was wrapping a dict in types.MappingProxyType, which stops writes but isn't hashable, so you still couldn't use it as a dict key or put it in a set. Long overdue.
2
u/aes110 Oct 09 '26
Seeing as this is the first big release since so much code is now done by AI, i wonder how well agents will adopt the new features given they dont have it in their training data
3
u/Ph0X Oct 10 '26
The whole point of LLM is that they are trained on such large data sets that they don't gain a level of knowledge that allows them to handle scenarios they haven't encountered before. For coding, it's like if you were trying to get it to use a new library, it can just put the relevant new code in its context window and understand how it works, even if it hasn't seen it before.
1
u/TheRNGuy Oct 10 '26
How big is difference?
Would you need to fix old code?
Not everyone probably use new features, too.
1
u/_Denizen_ Oct 11 '26
In the short term, you just provide the links to the new features as context to the queries. This will cost more tokens. In the medium term new models will release which have been trained on appropriate code and you won't have to provide the additional context.
1
1
u/akadeepesh 11d ago
The lazy imports and improved startup times caught my attention. Always nice to see Python evolving in ways that make everyday development more efficient. Excited to explore 3.15!
1
1
u/zomerbk Oct 13 '26
One practical note on the UTF-8 default: it won’t change the encoding of existing files. If your code reads older files saved in a different encoding, keep passing `encoding=` explicitly.
1
u/Kotopelmeshek Oct 14 '26
Great! But I'd wait until most of the libraries I need are running on Python 3.15!
1
u/wasay-inc Oct 10 '26
explicit lazy imports via pep 810 are easily the best addition in this release. being able to keep imports cleanly at the top of the file while deferring execution until runtime is going to clean up startup times for heavy automation scripts immensely.
-43
u/vnmger Oct 09 '26
Can somebody enlighten me, how do new language features which are QoL really Help us, in the age of agentic development.
Don't get me wrong, I love new iterations on my favorite languages, just really asking myself in a self-critic moment.
14
u/latkde Tuple unpacking gone wrong Oct 09 '26 edited Oct 09 '26
You're underestimating some of these changes, regardless of whether you're writing code by hand like me, or outsource code-level work to an agent. Some of these changes enable new things that weren't previously possible, or only possible with tedious workarounds. Lazy imports are a great example – which will eventually help speed up all the ML and AI libraries you're probably using.
It will take a while until LLMs use Python 3.15 features (Claude Opus still seems to write code like it's 3 years ago), but eventually the QoL improvements will trickle down. Why use many tokens when few tokens do trick? Even for vibecoders, it's great when languages become more expressive, tools become a bit faster, and type systems become more precise.
6
u/boringfantasy git push -f Oct 09 '26
Agents have basically no idea about them cause majority of their training data will be old Python, so these features are barely ever used.
-9
u/vnmger Oct 09 '26
But that is my point. Are we enthusiasts the only ones using that, or will ai later catch up, should we somehow try to stop it from catching up? I would love that
0
2
u/jayplusplus Oct 09 '26
Was thinking the same. Hopefully these QoL improvements ultimately help us better understand the code these agents write. At least while we're still the human in the loop.
0
62
u/treyhunner Python Morsels Oct 09 '26
A few of my personal favorites from this release:
UTF-8 as the default encoding (so confusing
utf-8&cp-1252ASCII superset encodings can stop battling and creating emojibake and decode errors in cross-platform code)The new sampling profiler because it's so fast and attaching to an already running process makes it very flexible
Unpacking in comprehensions:
[*sub for sub in nested]The new
sentineltypere.prefixmatch(the namere.matchhas always bugged me)I made a short video demoing these and a few others (lazy imports, frozendict, pprint's expand), with an article version on the same page: https://pym.dev/python315/