-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Implement meet for literal types; add tests for 'is_same_type' #6043
Changes from 3 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 |
---|---|---|
|
@@ -1240,3 +1240,81 @@ indirect.Literal() | |
[builtins fixtures/isinstancelist.pyi] | ||
[out] | ||
|
||
|
||
-- | ||
-- Other misc interactions | ||
-- | ||
|
||
[case testLiteralMeetsWithCallablesInLists] | ||
from typing import List, Callable, Union | ||
from typing_extensions import Literal | ||
|
||
a: Callable[[Literal[1]], int] | ||
b: Callable[[Literal[2]], str] | ||
c: Callable[[int], str] | ||
d: Callable[[object], str] | ||
e: Callable[[Union[Literal[1], Literal[2]]], str] | ||
|
||
arr1 = [a, a] | ||
arr2 = [a, b] | ||
arr3 = [a, c] | ||
arr4 = [a, d] | ||
arr5 = [a, e] | ||
|
||
reveal_type(arr1) # E: Revealed type is 'builtins.list[def (Literal[1]) -> builtins.int]' | ||
reveal_type(arr2) # E: Revealed type is 'builtins.list[builtins.function*]' | ||
reveal_type(arr3) # E: Revealed type is 'builtins.list[def (Literal[1]) -> builtins.object]' | ||
reveal_type(arr4) # E: Revealed type is 'builtins.list[def (Literal[1]) -> builtins.object]' | ||
reveal_type(arr5) # E: Revealed type is 'builtins.list[def (Literal[1]) -> builtins.object]' | ||
|
||
lit: Literal[1] | ||
reveal_type(arr1[0](lit)) # E: Revealed type is 'builtins.int' | ||
reveal_type(arr2[0](lit)) # E: Revealed type is 'Any' \ | ||
# E: Cannot call function of unknown type | ||
reveal_type(arr3[0](lit)) # E: Revealed type is 'builtins.object' | ||
reveal_type(arr4[0](lit)) # E: Revealed type is 'builtins.object' | ||
reveal_type(arr5[0](lit)) # E: Revealed type is 'builtins.object' | ||
[builtins fixtures/list.pyi] | ||
[out] | ||
|
||
[case testLiteralMeetsWithTypeVars] | ||
from typing import TypeVar, Callable, Union | ||
from typing_extensions import Literal | ||
|
||
T = TypeVar('T') | ||
def unify(func: Callable[[T, T], None]) -> T: pass | ||
|
||
def f1(x: Literal[1], y: Literal[1]) -> None: pass | ||
def f2(x: Literal[1], y: Literal[2]) -> None: pass | ||
def f3(x: Literal[1], y: int) -> None: pass | ||
def f4(x: Literal[1], y: object) -> None: pass | ||
def f5(x: Literal[1], y: Union[Literal[1], Literal[2]]) -> None: pass | ||
|
||
reveal_type(unify(f1)) # E: Revealed type is 'Literal[1]' | ||
reveal_type(unify(f2)) # E: Revealed type is 'None' | ||
reveal_type(unify(f3)) # E: Revealed type is 'Literal[1]' | ||
reveal_type(unify(f4)) # E: Revealed type is 'Literal[1]' | ||
reveal_type(unify(f5)) # E: Revealed type is 'Literal[1]' | ||
[out] | ||
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. TBH, having both these tests look a bit redundant (especially that you have very good unit tests). I would combine them into a single tests and check meeting some type one way, and some types another way. 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. I consolidated both tests into one and pruned out some probably-overkill |
||
|
||
[case testLiteralMeetsWithStrictOptional] | ||
# flags: --strict-optional | ||
from typing import TypeVar, Callable, Union | ||
from typing_extensions import Literal | ||
|
||
a: Callable[[Literal[1]], int] | ||
b: Callable[[Literal[2]], str] | ||
lit: Literal[1] | ||
|
||
arr = [a, b] | ||
reveal_type(arr) # E: Revealed type is 'builtins.list[builtins.function*]' | ||
reveal_type(arr[0](lit)) # E: Revealed type is 'Any' \ | ||
# E: Cannot call function of unknown type | ||
|
||
T = TypeVar('T') | ||
def unify(func: Callable[[T, T], None]) -> T: pass | ||
def func(x: Literal[1], y: Literal[2]) -> None: pass | ||
|
||
reveal_type(unify(func)) # E: Revealed type is '<nothing>' | ||
[builtins fixtures/list.pyi] | ||
[out] |
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.
Maybe test these two in reverse order too? (i.e. if literal is the second one)
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.
The implementation of
assert_meet
already does this for us, thankfully: it's defined as just:...where
assert_simple_meet
is responsible for actually doing the checking.