File tree Expand file tree Collapse file tree 6 files changed +49
-23
lines changed
Expand file tree Collapse file tree 6 files changed +49
-23
lines changed Original file line number Diff line number Diff line change 11conformant = " Pass"
22output = """
33directives_assert_type.py:27: error: Expression is of type "int | str", not "int" [assert-type]
4- directives_assert_type.py:28: error: Expression is of type "Any", not "int" [assert-type]
5- directives_assert_type.py:29: error: Expression is of type "Literal[4]", not "int" [assert-type]
6- directives_assert_type.py:31: error: "assert_type" expects 2 arguments [misc]
7- directives_assert_type.py:31: error: Too few arguments for "assert_type" [call-arg]
8- directives_assert_type.py:32: error: Expression is of type "Literal['']", not "int" [assert-type]
9- directives_assert_type.py:33: error: "assert_type" expects 2 arguments [misc]
10- directives_assert_type.py:33: error: Too many arguments for "assert_type" [call-arg]
4+ directives_assert_type.py:28: error: Expression is of type "int | str", not "Any" [assert-type]
5+ directives_assert_type.py:29: error: Expression is of type "Any", not "int" [assert-type]
6+ directives_assert_type.py:30: error: Expression is of type "Literal[4]", not "int" [assert-type]
7+ directives_assert_type.py:32: error: "assert_type" expects 2 arguments [misc]
8+ directives_assert_type.py:32: error: Too few arguments for "assert_type" [call-arg]
9+ directives_assert_type.py:33: error: Expression is of type "Literal['']", not "int" [assert-type]
10+ directives_assert_type.py:34: error: "assert_type" expects 2 arguments [misc]
11+ directives_assert_type.py:34: error: Too many arguments for "assert_type" [call-arg]
1112"""
1213conformance_automated = " Pass"
1314errors_diff = """
Original file line number Diff line number Diff line change @@ -4,9 +4,10 @@ errors_diff = """
44"""
55output = """
66ERROR directives_assert_type.py:27:16-24: assert_type(int | str, int) failed [assert-type]
7- ERROR directives_assert_type.py:28:16-24: assert_type(Any, int) failed [assert-type]
8- ERROR directives_assert_type.py:29:16-24: assert_type(Literal[4], int) failed [assert-type]
9- ERROR directives_assert_type.py:31:16-18: assert_type needs 2 positional arguments, got 0 [bad-argument-count]
10- ERROR directives_assert_type.py:32:16-25: assert_type(Literal[''], int) failed [assert-type]
11- ERROR directives_assert_type.py:33:16-33: assert_type needs 2 positional arguments, got 3 [bad-argument-count]
7+ ERROR directives_assert_type.py:28:16-24: assert_type(int | str, Any) failed [assert-type]
8+ ERROR directives_assert_type.py:29:16-24: assert_type(Any, int) failed [assert-type]
9+ ERROR directives_assert_type.py:30:16-24: assert_type(Literal[4], int) failed [assert-type]
10+ ERROR directives_assert_type.py:32:16-18: assert_type needs 2 positional arguments, got 0 [bad-argument-count]
11+ ERROR directives_assert_type.py:33:16-25: assert_type(Literal[''], int) failed [assert-type]
12+ ERROR directives_assert_type.py:34:16-33: assert_type needs 2 positional arguments, got 3 [bad-argument-count]
1213"""
Original file line number Diff line number Diff line change 11conformant = " Pass"
22output = """
33directives_assert_type.py:27:17 - error: "assert_type" mismatch: expected "int" but received "int | str" (reportAssertTypeFailure)
4- directives_assert_type.py:28:17 - error: "assert_type" mismatch: expected "int" but received "Any" (reportAssertTypeFailure)
5- directives_assert_type.py:29:17 - error: "assert_type" mismatch: expected "int" but received "Literal[4]" (reportAssertTypeFailure)
6- directives_assert_type.py:31:5 - error: "assert_type" expects two positional arguments (reportCallIssue)
7- directives_assert_type.py:32:17 - error: "assert_type" mismatch: expected "int" but received "Literal['']" (reportAssertTypeFailure)
8- directives_assert_type.py:33:5 - error: "assert_type" expects two positional arguments (reportCallIssue)
4+ directives_assert_type.py:28:17 - error: "assert_type" mismatch: expected "Any" but received "int | str" (reportAssertTypeFailure)
5+ directives_assert_type.py:29:17 - error: "assert_type" mismatch: expected "int" but received "Any" (reportAssertTypeFailure)
6+ directives_assert_type.py:30:17 - error: "assert_type" mismatch: expected "int" but received "Literal[4]" (reportAssertTypeFailure)
7+ directives_assert_type.py:32:5 - error: "assert_type" expects two positional arguments (reportCallIssue)
8+ directives_assert_type.py:33:17 - error: "assert_type" mismatch: expected "int" but received "Literal['']" (reportAssertTypeFailure)
9+ directives_assert_type.py:34:5 - error: "assert_type" expects two positional arguments (reportCallIssue)
10+ directives_assert_type.py:41:17 - error: "assert_type" mismatch: expected "str | Literal['spam']" but received "str" (reportAssertTypeFailure)
911"""
1012conformance_automated = " Pass"
1113errors_diff = """
Original file line number Diff line number Diff line change @@ -3,9 +3,11 @@ errors_diff = """
33"""
44output = """
55directives_assert_type.py:27: error: Expression is of type "int | str", not "int" [misc]
6- directives_assert_type.py:28: error: Expression is of type "Any", not "int" [misc]
7- directives_assert_type.py:29: error: Expression is of type "Literal[4]", not "int" [misc]
8- directives_assert_type.py:31: error: "assert_type" expects 2 arguments [call-arg]
9- directives_assert_type.py:32: error: Expression is of type "Literal['']", not "int" [misc]
10- directives_assert_type.py:33: error: "assert_type" expects 2 arguments [call-arg]
6+ directives_assert_type.py:28: error: Expression is of type "int | str", not "Any" [misc]
7+ directives_assert_type.py:29: error: Expression is of type "Any", not "int" [misc]
8+ directives_assert_type.py:30: error: Expression is of type "Literal[4]", not "int" [misc]
9+ directives_assert_type.py:32: error: "assert_type" expects 2 arguments [call-arg]
10+ directives_assert_type.py:33: error: Expression is of type "Literal['']", not "int" [misc]
11+ directives_assert_type.py:34: error: "assert_type" expects 2 arguments [call-arg]
12+ directives_assert_type.py:41: error: Expression is of type "str", not "str | Literal['spam']" [misc]
1113"""
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ def func1(
2525 assert_type (e , Literal [4 ]) # OK
2626
2727 assert_type (a , int ) # E: Type mismatch
28+ assert_type (a , Any ) # E: Type mismatch
2829 assert_type (c , int ) # E: Type mismatch
2930 assert_type (e , int ) # E: Type mismatch
3031
@@ -33,5 +34,12 @@ def func1(
3334 assert_type (a , int | str , a ) # E: too many arguments
3435
3536
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+
3644class ForwardReference :
3745 pass
Original file line number Diff line number Diff line change @@ -9,7 +9,8 @@ Type checker directives
99-----------------
1010
1111The function ``typing.assert_type(val, typ) `` allows users to
12- ask a static type checker to confirm that *val * has an inferred type of *typ *.
12+ ask a static type checker to confirm that the inferred type of *val *
13+ is :term: `equivalent ` to *typ *.
1314
1415When a type checker encounters a call to ``assert_type() ``, it
1516should emit an error if the value is not of the specified type::
@@ -18,6 +19,17 @@ should emit an error if the value is not of the specified type::
1819 assert_type(name, str) # OK, inferred type of `name` is `str`
1920 assert_type(name, int) # type checker error
2021
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+
2133The second argument must be a valid :term: `type expression `.
2234
2335.. _`reveal-type` :
You can’t perform that action at this time.
0 commit comments