From e9134f5ed73d803e0b32132f15984d450e2c6106 Mon Sep 17 00:00:00 2001 From: "Michael J. Sullivan" Date: Wed, 4 Mar 2026 14:28:18 -0800 Subject: [PATCH] PEP 827: Minor tweaks to the examples * Add Exclude and Extract to the TS-style utility types * Add an `age` field to User in the prisma style example so that we aren't selecting *all* non-id fields * Add some discussion about Property/Link/MultiLink, since there was some confusion where they came from --- peps/pep-0827.rst | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/peps/pep-0827.rst b/peps/pep-0827.rst index ea1f36a3744..3a8fb89f854 100644 --- a/peps/pep-0827.rst +++ b/peps/pep-0827.rst @@ -133,9 +133,16 @@ the database) like:: id: Property[int] name: Property[str] + age: Property[int | None] email: Property[str] posts: Link[Post] + +(In the example, ``Property`` indicates a scalar type, ``Link`` +indicates a reference to another table, and ``MultiLink`` indicates a +potentially many-to-many reference to another table; all would be +defined by the ORM library.) + So, in Python code, a call like:: db.select( @@ -1216,6 +1223,7 @@ We present implementations of a selection of them:: # Omit # Constructs a type by picking all properties from T and then removing Keys. + # Note that unlike in TS, our Omit does not depend on Exclude. type Omit[T, Keys] = typing.NewProtocol[ *[ p @@ -1224,6 +1232,27 @@ We present implementations of a selection of them:: ] ] + # Exclude + # Constructs a type by excluding from T all union members assignable to U. + type Exclude[T, U] = Union[ + *[ + x + for x in typing.Iter[typing.FromUnion[T]] + if not typing.IsAssignable[x, U] + ] + ] + + # Extract + # Constructs a type by extracting from T all union members assignable to U. + type Extract[T, U] = Union[ + *[ + x + for x in typing.Iter[typing.FromUnion[T]] + # Just the inverse of Exclude, really + if typing.IsAssignable[x, U] + ] + ] + # Partial # Constructs a type with all properties of T set to optional (T | None). type Partial[T] = typing.NewProtocol[