Skip to content

Commit 6f52931

Browse files
committed
Always require using Literal for Literal type arguments
Previously we were allowing stuff like `GetArg[T, Base, 0]`. This requires it to be `GetArg[T, Base, Literal[0]]`. This is obviously kind of uglier but means we don't need to propose an expansion of how literal type are treated. (And, importantly, I don't think it's possible to reliably make *strings* work, because of how strings are treated as type references...)
1 parent 25e4ba0 commit 6f52931

8 files changed

Lines changed: 135 additions & 133 deletions

File tree

pre-pep.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ grabs the argument to a ``Pointer``).
214214

215215
::
216216

217-
type PointerArg[T: Pointer] = GetArg[T, Pointer, 0]
217+
type PointerArg[T: Pointer] = GetArg[T, Pointer, Literal[0]]
218218

219219
``AdjustLink`` sticks a ``list`` around ``MultiLink``, using features
220220
we've discussed already.

spec-draft.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ It's important that there be a clearly specified type language for the type-leve
111111
# *[... for t in ...] arguments
112112
| <ident>[<variadic-type-arg(<type>)> +]
113113

114-
| <string-or-int-literal> # Only accepted in arguments to new functions?
115-
116114

117115
# Type conditional checks are just boolean compositions of
118116
# subtype checking.

tests/test_fastapilike_2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ class Field[T: FieldArgs](InitField[T]):
4646
####
4747

4848
# TODO: Should this go into the stdlib?
49-
type GetFieldItem[T: InitField, K] = GetAttr[GetArg[T, InitField, 0], K]
49+
type GetFieldItem[T: InitField, K] = GetAttr[
50+
GetArg[T, InitField, Literal[0]], K
51+
]
5052

5153

5254
##

tests/test_qblike.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Link[T]:
3737

3838
# Conditional type alias!
3939
type FilterLinks[T] = (
40-
Link[PropsOnly[GetArg[T, Link, 0]]] if IsSub[T, Link] else T
40+
Link[PropsOnly[GetArg[T, Link, Literal[0]]]] if IsSub[T, Link] else T
4141
)
4242

4343

tests/test_qblike_2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def select[ModelT, K: BaseTypedDict](
108108
grabs the argument to a ``Pointer``).
109109
110110
"""
111-
type PointerArg[T: Pointer] = GetArg[T, Pointer, 0]
111+
type PointerArg[T: Pointer] = GetArg[T, Pointer, Literal[0]]
112112

113113
"""
114114
``AdjustLink`` sticks a ``list`` around ``MultiLink``, using features

tests/test_type_dir.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 IsSub[T, Base] else Never
83+
type BaseArg[T] = GetArg[T, Base, Literal[0]] if IsSub[T, Base] else Never
8484

8585

8686
type AllOptional[T] = NewProtocol[

0 commit comments

Comments
 (0)