pytest-revealtype-injector 0.5.0__py3-none-any.whl → 0.6.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pytest_revealtype_injector/__init__.py +1 -1
- pytest_revealtype_injector/adapter/pyright_.py +1 -0
- pytest_revealtype_injector/hooks.py +28 -1
- pytest_revealtype_injector/models.py +1 -1
- {pytest_revealtype_injector-0.5.0.dist-info → pytest_revealtype_injector-0.6.0.dist-info}/METADATA +51 -9
- {pytest_revealtype_injector-0.5.0.dist-info → pytest_revealtype_injector-0.6.0.dist-info}/RECORD +10 -10
- {pytest_revealtype_injector-0.5.0.dist-info → pytest_revealtype_injector-0.6.0.dist-info}/WHEEL +0 -0
- {pytest_revealtype_injector-0.5.0.dist-info → pytest_revealtype_injector-0.6.0.dist-info}/entry_points.txt +0 -0
- {pytest_revealtype_injector-0.5.0.dist-info → pytest_revealtype_injector-0.6.0.dist-info}/licenses/COPYING +0 -0
- {pytest_revealtype_injector-0.5.0.dist-info → pytest_revealtype_injector-0.6.0.dist-info}/licenses/COPYING.mit +0 -0
|
@@ -16,8 +16,22 @@ adapter_stash_key: pytest.StashKey[set[TypeCheckerAdapter]]
|
|
|
16
16
|
|
|
17
17
|
def pytest_pyfunc_call(pyfuncitem: pytest.Function) -> None:
|
|
18
18
|
assert pyfuncitem.module is not None
|
|
19
|
-
|
|
19
|
+
adp_stash = pyfuncitem.config.stash[adapter_stash_key]
|
|
20
20
|
|
|
21
|
+
# Disable type checker based on marker
|
|
22
|
+
mark = pyfuncitem.get_closest_marker("notypechecker")
|
|
23
|
+
if mark:
|
|
24
|
+
disabled_adapters = {a.id for a in adp_stash if a.id in mark.args}
|
|
25
|
+
for a in disabled_adapters:
|
|
26
|
+
_logger.info(f"{a} adapter disabled by 'notypechecker' marker")
|
|
27
|
+
adapters = {a for a in adp_stash if a.id not in disabled_adapters}
|
|
28
|
+
else:
|
|
29
|
+
adapters = {a for a in adp_stash}
|
|
30
|
+
|
|
31
|
+
if not adapters:
|
|
32
|
+
pytest.fail("All type checkers have been disabled.")
|
|
33
|
+
|
|
34
|
+
# Replace reveal_type() with our own function
|
|
21
35
|
for name in dir(pyfuncitem.module):
|
|
22
36
|
if name.startswith("__") or name.startswith("@py"):
|
|
23
37
|
continue
|
|
@@ -54,6 +68,8 @@ def pytest_pyfunc_call(pyfuncitem: pytest.Function) -> None:
|
|
|
54
68
|
|
|
55
69
|
def pytest_collection_finish(session: pytest.Session) -> None:
|
|
56
70
|
files = {i.path for i in session.items}
|
|
71
|
+
if not files:
|
|
72
|
+
return
|
|
57
73
|
for adp in session.config.stash[adapter_stash_key]:
|
|
58
74
|
try:
|
|
59
75
|
adp.run_typechecker_on(files)
|
|
@@ -86,13 +102,16 @@ def pytest_addoption(parser: pytest.Parser) -> None:
|
|
|
86
102
|
|
|
87
103
|
|
|
88
104
|
def pytest_configure(config: pytest.Config) -> None:
|
|
105
|
+
# Globally disable adapters using command line options
|
|
89
106
|
global adapter_stash_key
|
|
90
107
|
adapter_stash_key = pytest.StashKey[set[TypeCheckerAdapter]]()
|
|
91
108
|
config.stash[adapter_stash_key] = set()
|
|
92
109
|
verbosity = config.get_verbosity(config.VERBOSITY_TEST_CASES)
|
|
93
110
|
log.set_verbosity(verbosity)
|
|
94
111
|
to_be_disabled = cast(list[str], config.getoption("revealtype_disable_adapter"))
|
|
112
|
+
all_ids: list[str] = []
|
|
95
113
|
for klass in adapter.get_adapter_classes():
|
|
114
|
+
all_ids.append(klass.id)
|
|
96
115
|
if klass.id in to_be_disabled:
|
|
97
116
|
_logger.info(f"({klass.id}) adapter disabled with command line option")
|
|
98
117
|
continue
|
|
@@ -100,3 +119,11 @@ def pytest_configure(config: pytest.Config) -> None:
|
|
|
100
119
|
adp.set_config_file(config)
|
|
101
120
|
adp.log_verbosity = verbosity
|
|
102
121
|
config.stash[adapter_stash_key].add(adp)
|
|
122
|
+
|
|
123
|
+
# Marker to disable adapters on demand
|
|
124
|
+
config.addinivalue_line(
|
|
125
|
+
"markers",
|
|
126
|
+
"notypechecker(name, ...): mark reveal_type() test to disable "
|
|
127
|
+
"specified type checker(s). Following type checkers are supported: "
|
|
128
|
+
+ ", ".join(all_ids),
|
|
129
|
+
)
|
{pytest_revealtype_injector-0.5.0.dist-info → pytest_revealtype_injector-0.6.0.dist-info}/METADATA
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pytest-revealtype-injector
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: Pytest plugin for replacing reveal_type() calls inside test functions with static and runtime type checking result comparison, for confirming type annotation validity.
|
|
5
5
|
Project-URL: homepage, https://github.com/abelcheung/pytest-revealtype-injector
|
|
6
6
|
Author-email: Abel Cheung <abelcheung@gmail.com>
|
|
@@ -18,12 +18,10 @@ Classifier: Programming Language :: Python :: 3.10
|
|
|
18
18
|
Classifier: Programming Language :: Python :: 3.11
|
|
19
19
|
Classifier: Programming Language :: Python :: 3.12
|
|
20
20
|
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
22
|
Classifier: Topic :: Software Development :: Testing
|
|
22
23
|
Classifier: Typing :: Typed
|
|
23
24
|
Requires-Python: >=3.10
|
|
24
|
-
Requires-Dist: basedpyright>=1.0
|
|
25
|
-
Requires-Dist: mypy>=1.11.2
|
|
26
|
-
Requires-Dist: pyright>=1.1
|
|
27
25
|
Requires-Dist: pytest<9,>=7.0
|
|
28
26
|
Requires-Dist: schema==0.7.7
|
|
29
27
|
Requires-Dist: typeguard>=4.3
|
|
@@ -37,7 +35,7 @@ Description-Content-Type: text/markdown
|
|
|
37
35
|
|
|
38
36
|
`pytest-revealtype-injector` is a `pytest` plugin for replacing [`reveal_type()`](https://docs.python.org/3/library/typing.html#typing.reveal_type) calls inside test functions as something more sophisticated. It does the following tasks in parallel:
|
|
39
37
|
|
|
40
|
-
- Launch external static type checkers (`pyright` and `mypy`) and store `reveal_type` results.
|
|
38
|
+
- Launch external static type checkers (`basesdpyright`, `pyright` and `mypy`) and store `reveal_type` results.
|
|
41
39
|
- Use [`typeguard`](https://github.com/agronholm/typeguard) to verify the aforementioned static type checker result _really_ matches runtime code result.
|
|
42
40
|
|
|
43
41
|
## Usage
|
|
@@ -54,7 +52,17 @@ For using `reveal_type()` inside tests, there is no boiler plate code involved.
|
|
|
54
52
|
from typing import reveal_type
|
|
55
53
|
```
|
|
56
54
|
|
|
57
|
-
|
|
55
|
+
If you care about compatibility with older pythons, use:
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
import sys
|
|
59
|
+
if sys.version >= (3, 11):
|
|
60
|
+
from typing import reveal_type
|
|
61
|
+
else:
|
|
62
|
+
from typing_extensions import reveal_type
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Just importing `typing` (or `typing_extensions`) module is fine too:
|
|
58
66
|
|
|
59
67
|
```python
|
|
60
68
|
import typing
|
|
@@ -64,7 +72,7 @@ def test_something():
|
|
|
64
72
|
typing.reveal_type(x) # typeguard fails here
|
|
65
73
|
```
|
|
66
74
|
|
|
67
|
-
Since this plugin scans for `reveal_type()` for replacement under carpet, even `import ... as ...` syntax works
|
|
75
|
+
Since this plugin scans for `reveal_type()` for replacement under carpet, even `import ... as ...` syntax works:
|
|
68
76
|
|
|
69
77
|
```python
|
|
70
78
|
import typing as typ # or...
|
|
@@ -73,7 +81,7 @@ from typing import reveal_type as rt
|
|
|
73
81
|
|
|
74
82
|
### Limitations
|
|
75
83
|
|
|
76
|
-
But there are
|
|
84
|
+
But there are 3 caveats.
|
|
77
85
|
|
|
78
86
|
1. This plugin only searches for global import in test files, so local import inside test function doesn't work. That means following code doesn't utilize this plugin at all:
|
|
79
87
|
|
|
@@ -84,7 +92,41 @@ def test_something():
|
|
|
84
92
|
reveal_type(x) # calls vanilla reveal_type()
|
|
85
93
|
```
|
|
86
94
|
|
|
87
|
-
2. `reveal_type()` calls have to stay
|
|
95
|
+
2. `reveal_type()` calls have to stay within a single line, although you can use `reveal_type` result in assertion or other purpose:
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
x = "1"
|
|
99
|
+
assert reveal_type(str(int(x))) == x
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
3. This plugin does not enlist any type checker as dependency, because any of them can be optionally disabled with pytest marker (see below) or command line option. It is up to application or library authors to include suitable type checker(s) as dependency themselves.
|
|
103
|
+
|
|
104
|
+
## Disable type checker with marker
|
|
105
|
+
|
|
106
|
+
Using [pytest marker](https://docs.pytest.org/en/stable/example/markers.html), it is possible to disable usage of certain type checker for specific test. All 3 types of markers (function, class and module level) are supported.
|
|
107
|
+
|
|
108
|
+
Function level:
|
|
109
|
+
```python
|
|
110
|
+
@pytest.mark.notypechecker("mypy")
|
|
111
|
+
def test_something(self) -> None:
|
|
112
|
+
x = 1
|
|
113
|
+
reveal_type(x)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Class level:
|
|
117
|
+
```python
|
|
118
|
+
@pytest.mark.notypechecker("pyright")
|
|
119
|
+
class TestSomething:
|
|
120
|
+
def test_foo(self) -> None:
|
|
121
|
+
...
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Module level:
|
|
125
|
+
```python
|
|
126
|
+
pytestmark = pytest.mark.notypechecker("basedpyright", "pyright")
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Note that disabling all type checkers is disallowed, and such tests would be treated as `pytest.fail`. Disable the `reveal_type()` call instead.
|
|
88
130
|
|
|
89
131
|
## Logging
|
|
90
132
|
|
{pytest_revealtype_injector-0.5.0.dist-info → pytest_revealtype_injector-0.6.0.dist-info}/RECORD
RENAMED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
pytest_revealtype_injector/__init__.py,sha256=
|
|
2
|
-
pytest_revealtype_injector/hooks.py,sha256=
|
|
1
|
+
pytest_revealtype_injector/__init__.py,sha256=AQ0K8mPMknrnsaYKFAvP9Zb1RZ-XZatYU16hN8bY9p0,211
|
|
2
|
+
pytest_revealtype_injector/hooks.py,sha256=_yJD6htagRJGElrSucFbDxSHT_osdoD9Lx3P4N5sjhM,4465
|
|
3
3
|
pytest_revealtype_injector/log.py,sha256=Ptd3yp1H1GlUum6BAwHc9cdyeGmaY8XYf0jp6qJmG4M,418
|
|
4
4
|
pytest_revealtype_injector/main.py,sha256=3w-_CoZHTrBSC-5iX0cYMsW5rzdmmsqIwcw39x4E0EQ,5656
|
|
5
|
-
pytest_revealtype_injector/models.py,sha256=
|
|
5
|
+
pytest_revealtype_injector/models.py,sha256=K8b5o0OXqc58S64K6LhJ5W_fyGqtYFAsb6fmWvs3lGA,5215
|
|
6
6
|
pytest_revealtype_injector/plugin.py,sha256=fkI6yF0dFVba0jEikIrsRp1NUQd2ohWLq4x2lSvFyH0,211
|
|
7
7
|
pytest_revealtype_injector/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
8
|
pytest_revealtype_injector/adapter/__init__.py,sha256=FRVB1eUrXaMzdQG0wRdIEKhx2dzGdyJcHDBb3eTTeOY,602
|
|
9
9
|
pytest_revealtype_injector/adapter/basedpyright_.py,sha256=8LX7GmJmg4OZ3LKO5WoQ7Ocub6Lxi3HTStIorMApzUA,466
|
|
10
10
|
pytest_revealtype_injector/adapter/mypy_.py,sha256=lMaxQ4uTvSIG6cocOW7lGbPxLwQSv2p5cn4p2o6YOfg,8830
|
|
11
|
-
pytest_revealtype_injector/adapter/pyright_.py,sha256=
|
|
12
|
-
pytest_revealtype_injector-0.
|
|
13
|
-
pytest_revealtype_injector-0.
|
|
14
|
-
pytest_revealtype_injector-0.
|
|
15
|
-
pytest_revealtype_injector-0.
|
|
16
|
-
pytest_revealtype_injector-0.
|
|
17
|
-
pytest_revealtype_injector-0.
|
|
11
|
+
pytest_revealtype_injector/adapter/pyright_.py,sha256=j9121YxGeulZpRRJAO0dLFpaoTjE6t343-qS-O952QU,5122
|
|
12
|
+
pytest_revealtype_injector-0.6.0.dist-info/METADATA,sha256=49ryc0Nz66GiXOZePBmG5vOz74l0x2HgeWbnl_CLrh4,6729
|
|
13
|
+
pytest_revealtype_injector-0.6.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
14
|
+
pytest_revealtype_injector-0.6.0.dist-info/entry_points.txt,sha256=UfOm7y3WQnOoGV1mgTMb42MI6iBRPIl88FJiAOnt6SY,74
|
|
15
|
+
pytest_revealtype_injector-0.6.0.dist-info/licenses/COPYING,sha256=LSYUX8PcSMvHCkhM5oi07eOrSLV89qdEJ-FVZmbcpNE,355
|
|
16
|
+
pytest_revealtype_injector-0.6.0.dist-info/licenses/COPYING.mit,sha256=IzYEFDIOECyuupg_B3O9FvgjnU9i4JtambpbleoYHdQ,1060
|
|
17
|
+
pytest_revealtype_injector-0.6.0.dist-info/RECORD,,
|
{pytest_revealtype_injector-0.5.0.dist-info → pytest_revealtype_injector-0.6.0.dist-info}/WHEEL
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|