Skip to content

Commit ac0a0b4

Browse files
committed
more lenient
1 parent 2d2041b commit ac0a0b4

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

conformance/tests/directives_assert_type.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ def func1(
1919
e: Annotated[Literal[4], ""],
2020
):
2121
assert_type(a, int | str) # OK
22-
assert_type(a, int | bool | str) # OK (equivalent type)
2322
assert_type(b, list[int]) # OK
2423
assert_type(c, Any) # OK
2524
assert_type(d, "ForwardReference") # OK
2625
assert_type(e, Literal[4]) # OK
2726

2827
assert_type(a, int) # E: Type mismatch
28+
assert_type(a, Any) # E: Type mismatch
2929
assert_type(c, int) # E: Type mismatch
3030
assert_type(e, int) # E: Type mismatch
3131

@@ -34,5 +34,12 @@ def func1(
3434
assert_type(a, int | str, a) # E: too many arguments
3535

3636

37+
# > If the two types are :term:`equivalent` but syntactically different,
38+
# > the type checker may reject the ``assert_type()`` call::
39+
40+
def func2(name: str):
41+
assert_type(name, str | Literal["spam"]) # E?: Equivalent but not identical
42+
43+
3744
class ForwardReference:
3845
pass

docs/spec/directives.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@ should emit an error if the value is not of the specified type::
1717

1818
def greet(name: str) -> None:
1919
assert_type(name, str) # OK, inferred type of `name` is `str`
20-
assert_type(name, str | Literal["spam"]) # OK, this type is equivalent to `str`
2120
assert_type(name, int) # type checker error
2221

22+
If the two types are :term:`equivalent` but syntactically different,
23+
the type checker may reject the ``assert_type()`` call::
24+
25+
from typing import assert_type, Literal
26+
27+
def greet(name: str) -> None:
28+
assert_type(name, str | Literal["spam"]) # type checker may error
29+
30+
Type checkers should aim to minimize cases where they reject
31+
``assert_type()`` calls that use equivalent types.
32+
2333
The second argument must be a valid :term:`type expression`.
2434

2535
.. _`reveal-type`:

0 commit comments

Comments
 (0)