/
test_pyright.py
222 lines (184 loc) · 5.98 KB
/
test_pyright.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
from __future__ import annotations
import json
import subprocess
import textwrap
from pathlib import Path
from typing import Any
import pytest
from hypothesistooling.projects.hypothesispython import HYPOTHESIS_PYTHON, PYTHON_SRC
from hypothesistooling.scripts import pip_tool, tool_path
PYTHON_VERSIONS = ["3.7", "3.8", "3.9", "3.10", "3.11"]
@pytest.mark.skip(
reason="Hypothesis type-annotates the public API as a convenience for users, "
"but strict checks for our internals would be a net drag on productivity."
)
def test_pyright_passes_on_hypothesis():
pip_tool("pyright", "--project", HYPOTHESIS_PYTHON)
@pytest.mark.parametrize("python_version", PYTHON_VERSIONS)
def test_pyright_passes_on_basic_test(tmp_path: Path, python_version: str):
file = tmp_path / "test.py"
file.write_text(
textwrap.dedent(
"""
import hypothesis
import hypothesis.strategies as st
@hypothesis.given(x=st.text())
def test_foo(x: str):
assert x == x
from hypothesis import given
from hypothesis.strategies import text
@given(x=text())
def test_bar(x: str):
assert x == x
"""
)
)
_write_config(
tmp_path, {"typeCheckingMode": "strict", "pythonVersion": python_version}
)
assert _get_pyright_errors(file) == []
@pytest.mark.parametrize("python_version", PYTHON_VERSIONS)
def test_given_only_allows_strategies(tmp_path: Path, python_version: str):
file = tmp_path / "test.py"
file.write_text(
textwrap.dedent(
"""
from hypothesis import given
@given(1)
def f():
pass
"""
)
)
_write_config(
tmp_path, {"typeCheckingMode": "strict", "pythonVersion": python_version}
)
assert (
sum(
e["message"].startswith(
'Argument of type "Literal[1]" cannot be assigned to parameter "_given_arguments"'
)
for e in _get_pyright_errors(file)
)
== 1
)
def test_pyright_issue_3296(tmp_path: Path):
file = tmp_path / "test.py"
file.write_text(
textwrap.dedent(
"""
from hypothesis.strategies import lists, integers
lists(integers()).map(sorted)
"""
)
)
_write_config(tmp_path, {"typeCheckingMode": "strict"})
assert _get_pyright_errors(file) == []
def test_pyright_raises_for_mixed_pos_kwargs_in_given(tmp_path: Path):
file = tmp_path / "test.py"
file.write_text(
textwrap.dedent(
"""
from hypothesis import given
from hypothesis.strategies import text
@given(text(), x=text())
def test_bar(x: str):
pass
"""
)
)
_write_config(tmp_path, {"typeCheckingMode": "strict"})
assert (
sum(
e["message"].startswith(
'No overloads for "given" match the provided arguments'
)
for e in _get_pyright_errors(file)
)
== 1
)
def test_pyright_issue_3348(tmp_path: Path):
file = tmp_path / "test.py"
file.write_text(
textwrap.dedent(
"""
import hypothesis.strategies as st
st.tuples(st.integers(), st.integers())
st.one_of(st.integers(), st.integers())
st.one_of([st.integers(), st.floats()]) # sequence of strats should be OK
st.sampled_from([1, 2])
"""
)
)
_write_config(tmp_path, {"typeCheckingMode": "strict"})
assert _get_pyright_errors(file) == []
def test_pyright_tuples_pos_args_only(tmp_path: Path):
file = tmp_path / "test.py"
file.write_text(
textwrap.dedent(
"""
import hypothesis.strategies as st
st.tuples(a1=st.integers())
st.tuples(a1=st.integers(), a2=st.integers())
"""
)
)
_write_config(tmp_path, {"typeCheckingMode": "strict"})
assert (
sum(
e["message"].startswith(
'No overloads for "tuples" match the provided arguments'
)
for e in _get_pyright_errors(file)
)
== 2
)
def test_pyright_one_of_pos_args_only(tmp_path: Path):
file = tmp_path / "test.py"
file.write_text(
textwrap.dedent(
"""
import hypothesis.strategies as st
st.one_of(a1=st.integers())
st.one_of(a1=st.integers(), a2=st.integers())
"""
)
)
_write_config(tmp_path, {"typeCheckingMode": "strict"})
assert (
sum(
e["message"].startswith(
'No overloads for "one_of" match the provided arguments'
)
for e in _get_pyright_errors(file)
)
== 2
)
# ---------- Helpers for running pyright ---------- #
def _get_pyright_output(file: Path) -> dict[str, Any]:
proc = subprocess.run(
[tool_path("pyright"), "--outputjson"],
cwd=file.parent,
encoding="utf-8",
text=True,
capture_output=True,
)
try:
return json.loads(proc.stdout)
except Exception:
print(proc.stdout)
raise
def _get_pyright_errors(file: Path) -> list[dict[str, Any]]:
return _get_pyright_output(file)["generalDiagnostics"]
def _write_config(config_dir: Path, data: dict[str, Any] | None = None):
config = {"extraPaths": [PYTHON_SRC], **(data or {})}
(config_dir / "pyrightconfig.json").write_text(json.dumps(config))