-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Emit warning for invalid use of quoted types in union syntax #18183
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -251,3 +251,65 @@ foo: ReadableBuffer | |
[file was_mmap.py] | ||
from was_builtins import * | ||
class mmap: ... | ||
|
||
[case testUnionOrSyntaxWithQuotedOperandsNotAllowed] | ||
# flags: --python-version 3.10 | ||
from typing import Union, assert_type | ||
|
||
x1: "int" | str # E: X | Y syntax for unions cannot use quoted operands; use quotes around the entire expression instead | ||
x2: int | "str" # E: X | Y syntax for unions cannot use quoted operands; use quotes around the entire expression instead | ||
x3: int | str | "bytes" # E: X | Y syntax for unions cannot use quoted operands; use quotes around the entire expression instead | ||
assert_type(x1, Union[int, str]) | ||
assert_type(x2, Union[int, str]) | ||
assert_type(x3, Union[int, str, bytes]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test also valid cases, like |
||
|
||
[case testUnionOrSyntaxWithQuotedOperandsWithTypeVar] | ||
# flags: --python-version 3.10 | ||
import types | ||
from typing import TypeVar, Union, assert_type | ||
from typing_extensions import TypeAlias | ||
|
||
T = TypeVar("T") | ||
|
||
ok1: TypeAlias = T | "int" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be useful to have more tests for things like |
||
ok2: TypeAlias = "int" | T | ||
ok3: TypeAlias = int | T | "str" | ||
ok4: TypeAlias = "int" | T | "str" | ||
ok5: TypeAlias = T | "int" | str | ||
ok6: TypeAlias = T | int | "str" | ||
ok7: TypeAlias = list["str" | T] | ||
|
||
bad1: TypeAlias = "T" | "int" # E: X | Y syntax for unions cannot use quoted operands; use quotes around the entire expression instead | ||
bad2: TypeAlias = int | "str" | T # E: X | Y syntax for unions cannot use quoted operands; use quotes around the entire expression instead | ||
bad3: TypeAlias = list["str" | int] # E: X | Y syntax for unions cannot use quoted operands; use quotes around the entire expression instead | ||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testUnionOrSyntaxWithQuotedOperandsWithAlias] | ||
# flags: --python-version 3.10 | ||
import types | ||
from typing import TypeVar | ||
from typing_extensions import TypeAlias | ||
|
||
T = TypeVar("T") | ||
|
||
A1: TypeAlias = int | ||
A2: TypeAlias = int | str | ||
A3: TypeAlias = list[T] | ||
A4: TypeAlias = T | str | ||
|
||
x1: A1 | "bytes" # E: X | Y syntax for unions cannot use quoted operands; use quotes around the entire expression instead | ||
x2: A2 | "bytes" # E: X | Y syntax for unions cannot use quoted operands; use quotes around the entire expression instead | ||
x3: A3[int] | "bytes" # E: X | Y syntax for unions cannot use quoted operands; use quotes around the entire expression instead | ||
x4: A4[int] | "bytes" # ok | ||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testUnionOrSyntaxWithQuotedOperandsFutureAnnotations] | ||
# flags: --python-version 3.10 | ||
from __future__ import annotations | ||
import types | ||
from typing_extensions import TypeAlias | ||
|
||
x1: int | "str" # ok | ||
def f(x: int | "str"): pass # ok | ||
x2: TypeAlias = int | "str" # E: X | Y syntax for unions cannot use quoted operands; use quotes around the entire expression instead | ||
[builtins fixtures/tuple.pyi] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nit: This new if statement now accounts for most the code in this function. What about moving it to a helper function to make the basic logic clearer?