-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use X | Y union syntax in error messages (#15102)
This should fix #15082: If the python version is set to 3.10 or later union error reports are going to be displayed using the `X | Y` syntax. If None is found in a union it is shown as the last element in order to improve visibility (`Union[bool, None, str]` becomes `"bool | str | None"`. To achieve this an option `use_or_syntax()` is created that is set to true if the python version is 3.10 or later. For testing a hidden flag --force-union-syntax is used that sets the option to false.
- Loading branch information
1 parent
fe8873f
commit 334daca
Showing
8 changed files
with
103 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
[case testUnionErrorSyntax] | ||
# flags: --python-version 3.10 --no-force-union-syntax | ||
from typing import Union | ||
x : Union[bool, str] | ||
x = 3 # E: Incompatible types in assignment (expression has type "int", variable has type "bool | str") | ||
|
||
[case testOrErrorSyntax] | ||
# flags: --python-version 3.10 --force-union-syntax | ||
from typing import Union | ||
x : Union[bool, str] | ||
x = 3 # E: Incompatible types in assignment (expression has type "int", variable has type "Union[bool, str]") | ||
|
||
[case testOrNoneErrorSyntax] | ||
# flags: --python-version 3.10 --no-force-union-syntax | ||
from typing import Union | ||
x : Union[bool, None] | ||
x = 3 # E: Incompatible types in assignment (expression has type "int", variable has type "bool | None") | ||
|
||
[case testOptionalErrorSyntax] | ||
# flags: --python-version 3.10 --force-union-syntax | ||
from typing import Union | ||
x : Union[bool, None] | ||
x = 3 # E: Incompatible types in assignment (expression has type "int", variable has type "Optional[bool]") | ||
|
||
[case testNoneAsFinalItem] | ||
# flags: --python-version 3.10 --no-force-union-syntax | ||
from typing import Union | ||
x : Union[bool, None, str] | ||
x = 3 # E: Incompatible types in assignment (expression has type "int", variable has type "bool | str | None") | ||
|
||
[case testLiteralOrErrorSyntax] | ||
# flags: --python-version 3.10 --no-force-union-syntax | ||
from typing import Union | ||
from typing_extensions import Literal | ||
x : Union[Literal[1], Literal[2], str] | ||
x = 3 # E: Incompatible types in assignment (expression has type "Literal[3]", variable has type "Literal[1, 2] | str") | ||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testLiteralUnionErrorSyntax] | ||
# flags: --python-version 3.10 --force-union-syntax | ||
from typing import Union | ||
from typing_extensions import Literal | ||
x : Union[Literal[1], Literal[2], str] | ||
x = 3 # E: Incompatible types in assignment (expression has type "Literal[3]", variable has type "Union[str, Literal[1, 2]]") | ||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testLiteralOrNoneErrorSyntax] | ||
# flags: --python-version 3.10 --no-force-union-syntax | ||
from typing import Union | ||
from typing_extensions import Literal | ||
x : Union[Literal[1], None] | ||
x = 3 # E: Incompatible types in assignment (expression has type "Literal[3]", variable has type "Literal[1] | None") | ||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testLiteralOptionalErrorSyntax] | ||
# flags: --python-version 3.10 --force-union-syntax | ||
from typing import Union | ||
from typing_extensions import Literal | ||
x : Union[Literal[1], None] | ||
x = 3 # E: Incompatible types in assignment (expression has type "Literal[3]", variable has type "Optional[Literal[1]]") | ||
[builtins fixtures/tuple.pyi] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters