Skip to content

Commit 329e46c

Browse files
committed
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
1 parent 82acded commit 329e46c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

peps/pep-0827.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,16 @@ the database) like::
133133
id: Property[int]
134134

135135
name: Property[str]
136+
age: Property[int | None]
136137
email: Property[str]
137138
posts: Link[Post]
138139

140+
141+
(In the example, ``Property`` indicates a scalar type, ``Link``
142+
indicates a reference to another table, and ``MultiLink`` indicates a
143+
potentially many-to-many reference to another table; all would be
144+
defined by the ORM library.)
145+
139146
So, in Python code, a call like::
140147

141148
db.select(
@@ -1216,6 +1223,7 @@ We present implementations of a selection of them::
12161223

12171224
# Omit<T, Keys>
12181225
# Constructs a type by picking all properties from T and then removing Keys.
1226+
# Note that unlike in TS, our Omit does not depend on Exclude.
12191227
type Omit[T, Keys] = typing.NewProtocol[
12201228
*[
12211229
p
@@ -1224,6 +1232,26 @@ We present implementations of a selection of them::
12241232
]
12251233
]
12261234

1235+
# Exclude<T, U>
1236+
# Constructs a type by excluding from T all union members assignable to U.
1237+
type Exclude[T, U] = Union[
1238+
*[
1239+
x
1240+
for x in typing.Iter[typing.FromUnion[T]]
1241+
if not typing.IsAssignable[x, U]
1242+
]
1243+
]
1244+
1245+
# Extract<T, U>
1246+
# Constructs a type by extracting from T all union members assignable to U.
1247+
type Extract[T, U] = Union[
1248+
*[
1249+
x
1250+
for x in typing.Iter[typing.FromUnion[T]]
1251+
if typing.IsAssignable[x, U]
1252+
]
1253+
]
1254+
12271255
# Partial<T>
12281256
# Constructs a type with all properties of T set to optional (T | None).
12291257
type Partial[T] = typing.NewProtocol[

0 commit comments

Comments
 (0)