Skip to content

Commit 08d866b

Browse files
Rename TypeExpr to TypeForm (#475)
No backwards compatibility required because we never released TypeExpr. Also took the opportunity to expand the docstring.
1 parent 7632716 commit 08d866b

File tree

4 files changed

+55
-41
lines changed

4 files changed

+55
-41
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Unreleased
22

3-
- Add `typing_extensions.TypeExpr` from PEP 747. Patch by
3+
- Add `typing_extensions.TypeForm` from PEP 747. Patch by
44
Jelle Zijlstra.
55
- Add `typing_extensions.get_annotations`, a backport of
66
`inspect.get_annotations` that adds features specified

doc/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,9 @@ Special typing primitives
367367

368368
.. versionadded:: 4.6.0
369369

370-
.. data:: TypeExpr
370+
.. data:: TypeForm
371371

372-
See :pep:`747`. A type hint representing a type expression.
372+
See :pep:`747`. A special form representing the value of a type expression.
373373

374374
.. versionadded:: 4.13.0
375375

src/test_typing_extensions.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
TypeAlias,
7171
TypeAliasType,
7272
TypedDict,
73-
TypeExpr,
73+
TypeForm,
7474
TypeGuard,
7575
TypeIs,
7676
TypeVar,
@@ -5508,33 +5508,33 @@ def test_no_isinstance(self):
55085508
issubclass(int, TypeIs)
55095509

55105510

5511-
class TypeExprTests(BaseTestCase):
5511+
class TypeFormTests(BaseTestCase):
55125512
def test_basics(self):
5513-
TypeExpr[int] # OK
5514-
self.assertEqual(TypeExpr[int], TypeExpr[int])
5513+
TypeForm[int] # OK
5514+
self.assertEqual(TypeForm[int], TypeForm[int])
55155515

5516-
def foo(arg) -> TypeExpr[int]: ...
5517-
self.assertEqual(gth(foo), {'return': TypeExpr[int]})
5516+
def foo(arg) -> TypeForm[int]: ...
5517+
self.assertEqual(gth(foo), {'return': TypeForm[int]})
55185518

55195519
def test_repr(self):
5520-
if hasattr(typing, 'TypeExpr'):
5520+
if hasattr(typing, 'TypeForm'):
55215521
mod_name = 'typing'
55225522
else:
55235523
mod_name = 'typing_extensions'
5524-
self.assertEqual(repr(TypeExpr), f'{mod_name}.TypeExpr')
5525-
cv = TypeExpr[int]
5526-
self.assertEqual(repr(cv), f'{mod_name}.TypeExpr[int]')
5527-
cv = TypeExpr[Employee]
5528-
self.assertEqual(repr(cv), f'{mod_name}.TypeExpr[{__name__}.Employee]')
5529-
cv = TypeExpr[Tuple[int]]
5530-
self.assertEqual(repr(cv), f'{mod_name}.TypeExpr[typing.Tuple[int]]')
5524+
self.assertEqual(repr(TypeForm), f'{mod_name}.TypeForm')
5525+
cv = TypeForm[int]
5526+
self.assertEqual(repr(cv), f'{mod_name}.TypeForm[int]')
5527+
cv = TypeForm[Employee]
5528+
self.assertEqual(repr(cv), f'{mod_name}.TypeForm[{__name__}.Employee]')
5529+
cv = TypeForm[Tuple[int]]
5530+
self.assertEqual(repr(cv), f'{mod_name}.TypeForm[typing.Tuple[int]]')
55315531

55325532
def test_cannot_subclass(self):
55335533
with self.assertRaises(TypeError):
5534-
class C(type(TypeExpr)):
5534+
class C(type(TypeForm)):
55355535
pass
55365536
with self.assertRaises(TypeError):
5537-
class D(type(TypeExpr[int])):
5537+
class D(type(TypeForm[int])):
55385538
pass
55395539

55405540
def test_call(self):
@@ -5546,24 +5546,24 @@ def test_call(self):
55465546
]
55475547
for obj in objs:
55485548
with self.subTest(obj=obj):
5549-
self.assertIs(TypeExpr(obj), obj)
5549+
self.assertIs(TypeForm(obj), obj)
55505550

55515551
with self.assertRaises(TypeError):
5552-
TypeExpr()
5552+
TypeForm()
55535553
with self.assertRaises(TypeError):
5554-
TypeExpr("too", "many")
5554+
TypeForm("too", "many")
55555555

55565556
def test_cannot_init_type(self):
55575557
with self.assertRaises(TypeError):
5558-
type(TypeExpr)()
5558+
type(TypeForm)()
55595559
with self.assertRaises(TypeError):
5560-
type(TypeExpr[Optional[int]])()
5560+
type(TypeForm[Optional[int]])()
55615561

55625562
def test_no_isinstance(self):
55635563
with self.assertRaises(TypeError):
5564-
isinstance(1, TypeExpr[int])
5564+
isinstance(1, TypeForm[int])
55655565
with self.assertRaises(TypeError):
5566-
issubclass(int, TypeExpr)
5566+
issubclass(int, TypeForm)
55675567

55685568

55695569
class LiteralStringTests(BaseTestCase):

src/typing_extensions.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
'Text',
8787
'TypeAlias',
8888
'TypeAliasType',
89-
'TypeExpr',
89+
'TypeForm',
9090
'TypeGuard',
9191
'TypeIs',
9292
'TYPE_CHECKING',
@@ -2047,23 +2047,30 @@ def f(val: Union[int, Awaitable[int]]) -> int:
20472047
""")
20482048

20492049
# 3.14+?
2050-
if hasattr(typing, 'TypeExpr'):
2051-
TypeExpr = typing.TypeExpr
2050+
if hasattr(typing, 'TypeForm'):
2051+
TypeForm = typing.TypeForm
20522052
# 3.9
20532053
elif sys.version_info[:2] >= (3, 9):
2054-
class _TypeExprForm(_ExtensionsSpecialForm, _root=True):
2055-
# TypeExpr(X) is equivalent to X but indicates to the type checker
2056-
# that the object is a TypeExpr.
2054+
class _TypeFormForm(_ExtensionsSpecialForm, _root=True):
2055+
# TypeForm(X) is equivalent to X but indicates to the type checker
2056+
# that the object is a TypeForm.
20572057
def __call__(self, obj, /):
20582058
return obj
20592059

2060-
@_TypeExprForm
2061-
def TypeExpr(self, parameters):
2062-
"""Special typing form used to represent a type expression.
2060+
@_TypeFormForm
2061+
def TypeForm(self, parameters):
2062+
"""A special form representing the value that results from the evaluation
2063+
of a type expression. This value encodes the information supplied in the
2064+
type expression, and it represents the type described by that type expression.
2065+
2066+
When used in a type expression, TypeForm describes a set of type form objects.
2067+
It accepts a single type argument, which must be a valid type expression.
2068+
``TypeForm[T]`` describes the set of all type form objects that represent
2069+
the type T or types that are assignable to T.
20632070
20642071
Usage:
20652072
2066-
def cast[T](typ: TypeExpr[T], value: Any) -> T: ...
2073+
def cast[T](typ: TypeForm[T], value: Any) -> T: ...
20672074
20682075
reveal_type(cast(int, "x")) # int
20692076
@@ -2073,7 +2080,7 @@ def cast[T](typ: TypeExpr[T], value: Any) -> T: ...
20732080
return typing._GenericAlias(self, (item,))
20742081
# 3.8
20752082
else:
2076-
class _TypeExprForm(_ExtensionsSpecialForm, _root=True):
2083+
class _TypeFormForm(_ExtensionsSpecialForm, _root=True):
20772084
def __getitem__(self, parameters):
20782085
item = typing._type_check(parameters,
20792086
f'{self._name} accepts only a single type')
@@ -2082,13 +2089,20 @@ def __getitem__(self, parameters):
20822089
def __call__(self, obj, /):
20832090
return obj
20842091

2085-
TypeExpr = _TypeExprForm(
2086-
'TypeExpr',
2087-
doc="""Special typing form used to represent a type expression.
2092+
TypeForm = _TypeFormForm(
2093+
'TypeForm',
2094+
doc="""A special form representing the value that results from the evaluation
2095+
of a type expression. This value encodes the information supplied in the
2096+
type expression, and it represents the type described by that type expression.
2097+
2098+
When used in a type expression, TypeForm describes a set of type form objects.
2099+
It accepts a single type argument, which must be a valid type expression.
2100+
``TypeForm[T]`` describes the set of all type form objects that represent
2101+
the type T or types that are assignable to T.
20882102
20892103
Usage:
20902104
2091-
def cast[T](typ: TypeExpr[T], value: Any) -> T: ...
2105+
def cast[T](typ: TypeForm[T], value: Any) -> T: ...
20922106
20932107
reveal_type(cast(int, "x")) # int
20942108

0 commit comments

Comments
 (0)