pytest-revealtype-injector 0.6.2__py3-none-any.whl → 0.6.3__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.
@@ -1,3 +1,3 @@
1
1
  """Pytest plugin for replacing reveal_type() calls inside test functions with static and runtime type checking result comparison, for confirming type annotation validity.""" # noqa: E501
2
2
 
3
- __version__ = "0.6.2"
3
+ __version__ = "0.6.3"
@@ -2,6 +2,7 @@ from __future__ import annotations
2
2
 
3
3
  import functools
4
4
  import inspect
5
+ from collections.abc import Iterator
5
6
  from typing import cast
6
7
 
7
8
  import pytest
@@ -14,7 +15,8 @@ _logger = log.get_logger()
14
15
  adapter_stash_key: pytest.StashKey[set[TypeCheckerAdapter]]
15
16
 
16
17
 
17
- def pytest_pyfunc_call(pyfuncitem: pytest.Function) -> None:
18
+ @pytest.hookimpl(wrapper=True)
19
+ def pytest_pyfunc_call(pyfuncitem: pytest.Function) -> Iterator[None]:
18
20
  assert pyfuncitem.module is not None
19
21
  adp_stash = pyfuncitem.config.stash[adapter_stash_key]
20
22
 
@@ -23,7 +25,7 @@ def pytest_pyfunc_call(pyfuncitem: pytest.Function) -> None:
23
25
  if mark:
24
26
  disabled_adapters = {a.id for a in adp_stash if a.id in mark.args}
25
27
  for a in disabled_adapters:
26
- _logger.info(f"{a} adapter disabled by 'notypechecker' marker")
28
+ _logger.info(f"{a} adapter disabled by 'notypechecker' marker in {pyfuncitem.name} test")
27
29
  adapters = {a for a in adp_stash if a.id not in disabled_adapters}
28
30
  else:
29
31
  adapters = {a for a in adp_stash}
@@ -31,39 +33,43 @@ def pytest_pyfunc_call(pyfuncitem: pytest.Function) -> None:
31
33
  if not adapters:
32
34
  pytest.fail("All type checkers have been disabled.")
33
35
 
34
- # Replace reveal_type() with our own function
35
- for name in dir(pyfuncitem.module):
36
- if name.startswith("__") or name.startswith("@py"):
37
- continue
38
-
39
- item = getattr(pyfuncitem.module, name)
40
- if inspect.isfunction(item):
41
- if item.__name__ != "reveal_type" or item.__module__ not in {
42
- "typing",
43
- "typing_extensions",
44
- }:
36
+ # Monkeypatch reveal_type() with our own function, to guarantee
37
+ # each test func can receive different adapters
38
+ with pytest.MonkeyPatch.context() as mp:
39
+ for name in dir(pyfuncitem.module):
40
+ if name.startswith("__") or name.startswith("@py"):
45
41
  continue
46
- injected = functools.partial(
47
- revealtype_injector,
48
- adapters=adapters,
49
- rt_funcname=name,
50
- )
51
- setattr(pyfuncitem.module, name, injected)
52
- _logger.info(f"Replaced {name}() from global import with {injected}")
53
- break
54
42
 
55
- elif inspect.ismodule(item):
56
- if item.__name__ not in {"typing", "typing_extensions"}:
57
- continue
58
- assert hasattr(item, "reveal_type")
59
- injected = functools.partial(
60
- revealtype_injector,
61
- adapters=adapters,
62
- rt_funcname=f"{name}.reveal_type",
63
- )
64
- setattr(item, "reveal_type", injected)
65
- _logger.info(f"Replaced {name}.reveal_type() with {injected}")
66
- break
43
+ item = getattr(pyfuncitem.module, name)
44
+ if inspect.isfunction(item):
45
+ if item.__name__ != "reveal_type" or item.__module__ not in {
46
+ "typing",
47
+ "typing_extensions",
48
+ }:
49
+ continue
50
+ injected = functools.partial(
51
+ revealtype_injector,
52
+ adapters=adapters,
53
+ rt_funcname=name,
54
+ )
55
+ mp.setattr(pyfuncitem.module, name, injected)
56
+ _logger.info(f"Replaced {name}() from global import with {injected} in {pyfuncitem.name} test")
57
+ break
58
+
59
+ elif inspect.ismodule(item):
60
+ if item.__name__ not in {"typing", "typing_extensions"}:
61
+ continue
62
+ assert hasattr(item, "reveal_type")
63
+ injected = functools.partial(
64
+ revealtype_injector,
65
+ adapters=adapters,
66
+ rt_funcname=f"{name}.reveal_type",
67
+ )
68
+ mp.setattr(item, "reveal_type", injected)
69
+ _logger.info(f"Replaced {name}.reveal_type() with {injected} in {pyfuncitem.name} test")
70
+ break
71
+
72
+ return cast(None, (yield)) # type: ignore[redundant-cast]
67
73
 
68
74
 
69
75
  def pytest_collection_finish(session: pytest.Session) -> None:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pytest-revealtype-injector
3
- Version: 0.6.2
3
+ Version: 0.6.3
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>
@@ -22,8 +22,8 @@ Classifier: Programming Language :: Python :: 3.14
22
22
  Classifier: Topic :: Software Development :: Testing
23
23
  Classifier: Typing :: Typed
24
24
  Requires-Python: >=3.10
25
- Requires-Dist: pytest<9,>=7.0
26
- Requires-Dist: schema==0.7.7
25
+ Requires-Dist: pytest>=7.0
26
+ Requires-Dist: schema>=0.7.8
27
27
  Requires-Dist: typeguard>=4.3
28
28
  Requires-Dist: typing-extensions>=4.0; python_version < '3.11'
29
29
  Description-Content-Type: text/markdown
@@ -1,5 +1,5 @@
1
- pytest_revealtype_injector/__init__.py,sha256=aiBTdl1GqqEAFu1nrAFIExIe_G2uGgrFYltHSNLKUXo,211
2
- pytest_revealtype_injector/hooks.py,sha256=_yJD6htagRJGElrSucFbDxSHT_osdoD9Lx3P4N5sjhM,4465
1
+ pytest_revealtype_injector/__init__.py,sha256=AfzNNcCIxo6g21Ew_p2Dlxv02-mEZ2B_fZ8moShs98Q,211
2
+ pytest_revealtype_injector/hooks.py,sha256=YAUsgVVd5N9FDx0gIpB7bqRYO7hJq7yli4u9nasQqzk,4930
3
3
  pytest_revealtype_injector/log.py,sha256=Ptd3yp1H1GlUum6BAwHc9cdyeGmaY8XYf0jp6qJmG4M,418
4
4
  pytest_revealtype_injector/main.py,sha256=3w-_CoZHTrBSC-5iX0cYMsW5rzdmmsqIwcw39x4E0EQ,5656
5
5
  pytest_revealtype_injector/models.py,sha256=K8b5o0OXqc58S64K6LhJ5W_fyGqtYFAsb6fmWvs3lGA,5215
@@ -9,9 +9,9 @@ pytest_revealtype_injector/adapter/__init__.py,sha256=FRVB1eUrXaMzdQG0wRdIEKhx2d
9
9
  pytest_revealtype_injector/adapter/basedpyright_.py,sha256=8LX7GmJmg4OZ3LKO5WoQ7Ocub6Lxi3HTStIorMApzUA,466
10
10
  pytest_revealtype_injector/adapter/mypy_.py,sha256=A8Uw21qm-BRMED1HiQ13McMn8jQCVTTZDnNM2inzkBE,9314
11
11
  pytest_revealtype_injector/adapter/pyright_.py,sha256=j9121YxGeulZpRRJAO0dLFpaoTjE6t343-qS-O952QU,5122
12
- pytest_revealtype_injector-0.6.2.dist-info/METADATA,sha256=CgwPmLecZ0bP_Zji97wP8lxccjtPPU3o6Y_xsoBORg0,6729
13
- pytest_revealtype_injector-0.6.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
14
- pytest_revealtype_injector-0.6.2.dist-info/entry_points.txt,sha256=UfOm7y3WQnOoGV1mgTMb42MI6iBRPIl88FJiAOnt6SY,74
15
- pytest_revealtype_injector-0.6.2.dist-info/licenses/COPYING,sha256=LSYUX8PcSMvHCkhM5oi07eOrSLV89qdEJ-FVZmbcpNE,355
16
- pytest_revealtype_injector-0.6.2.dist-info/licenses/COPYING.mit,sha256=IzYEFDIOECyuupg_B3O9FvgjnU9i4JtambpbleoYHdQ,1060
17
- pytest_revealtype_injector-0.6.2.dist-info/RECORD,,
12
+ pytest_revealtype_injector-0.6.3.dist-info/METADATA,sha256=rf4X6w9S1G0nk14Q4rmHCdYtW_93znizUDdvEF9aDjU,6726
13
+ pytest_revealtype_injector-0.6.3.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
14
+ pytest_revealtype_injector-0.6.3.dist-info/entry_points.txt,sha256=UfOm7y3WQnOoGV1mgTMb42MI6iBRPIl88FJiAOnt6SY,74
15
+ pytest_revealtype_injector-0.6.3.dist-info/licenses/COPYING,sha256=LSYUX8PcSMvHCkhM5oi07eOrSLV89qdEJ-FVZmbcpNE,355
16
+ pytest_revealtype_injector-0.6.3.dist-info/licenses/COPYING.mit,sha256=IzYEFDIOECyuupg_B3O9FvgjnU9i4JtambpbleoYHdQ,1060
17
+ pytest_revealtype_injector-0.6.3.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.27.0
2
+ Generator: hatchling 1.28.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any