pytest-revealtype-injector 0.6.1__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.1"
3
+ __version__ = "0.6.3"
@@ -136,6 +136,25 @@ class NameCollector(NameCollectorBase):
136
136
  return node
137
137
 
138
138
 
139
+ # Mypy can insert extra character into expression so that it
140
+ # becomes invalid and unparsable. 0.9x days there
141
+ # was '*', and now '?' (and '=' for typeddict too).
142
+ # Instead of globally throwing them away (and causing
143
+ # literal string constants to not match), we iteratively
144
+ # remove chars one by one only where parsing error occurs.
145
+ #
146
+ def _strip_unwanted_char(input: str) -> str:
147
+ result = input
148
+ while True:
149
+ try:
150
+ _ = ast.parse(result)
151
+ except SyntaxError as e:
152
+ assert e.offset is not None
153
+ result = result[:e.offset-1] + result[e.offset:]
154
+ else:
155
+ return result
156
+
157
+
139
158
  class MypyAdapter(TypeCheckerAdapter):
140
159
  id = "mypy"
141
160
  _executable = "" # unused, calls mypy.api.run() here
@@ -212,12 +231,7 @@ class MypyAdapter(TypeCheckerAdapter):
212
231
  )
213
232
  if (m := self._type_mesg_re.fullmatch(diag["message"])) is None:
214
233
  continue
215
- # Mypy can insert extra character into expression so that it
216
- # becomes invalid and unparsable. 0.9x days there
217
- # was '*', and now '?' (and '=' for typeddict too).
218
- # Try stripping those character and pray we get something
219
- # usable for evaluation
220
- expression = m["type"].translate({ord(c): None for c in "*?="})
234
+ expression = _strip_unwanted_char(m["type"])
221
235
  try:
222
236
  # Unlike pyright, mypy output doesn't contain variable name
223
237
  self.typechecker_result[pos] = VarType(None, ForwardRef(expression))
@@ -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.1
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=wu2hLHVkWsPCsABHczD0U9APBuw8UCBvOxv6V1x5Xyg,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
@@ -7,11 +7,11 @@ pytest_revealtype_injector/plugin.py,sha256=fkI6yF0dFVba0jEikIrsRp1NUQd2ohWLq4x2
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
- pytest_revealtype_injector/adapter/mypy_.py,sha256=d69nGsdhkuKMPjqWMLj-KyY8HI9k6R7VaVUSw3P-IZg,9000
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.1.dist-info/METADATA,sha256=-hYQiHx1mbDeL-su0LQYwJv2L3zTEELwWs9da_cKVWA,6729
13
- pytest_revealtype_injector-0.6.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
14
- pytest_revealtype_injector-0.6.1.dist-info/entry_points.txt,sha256=UfOm7y3WQnOoGV1mgTMb42MI6iBRPIl88FJiAOnt6SY,74
15
- pytest_revealtype_injector-0.6.1.dist-info/licenses/COPYING,sha256=LSYUX8PcSMvHCkhM5oi07eOrSLV89qdEJ-FVZmbcpNE,355
16
- pytest_revealtype_injector-0.6.1.dist-info/licenses/COPYING.mit,sha256=IzYEFDIOECyuupg_B3O9FvgjnU9i4JtambpbleoYHdQ,1060
17
- pytest_revealtype_injector-0.6.1.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