tests: add tests for type checker
This commit is contained in:
14
tests/cases/checker/01_simple_types.py
Normal file
14
tests/cases/checker/01_simple_types.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
# type: ignore
|
||||||
|
# ruff: disable[F821]
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
df: Frame[
|
||||||
|
verified: bool,
|
||||||
|
birth_year: int,
|
||||||
|
height: float + ( _ > 0 ) + ( _ < 250 ),
|
||||||
|
name: str,
|
||||||
|
date: datetime,
|
||||||
|
float,
|
||||||
|
unknown: _,
|
||||||
|
_
|
||||||
|
]
|
||||||
3
tests/cases/checker/01_simple_types.py.ref.json
Normal file
3
tests/cases/checker/01_simple_types.py.ref.json
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"diagnostics": []
|
||||||
|
}
|
||||||
11
tests/cases/checker/02_simple_operations.py
Normal file
11
tests/cases/checker/02_simple_operations.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
a: int = 3
|
||||||
|
b: int = 4
|
||||||
|
|
||||||
|
c = a + b
|
||||||
|
|
||||||
|
c = "invalid"
|
||||||
|
|
||||||
|
d = True
|
||||||
|
e = d + d
|
||||||
|
|
||||||
|
f: float = a
|
||||||
46
tests/cases/checker/02_simple_operations.py.ref.json
Normal file
46
tests/cases/checker/02_simple_operations.py.ref.json
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
{
|
||||||
|
"diagnostics": [
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
6,
|
||||||
|
0
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
6,
|
||||||
|
13
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Cannot assign BaseType(name='str') to c of type BaseType(name='int')"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
9,
|
||||||
|
4
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
9,
|
||||||
|
9
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Undefined operation __add__ between BaseType(name='bool') and BaseType(name='bool')"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
11,
|
||||||
|
0
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
11,
|
||||||
|
12
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Cannot assign BaseType(name='int') to f of type BaseType(name='float')"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
18
tests/cases/checker/03_functions.py
Normal file
18
tests/cases/checker/03_functions.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
def foo(a: int, /, b: float, *, c: str):
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
r1 = foo()
|
||||||
|
r2 = foo(1)
|
||||||
|
r3 = foo(1, 2.0)
|
||||||
|
r4 = foo(1, b=2.0)
|
||||||
|
r5 = foo(1, 2.0, "test")
|
||||||
|
r6 = foo(1, 2.0, b=3.0)
|
||||||
|
r7 = foo(a=1)
|
||||||
|
r8 = foo(g="test")
|
||||||
|
|
||||||
|
r9a = foo(1, 2.0, c="test")
|
||||||
|
r9b = foo(1, b=2.0, c="test")
|
||||||
|
r9c = foo(1, c="test", b=2.0)
|
||||||
|
|
||||||
|
r10 = foo("a", 3, c=False)
|
||||||
270
tests/cases/checker/03_functions.py.ref.json
Normal file
270
tests/cases/checker/03_functions.py.ref.json
Normal file
@@ -0,0 +1,270 @@
|
|||||||
|
{
|
||||||
|
"diagnostics": [
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
5,
|
||||||
|
5
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
5,
|
||||||
|
10
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Missing required positional arguments: 'a' and 'b'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
5,
|
||||||
|
5
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
5,
|
||||||
|
10
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Missing required keyword argument: 'c'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
6,
|
||||||
|
5
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
6,
|
||||||
|
11
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Missing required positional argument: 'b'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
6,
|
||||||
|
5
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
6,
|
||||||
|
11
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Missing required keyword argument: 'c'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
7,
|
||||||
|
5
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
7,
|
||||||
|
16
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Missing required keyword argument: 'c'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
8,
|
||||||
|
5
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
8,
|
||||||
|
18
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Missing required keyword argument: 'c'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
9,
|
||||||
|
17
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
9,
|
||||||
|
23
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Too many positional arguments"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
9,
|
||||||
|
5
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
9,
|
||||||
|
24
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Missing required keyword argument: 'c'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
10,
|
||||||
|
19
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
10,
|
||||||
|
22
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Multiple values for argument 'b'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
10,
|
||||||
|
5
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
10,
|
||||||
|
23
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Missing required keyword argument: 'c'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
11,
|
||||||
|
11
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
11,
|
||||||
|
12
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Unknown keyword argument 'a'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
11,
|
||||||
|
5
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
11,
|
||||||
|
13
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Missing required positional arguments: 'a' and 'b'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
11,
|
||||||
|
5
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
11,
|
||||||
|
13
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Missing required keyword argument: 'c'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
12,
|
||||||
|
11
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
12,
|
||||||
|
17
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Unknown keyword argument 'g'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
12,
|
||||||
|
5
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
12,
|
||||||
|
18
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Missing required positional arguments: 'a' and 'b'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
12,
|
||||||
|
5
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
12,
|
||||||
|
18
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Missing required keyword argument: 'c'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
18,
|
||||||
|
10
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
18,
|
||||||
|
13
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Wrong type for argument 'a', expected BaseType(name='int'), got BaseType(name='str')"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
18,
|
||||||
|
15
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
18,
|
||||||
|
16
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Wrong type for argument 'b', expected BaseType(name='float'), got BaseType(name='int')"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
18,
|
||||||
|
20
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
18,
|
||||||
|
25
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Wrong type for argument 'c', expected BaseType(name='str'), got BaseType(name='bool')"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
14
tests/cases/checker/04_custom_types.midas
Normal file
14
tests/cases/checker/04_custom_types.midas
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
type Meter(float)
|
||||||
|
type Second(float)
|
||||||
|
type MeterPerSecond(float)
|
||||||
|
|
||||||
|
extend Meter {
|
||||||
|
op __add__(Meter) -> Meter
|
||||||
|
op __sub__(Meter) -> Meter
|
||||||
|
op __truediv__(Second) -> MeterPerSecond
|
||||||
|
}
|
||||||
|
|
||||||
|
extend Second {
|
||||||
|
op __add__(Second) -> Second
|
||||||
|
op __sub__(Second) -> Second
|
||||||
|
}
|
||||||
8
tests/cases/checker/04_custom_types.py
Normal file
8
tests/cases/checker/04_custom_types.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# type: ignore
|
||||||
|
# ruff: disable [F821]
|
||||||
|
|
||||||
|
midas.using("04_custom_types.midas")
|
||||||
|
|
||||||
|
distance: Meter = 123.45
|
||||||
|
time: Second = 6.7
|
||||||
|
speed = distance / time
|
||||||
32
tests/cases/checker/04_custom_types.py.ref.json
Normal file
32
tests/cases/checker/04_custom_types.py.ref.json
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
{
|
||||||
|
"diagnostics": [
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
6,
|
||||||
|
0
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
6,
|
||||||
|
24
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Cannot assign BaseType(name='float') to distance of type SimpleType(name='Meter', base=BaseType(name='float'))"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Error",
|
||||||
|
"location": {
|
||||||
|
"start": [
|
||||||
|
7,
|
||||||
|
0
|
||||||
|
],
|
||||||
|
"end": [
|
||||||
|
7,
|
||||||
|
18
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"message": "Cannot assign BaseType(name='float') to time of type SimpleType(name='Second', base=BaseType(name='float'))"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user