mirror of
https://github.com/microsoft/pyright.git
synced 2024-09-11 16:06:39 +03:00
Added unit test for expected type matching.
This commit is contained in:
parent
32849ed410
commit
bc6dd0265a
36
server/src/tests/samples/assignment3.py
Normal file
36
server/src/tests/samples/assignment3.py
Normal file
@ -0,0 +1,36 @@
|
||||
# This sample tests various assignment scenarios where
|
||||
# there is an expected type, so bidirectional type
|
||||
# inference is used.
|
||||
|
||||
from typing import Callable, Dict, Tuple
|
||||
|
||||
f1: Callable[[int, int], int] = lambda a, b: a + b
|
||||
|
||||
# This should generate an error because x should be
|
||||
# determined to be an "int", so "len(x)" is invalid.
|
||||
map(lambda x: len(x), [1, 2, 3])
|
||||
|
||||
def must_be_int(val: int):
|
||||
return val
|
||||
|
||||
d1: Dict[str, Tuple[int, Callable[[int], int]]] = {
|
||||
'hello': (3, lambda x: must_be_int(x))
|
||||
}
|
||||
|
||||
d2: Dict[str, Tuple[int, Callable[[int], int]]] = {
|
||||
# This should generate an error because the key is not a str.
|
||||
3: (3, lambda x: must_be_int(x))
|
||||
}
|
||||
|
||||
d3: Dict[str, Tuple[int, Callable[[int], int]]] = {
|
||||
# This should generate an error because the first element
|
||||
# of the tuple is not the correct type.
|
||||
'3': (3.0, lambda x: must_be_int(x))
|
||||
}
|
||||
|
||||
d4: Dict[str, Tuple[int, Callable[[int], int]]] = {
|
||||
# This should generate an error because the lambda
|
||||
# type doesn't match.
|
||||
'3': (3, lambda _: 3.4)
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
# This sample tests the type checker's handling of
|
||||
# syntesized __init__ and __new__ methods for
|
||||
# synthesized __init__ and __new__ methods for
|
||||
# dataclass classes and their subclasses.
|
||||
|
||||
from dataclasses import dataclass
|
||||
@ -23,4 +23,4 @@ b = B(a, 5)
|
||||
a = A(3, 4)
|
||||
|
||||
# This should generate an error because there is one too few parameters
|
||||
b = B(3)
|
||||
b = B(a)
|
||||
|
@ -543,6 +543,12 @@ test('Assignment2', () => {
|
||||
validateResults(analysisResults, 2);
|
||||
});
|
||||
|
||||
test('Assignment3', () => {
|
||||
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['assignment3.py']);
|
||||
|
||||
validateResults(analysisResults, 4);
|
||||
});
|
||||
|
||||
test('AugmentedAssignment1', () => {
|
||||
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['augmentedAssignment1.py']);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user