Skip to content

Commit 9c9cd91

Browse files
committed
Rename Sub -> IsSub
Yury says Guido would complain about `Sub`.
1 parent a9ed9b0 commit 9c9cd91

7 files changed

Lines changed: 41 additions & 37 deletions

File tree

tests/test_fastapilike_1.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
NewProtocol,
1010
Iter,
1111
Attrs,
12-
Sub,
12+
IsSub,
1313
GetAnnotations,
1414
DropAnnotations,
1515
FromUnion,
@@ -54,7 +54,7 @@ class _Default:
5454
GetName[p],
5555
DropAnnotations[GetType[p]],
5656
Literal["keyword", "default"]
57-
if Sub[
57+
if IsSub[
5858
Literal[PropQuals.HAS_DEFAULT],
5959
GetAnnotations[GetType[p]],
6060
]
@@ -104,13 +104,15 @@ class _Default:
104104
# Strip `| None` from a type by iterating over its union components
105105
# and filtering
106106
type NotOptional[T] = Union[
107-
*[x for x in Iter[FromUnion[T]] if not Sub[x, None]]
107+
*[x for x in Iter[FromUnion[T]] if not IsSub[x, None]]
108108
]
109109

110110
# Adjust an attribute type for use in Public below by dropping | None for
111111
# primary keys and stripping all annotations.
112112
type FixPublicType[T] = DropAnnotations[
113-
NotOptional[T] if Sub[Literal[PropQuals.PRIMARY], GetAnnotations[T]] else T
113+
NotOptional[T]
114+
if IsSub[Literal[PropQuals.PRIMARY], GetAnnotations[T]]
115+
else T
114116
]
115117

116118
# Strip out everything that is Hidden and also make the primary key required
@@ -120,7 +122,7 @@ class _Default:
120122
*[
121123
Member[GetName[p], FixPublicType[GetType[p]], GetQuals[p]]
122124
for p in Iter[Attrs[T]]
123-
if not Sub[Literal[PropQuals.HIDDEN], GetAnnotations[GetType[p]]]
125+
if not IsSub[Literal[PropQuals.HIDDEN], GetAnnotations[GetType[p]]]
124126
]
125127
]
126128

@@ -129,7 +131,7 @@ class _Default:
129131
*[
130132
Member[GetName[p], GetType[p], GetQuals[p]]
131133
for p in Iter[Attrs[T]]
132-
if not Sub[Literal[PropQuals.PRIMARY], GetAnnotations[GetType[p]]]
134+
if not IsSub[Literal[PropQuals.PRIMARY], GetAnnotations[GetType[p]]]
133135
]
134136
]
135137

@@ -144,7 +146,7 @@ class _Default:
144146
GetQuals[p],
145147
]
146148
for p in Iter[Attrs[T]]
147-
if not Sub[Literal[PropQuals.PRIMARY], GetAnnotations[GetType[p]]]
149+
if not IsSub[Literal[PropQuals.PRIMARY], GetAnnotations[GetType[p]]]
148150
]
149151
]
150152

tests/test_fastapilike_2.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
NewProtocol,
1616
Iter,
1717
Attrs,
18-
Sub,
18+
IsSub,
1919
FromUnion,
2020
GetArg,
2121
GetAttr,
@@ -54,14 +54,14 @@ class Field[T: FieldArgs](InitField[T]):
5454
# Strip `| None` from a type by iterating over its union components
5555
# and filtering
5656
type NotOptional[T] = Union[
57-
*[x for x in Iter[FromUnion[T]] if not Sub[x, None]]
57+
*[x for x in Iter[FromUnion[T]] if not IsSub[x, None]]
5858
]
5959

6060
# Adjust an attribute type for use in Public below by dropping | None for
6161
# primary keys and stripping all annotations.
6262
type FixPublicType[T, Init] = (
6363
NotOptional[T]
64-
if Sub[Literal[True], GetFieldItem[Init, Literal["primary_key"]]]
64+
if IsSub[Literal[True], GetFieldItem[Init, Literal["primary_key"]]]
6565
else T
6666
)
6767

@@ -72,23 +72,23 @@ class Field[T: FieldArgs](InitField[T]):
7272
*[
7373
Member[GetName[p], FixPublicType[GetType[p], GetInit[p]], GetQuals[p]]
7474
for p in Iter[Attrs[T]]
75-
if not Sub[Literal[True], GetFieldItem[GetInit[p], Literal["hidden"]]]
75+
if not IsSub[Literal[True], GetFieldItem[GetInit[p], Literal["hidden"]]]
7676
]
7777
]
7878

7979
# Extract the default type from an Init field.
8080
# If it is a Field, then we try pulling out the "default" field,
8181
# otherwise we return the type itself.
8282
type GetDefault[Init] = (
83-
GetFieldItem[Init, Literal["default"]] if Sub[Init, Field] else Init
83+
GetFieldItem[Init, Literal["default"]] if IsSub[Init, Field] else Init
8484
)
8585

8686
# Create takes everything but the primary key and preserves defaults
8787
type Create[T] = NewProtocol[
8888
*[
8989
Member[GetName[p], GetType[p], GetQuals[p], GetDefault[GetInit[p]]]
9090
for p in Iter[Attrs[T]]
91-
if not Sub[
91+
if not IsSub[
9292
Literal[True], GetFieldItem[GetInit[p], Literal["primary_key"]]
9393
]
9494
]
@@ -105,7 +105,7 @@ class Field[T: FieldArgs](InitField[T]):
105105
Literal[None],
106106
]
107107
for p in Iter[Attrs[T]]
108-
if not Sub[
108+
if not IsSub[
109109
Literal[True], GetFieldItem[GetInit[p], Literal["primary_key"]]
110110
]
111111
]
@@ -129,7 +129,7 @@ class Field[T: FieldArgs](InitField[T]):
129129
# All arguments are keyword-only
130130
# It takes a default if a default is specified in the class
131131
Literal["keyword"]
132-
if Sub[
132+
if IsSub[
133133
GetDefault[GetInit[p]],
134134
Never,
135135
]

tests/test_qblike.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
NewProtocol,
99
Iter,
1010
Attrs,
11-
Sub,
11+
IsSub,
1212
GetType,
1313
Member,
1414
GetName,
@@ -28,11 +28,13 @@ class Link[T]:
2828

2929

3030
type PropsOnly[T] = NewProtocol[
31-
*[p for p in Iter[Attrs[T]] if Sub[GetType[p], Property]]
31+
*[p for p in Iter[Attrs[T]] if IsSub[GetType[p], Property]]
3232
]
3333

3434
# Conditional type alias!
35-
type FilterLinks[T] = Link[PropsOnly[GetArg[T, Link, 0]]] if Sub[T, Link] else T
35+
type FilterLinks[T] = (
36+
Link[PropsOnly[GetArg[T, Link, 0]]] if IsSub[T, Link] else T
37+
)
3638

3739

3840
def select[K: BaseTypedDict](

tests/test_qblike_2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
NewProtocol,
99
Iter,
1010
Attrs,
11-
Sub,
11+
IsSub,
1212
GetType,
1313
Member,
1414
GetName,
@@ -95,7 +95,7 @@ def select[ModelT, K: BaseTypedDict](
9595
"""
9696

9797
type ConvertField[T] = (
98-
AdjustLink[PropsOnly[PointerArg[T]], T] if Sub[T, Link] else PointerArg[T]
98+
AdjustLink[PropsOnly[PointerArg[T]], T] if IsSub[T, Link] else PointerArg[T]
9999
)
100100

101101
"""``PointerArg`` gets the type argument to ``Pointer`` or a subclass.
@@ -115,7 +115,7 @@ def select[ModelT, K: BaseTypedDict](
115115
we've discussed already.
116116
117117
"""
118-
type AdjustLink[Tgt, LinkTy] = list[Tgt] if Sub[LinkTy, MultiLink] else Tgt
118+
type AdjustLink[Tgt, LinkTy] = list[Tgt] if IsSub[LinkTy, MultiLink] else Tgt
119119

120120
"""And the final helper, ``PropsOnly[T]``, generates a new type that
121121
contains all the ``Property`` attributes of ``T``.
@@ -126,7 +126,7 @@ def select[ModelT, K: BaseTypedDict](
126126
*[
127127
Member[GetName[p], PointerArg[GetType[p]]]
128128
for p in Iter[Attrs[T]]
129-
if Sub[GetType[p], Property]
129+
if IsSub[GetType[p], Property]
130130
]
131131
]
132132
]

tests/test_type_dir.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
GetQuals,
1212
GetType,
1313
InitField,
14-
Sub,
14+
IsSub,
1515
Iter,
1616
Member,
1717
Members,
@@ -80,7 +80,7 @@ class Final(Mine, Ordinary, Wrapper[float], AnotherBase[float], Last[int]):
8080
pass
8181

8282

83-
type BaseArg[T] = GetArg[T, Base, 0] if Sub[T, Base] else Never
83+
type BaseArg[T] = GetArg[T, Base, 0] if IsSub[T, Base] else Never
8484

8585

8686
type AllOptional[T] = NewProtocol[
@@ -101,7 +101,7 @@ class Final(Mine, Ordinary, Wrapper[float], AnotherBase[float], Last[int]):
101101
]
102102

103103
type Prims[T] = NewProtocol[
104-
*[p for p in Iter[Attrs[T]] if Sub[GetType[p], int | str]]
104+
*[p for p in Iter[Attrs[T]] if IsSub[GetType[p], int | str]]
105105
]
106106

107107
type NoLiterals1[T] = NewProtocol[
@@ -114,7 +114,7 @@ class Final(Mine, Ordinary, Wrapper[float], AnotherBase[float], Last[int]):
114114
for t in Iter[FromUnion[GetType[p]]]
115115
# XXX: 'typing.Literal' is not *really* a type...
116116
# Maybe we can't do this, which maybe is fine.
117-
if not Sub[t, Literal]
117+
if not IsSub[t, Literal]
118118
]
119119
],
120120
GetQuals[p],
@@ -130,10 +130,10 @@ class Final(Mine, Ordinary, Wrapper[float], AnotherBase[float], Last[int]):
130130
type IsLiteral[T] = (
131131
Literal[True]
132132
if (
133-
(Sub[T, str] and not Sub[str, T])
134-
or (Sub[T, bytes] and not Sub[bytes, T])
135-
or (Sub[T, bool] and not Sub[bool, T])
136-
or (Sub[T, int] and not Sub[int, T])
133+
(IsSub[T, str] and not IsSub[str, T])
134+
or (IsSub[T, bytes] and not IsSub[bytes, T])
135+
or (IsSub[T, bool] and not IsSub[bool, T])
136+
or (IsSub[T, int] and not IsSub[int, T])
137137
# XXX: enum, None
138138
)
139139
else Literal[False]
@@ -150,7 +150,7 @@ class Final(Mine, Ordinary, Wrapper[float], AnotherBase[float], Last[int]):
150150
# XXX: 'typing.Literal' is not *really* a type...
151151
# Maybe we can't do this, which maybe is fine.
152152
# if not IsSubtype[t, Literal]
153-
if not Sub[IsLiteral[t], Literal[True]]
153+
if not IsSub[IsLiteral[t], Literal[True]]
154154
]
155155
],
156156
GetQuals[p],

tests/test_type_eval.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
GetName,
2727
GetType,
2828
GetAnnotations,
29-
Sub,
29+
IsSub,
3030
Iter,
3131
Length,
3232
Member,
@@ -63,7 +63,7 @@ class F_int(F[int]):
6363
*[
6464
(
6565
Member[GetName[p], OrGotcha[GetType[p]]]
66-
if not Sub[GetType[p], A]
66+
if not IsSub[GetType[p], A]
6767
else Member[GetName[p], OrGotcha[MapRecursive[A]]]
6868
)
6969
# XXX: This next line *ought* to work, but we haven't
@@ -709,7 +709,7 @@ def test_uppercase_never():
709709

710710

711711
def test_never_is():
712-
d = eval_typing(Sub[Never, Never])
712+
d = eval_typing(IsSub[Never, Never])
713713
assert d is True
714714

715715

@@ -769,7 +769,7 @@ def test_eval_literal_idempotent_01():
769769

770770

771771
def test_is_literal_true_vs_one():
772-
assert eval_typing(Sub[Literal[True], Literal[1]]) is False
772+
assert eval_typing(IsSub[Literal[True], Literal[1]]) is False
773773

774774

775775
def test_callable_to_signature():
@@ -832,7 +832,7 @@ class AnnoTest:
832832

833833

834834
def test_type_eval_annotated_02():
835-
res = eval_typing(Sub[GetAttr[AnnoTest, Literal["a"]], int])
835+
res = eval_typing(IsSub[GetAttr[AnnoTest, Literal["a"]], int])
836836
assert res is True
837837

838838

typemap/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,4 @@ def IsSubSimilar(self, tps):
214214
return _IsGenericAlias(self, tps)
215215

216216

217-
Sub = IsSubSimilar
217+
IsSub = IsSubSimilar

0 commit comments

Comments
 (0)