Python Knowledge Patch
Use this skill when updating Python applications, libraries, tooling, native extensions, embedded runtimes, or CPython builds whose behavior depends on recent language and standard-library changes.
Confirm the exact interpreter and maintenance release before applying an item. Open the topic reference that matches the code under review; the quick reference below emphasizes compatibility failures, changed defaults, and the most broadly useful additions.
Reference index
| Reference | Topics |
|---|---|
| Language and runtime | Syntax, built-ins, object behavior, text, numbers, garbage collection, removals |
| Typing and introspection | Annotations, ASTs, frames, signatures, type expressions, symbols |
| Concurrency and asyncio | Threads, queues, multiprocessing, task groups, subinterpreters, free-threading |
| Data, I/O, and serialization | Configuration, SQLite, archives, compression, structured data, streams |
| Networking and security | TLS, HTTP, URLs, email, protocol parsing and validation |
| Filesystems, OS, and platforms | Paths, descriptors, memory maps, resources, locale, operating systems |
| Tooling, debugging, and testing | Imports, REPLs, profiling, pdb, logging, warnings, tests, packaging |
| C API and extensions | Extension compatibility, references, types, modules, embedding, Stable ABI |
| Build and distribution | Configure controls, JIT, toolchains, artifacts, cross-builds, installers |
Compatibility first
Runtime and language behavior
functools.partialstored directly on a class emitsFutureWarning; wrap it instaticmethod()when non-binding behavior is intended.- Optimized builds reject the same invalid syntax as ordinary builds. Do not
rely on
-Oto remove an invalid write to__debug__,await, or async comprehension. - Generator-expression iteration is deferred until the generator runs. Code that expects a source error at construction must force iteration explicitly.
Path.exists()andPath.is_*()suppress everyOSError; usestat()when permissions, encoding, or other failures must remain observable.\Bnow matches empty input as the inverse of\b. Use(?!\A\Z)\Bwhen empty strings must remain excluded.- Copying or pickling
itertoolsiterators is no longer supported. - Garbage-collector behavior depends on the maintenance release: do not infer the collector design from the feature release alone.
Removed and deprecated call patterns
- Pass a mapping for SQLite named placeholders. Sequences now raise
ProgrammingError. - Use
sqlite_versionandsqlite_version_info; the module'sversionandversion_infoattributes are removed. - Prefer
subprocesstoos.popen()andos.spawn*(),Path.as_uri()toPurePath.as_uri(), and normal file opening tocodecs.open(). - Replace
pkgutil.get_loader()/find_loader(),pty.master_open()/slave_open(), and the legacyURLopenerclasses before upgrading. - Name
sqlite3.connect()options after the database; pass function and callback registration arguments positionally. - Use
os.path.isreserved()instead ofPurePath.is_reserved(), modern loader APIs instead ofload_module(), and class or mapping forms forNamedTupleandTypedDictconstruction. - Stop depending on
CodeType.co_lnotab, the privatesre_*modules, CGI support inhttp.server, or removed WAVE marker methods. - Avoid legacy false query-string inputs; normalize them before parsing.
Changed defaults and failure modes
gzip.compress()produces reproducible output by default withmtime=0and OS byte 255. Passmtime=Nonewhen wall-clock timestamps are desired.- Pickle protocol 5 is the default. Select an older protocol explicitly when older consumers must read the data.
- Unclosed
GzipFileandNamedTemporaryFileinstances emitResourceWarning; use explicit ownership and closure. - Nonblocking text reads and
hashlib.file_digest()may raiseBlockingIOErrorinstead of returning empty/spurious data. - Email header assignment validates field names, and generators reject unsafe or non-EAI output instead of flattening it inaccurately.
ConfigParserrefuses keys that would not round-trip through its output.QueueListener.start()raises if already started; use it as a context manager for paired startup and shutdown.- Query-string, URL, cookie, WSGI, HTTP, POP3, IMAP, and archive handling has stricter input validation. Keep malformed-input tests in compatibility runs.
Asyncio, processes, and free-threading
- Guard custom task factories by exact maintenance release: the 3.13.3 keyword-forwarding behavior was corrected in 3.13.4.
Thread.join()waits for the underlying operating-system thread to exit.- Unix asyncio servers remove their socket path when closed.
SharedMemory(track=False)opts out of resource-tracker cleanup; tracker leaks now produce a nonzero tracker exit.- Free-threaded builds change warning-context and thread-context inheritance. Test both GIL-enabled and free-threaded configurations when relevant.
- Hold one critical section around an entire
PyDict_Next()traversal; per-step locking is not sufficient. - Prefer supported iterator serialization/synchronization helpers when sharing generators across concurrent callers.
- Use direct task-group cancellation when the target runtime provides it, instead of injecting a task whose only purpose is to raise.
High-value additions
Safer structured data and I/O
ConfigParser(allow_unnamed_section=True)accepts top-level keys; newer mapping access can also createUNNAMED_SECTION.importlib.resourceshelpers accept nested path components. Pass textencodinganderrorsby keyword.- Tar streaming can avoid caching every member. Tar extraction filters also harden symlink fallback and directory fixups.
ZipFile.writestr()honorsSOURCE_DATE_EPOCH, andZipInfo._for_archive()resolves the metadata defaults that will be written.io.Readerandio.Writerare structural protocols for simple stream APIs.- TOML 1.1 users should open the data reference for exact parsing changes.
Runtime features to gate by interpreter
Fractionaccepts any object implementingas_integer_ratio().- Three-argument
pow()can dispatch to__rpow__(). superobjects can be copied and pickled.datetimeandtimeISO parsing accepts24:00.- Newer runtimes add explicit lazy imports, immutable built-in mappings,
identity-stable sentinels, unpacking comprehensions, generic
slice, and copy-freebytearray.take_bytes(); never emit their syntax or built-ins for an older interpreter. - Newer typing surfaces include
TypeForm, closed or extensibleTypedDict, richer type aliases, and bounded or variantTypeVarTupledeclarations.
Debugging and observability
- Pdb supports packaged and module targets, live process attachment, and async breakpoints; use the exact same runtime version for attachment.
sys.monitoringexposes richer exception events, including per-code enablement in newer runtimes.- Native thread names and C stacks can appear in
faulthandleroutput. - Asyncio can expose live task trees and in-process call graphs.
- The
profilingpackage adds deterministic tracing plus an attachable sampling profiler with async-aware, process, flame-graph, and replay workflows. -X importtime=2includes cached imports, and-X perf_jitenables enhanced Linux perf integration.
C extensions and embedding
- Declare free-threaded support with
Py_mod_gilfor multi-phase modules orPyUnstable_Module_SetGIL()for single-phase modules. Undeclared modules may re-enable the GIL. PyModule_Add()always steals the passed reference. Prefer new*Ref()lookup helpers when ownership must be explicit.- Use error-preserving attribute and mapping lookup APIs when lookup failures
must propagate instead of reaching
sys.unraisablehook(). - Include every system header directly;
Python.hno longer supplies several platform headers transitively. - Replace removed trashcan macros, ambiguous iteration, private integer and Unicode builders, and direct representation access with their public APIs.
- Limited-API reference-count and type macros are opaque. Never use
Py_REFCNT(obj) == 1as a uniqueness test for borrowed stack references. - Release extension-held interned strings before finalization when an embedder can reinitialize the runtime.
- Newer free-threaded Stable ABI and slot/export APIs require separate artifact planning; keep ordinary and free-threaded wheels distinct when unsupported APIs are used.
Upgrade workflow
- Confirm the exact executable, ABI, maintenance release, GIL mode, operating system, and extension build configuration.
- Search first for removals, keyword/positional migration warnings, iterator serialization, old loader APIs, legacy SQLite usage, and private C APIs.
- Audit changed defaults in serialization, text encoding, query parsing, archives, process startup, warning handling, and argument parsing.
- Open the matching topic reference and trace every affected call site; preview-only syntax and APIs require an explicit runtime gate.
- Run tests with warnings and
ResourceWarningvisible. Exercise malformed input, nonblocking I/O, shutdown, finalization, interpreter reinitialization, and free-threaded imports where applicable. - For binary extensions, test Limited/Stable API claims, ownership on every error path, GIL declarations, and wheel tags on every supported platform.