Skip to content

Commit 5b5a22d

Browse files
committed
Support typing.assert_type.
* Aliases typing.assert_type to our assert_type pseudo-builtin. * Removes single-argument assert_type form. This functionality didn't make it into the typing library variant. I don't think it's used much anyway, given that pytype_extensions.assert_type doesn't support it at runtime, and no one's ever noticed. PiperOrigin-RevId: 577000283
1 parent c452719 commit 5b5a22d

File tree

6 files changed

+15
-26
lines changed

6 files changed

+15
-26
lines changed

pytype/errors.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,16 +1250,10 @@ def reveal_type(self, stack, node, var):
12501250
self.error(stack, self._var_to_printed_type(var, node))
12511251

12521252
@_error_name("assert-type")
1253-
def assert_type(self, stack, node, var, typ=None):
1253+
def assert_type(self, stack, node, var, typ):
12541254
"""Check that a variable type matches its expected value."""
12551255
actual = self._var_to_printed_type(var, node)
12561256

1257-
# assert_type(x) checks that x is not Any
1258-
if typ is None:
1259-
if actual in ("Any", "typing.Any"):
1260-
self.error(stack, f"Asserted type was {actual}")
1261-
return
1262-
12631257
try:
12641258
expected = abstract_utils.get_atomic_python_constant(typ, str)
12651259
except abstract_utils.ConversionError:

pytype/overlays/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ py_library(
320320
.named_tuple
321321
.overlay
322322
.overlay_utils
323+
.special_builtins
323324
.typed_dict
324325
pytype.utils
325326
pytype.abstract.abstract

pytype/overlays/special_builtins.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -553,10 +553,7 @@ class AssertType(BuiltinFunction):
553553
_NAME = "assert_type"
554554

555555
def call(self, node, func, args, alias_map=None):
556-
if len(args.posargs) == 1:
557-
a, = args.posargs
558-
t = None
559-
elif len(args.posargs) == 2:
556+
if len(args.posargs) == 2:
560557
a, t = args.posargs
561558
else:
562559
raise function.WrongArgCount(self._SIGNATURE, args, self.ctx)

pytype/overlays/typing_overlay.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pytype.overlays import named_tuple
1717
from pytype.overlays import overlay
1818
from pytype.overlays import overlay_utils
19+
from pytype.overlays import special_builtins
1920
from pytype.overlays import typed_dict
2021
from pytype.pytd import pep484
2122
from pytype.pytd import pytd
@@ -627,7 +628,6 @@ def build_re_member(ctx, module):
627628
"TypeVarTuple": (3, 11),
628629
"Unpack": (3, 11),
629630
"assert_never": (3, 11),
630-
"assert_type": (3, 11),
631631
"reveal_type": (3, 11),
632632
}
633633

@@ -658,6 +658,9 @@ def build_re_member(ctx, module):
658658
"TypedDict": (overlay.drop_module(typed_dict.TypedDictBuilder), (3, 8)),
659659
"Union": (overlay.drop_module(Union), None),
660660
"TYPE_CHECKING": (overlay.drop_module(build_typechecking), None),
661+
"assert_type": (
662+
overlay.add_name("assert_type", special_builtins.AssertType.make_alias),
663+
(3, 11)),
661664
"cast": (overlay.add_name("cast", Cast.make), None),
662665
"clear_overloads": (_builder_from_name("clear_overloads"), (3, 11)),
663666
"dataclass_transform": (overlay.add_name(

pytype/tests/test_errors2.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -345,31 +345,27 @@ def test_assert_type(self):
345345
class A: pass
346346
def f(x: int, y: str, z):
347347
assert_type(x, int)
348-
assert_type(y, int) # assert-type[e1]
349-
assert_type(z) # assert-type[e2]
348+
assert_type(y, int) # assert-type[e]
350349
if __random__:
351350
x = A()
352351
assert_type(x, Union[A, int])
353352
""")
354353
self.assertErrorSequences(errors, {
355-
"e1": ["Expected", "int", "Actual", "str"],
356-
"e2": ["type was Any"]
354+
"e": ["Expected", "int", "Actual", "str"],
357355
})
358356

359357
def test_assert_type_str(self):
360358
_, errors = self.InferWithErrors("""
361359
class A: pass
362360
def f(x: int, y: str, z):
363361
assert_type(x, 'int')
364-
assert_type(y, 'int') # assert-type[e1]
365-
assert_type(z) # assert-type[e2]
362+
assert_type(y, 'int') # assert-type[e]
366363
if __random__:
367364
x = A()
368365
assert_type(x, 'Union[A, int]')
369366
""")
370367
self.assertErrorSequences(errors, {
371-
"e1": ["Expected", "int", "Actual", "str"],
372-
"e2": ["type was Any"]
368+
"e": ["Expected", "int", "Actual", "str"],
373369
})
374370

375371
def test_assert_type_import(self):
@@ -383,15 +379,13 @@ def assert_type(*args): ...
383379
class A: pass
384380
def f(x: int, y: str, z):
385381
assert_type(x, int)
386-
assert_type(y, int) # assert-type[e1]
387-
assert_type(z) # assert-type[e2]
382+
assert_type(y, int) # assert-type[e]
388383
if __random__:
389384
x = A()
390385
assert_type(x, Union[A, int])
391386
""", pythonpath=[d.path])
392387
self.assertErrorSequences(errors, {
393-
"e1": ["Expected", "int", "Actual", "str"],
394-
"e2": ["type was Any"]
388+
"e": ["Expected", "int", "Actual", "str"],
395389
})
396390

397391
def test_combine_containers(self):

pytype/tests/test_pattern_matching.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,8 +718,8 @@ def f(x: A | B, y: A | B):
718718
assert_type(c, int)
719719
assert_type(d, str)
720720
case _:
721-
assert_type(x)
722-
assert_type(y)
721+
assert_type(x, B)
722+
assert_type(y, A)
723723
""")
724724

725725
def test_builtin(self):

0 commit comments

Comments
 (0)