11"""Test the numpydoc validate pre-commit hook."""
22
33import inspect
4+ import re
45from pathlib import Path
56
67import pytest
@@ -25,42 +26,42 @@ def test_validate_hook(example_module, config, capsys):
2526 """Test that a file is correctly processed in the absence of config files."""
2627
2728 expected = inspect .cleandoc (
28- """
29- numpydoc/tests/hooks/ example_module.py :4: ES01 No extended summary found
29+ f """
30+ { example_module !s } :4: ES01 No extended summary found
3031
31- numpydoc/tests/hooks/ example_module.py :4: PR01 Parameters {'name'} not documented
32+ { example_module !s } :4: PR01 Parameters {{ 'name'} } not documented
3233
33- numpydoc/tests/hooks/ example_module.py :4: SA01 See Also section not found
34+ { example_module !s } :4: SA01 See Also section not found
3435
35- numpydoc/tests/hooks/ example_module.py :4: EX01 No examples section found
36+ { example_module !s } :4: EX01 No examples section found
3637
37- numpydoc/tests/hooks/ example_module.py :8: ES01 No extended summary found
38+ { example_module !s } :8: ES01 No extended summary found
3839
39- numpydoc/tests/hooks/ example_module.py :8: SA01 See Also section not found
40+ { example_module !s } :8: SA01 See Also section not found
4041
41- numpydoc/tests/hooks/ example_module.py :8: EX01 No examples section found
42+ { example_module !s } :8: EX01 No examples section found
4243
43- numpydoc/tests/hooks/ example_module.py :11: GL08 The object does not have a docstring
44+ { example_module !s } :11: GL08 The object does not have a docstring
4445
45- numpydoc/tests/hooks/ example_module.py :17: ES01 No extended summary found
46+ { example_module !s } :17: ES01 No extended summary found
4647
47- numpydoc/tests/hooks/ example_module.py :17: PR01 Parameters {'**kwargs'} not documented
48+ { example_module !s } :17: PR01 Parameters {{ '**kwargs'} } not documented
4849
49- numpydoc/tests/hooks/ example_module.py :17: PR07 Parameter "*args" has no description
50+ { example_module !s } :17: PR07 Parameter "*args" has no description
5051
51- numpydoc/tests/hooks/ example_module.py :17: SA01 See Also section not found
52+ { example_module !s } :17: SA01 See Also section not found
5253
53- numpydoc/tests/hooks/ example_module.py :17: EX01 No examples section found
54+ { example_module !s } :17: EX01 No examples section found
5455
55- numpydoc/tests/hooks/ example_module.py :26: SS05 Summary must start with infinitive verb, not third person (e.g. use "Generate" instead of "Generates")
56+ { example_module !s } :26: SS05 Summary must start with infinitive verb, not third person (e.g. use "Generate" instead of "Generates")
5657
57- numpydoc/tests/hooks/ example_module.py :26: ES01 No extended summary found
58+ { example_module !s } :26: ES01 No extended summary found
5859
59- numpydoc/tests/hooks/ example_module.py :26: SA01 See Also section not found
60+ { example_module !s } :26: SA01 See Also section not found
6061
61- numpydoc/tests/hooks/ example_module.py :26: EX01 No examples section found
62+ { example_module !s } :26: EX01 No examples section found
6263
63- numpydoc/tests/hooks/ example_module.py :30: GL08 The object does not have a docstring
64+ { example_module !s } :30: GL08 The object does not have a docstring
6465 """
6566 )
6667
@@ -76,18 +77,18 @@ def test_validate_hook_with_ignore(example_module, capsys):
7677 """
7778
7879 expected = inspect .cleandoc (
79- """
80- numpydoc/tests/hooks/ example_module.py :4: PR01 Parameters {'name'} not documented
80+ f """
81+ { example_module !s } :4: PR01 Parameters {{ 'name'} } not documented
8182
82- numpydoc/tests/hooks/ example_module.py :11: GL08 The object does not have a docstring
83+ { example_module !s } :11: GL08 The object does not have a docstring
8384
84- numpydoc/tests/hooks/ example_module.py :17: PR01 Parameters {'**kwargs'} not documented
85+ { example_module !s } :17: PR01 Parameters {{ '**kwargs'} } not documented
8586
86- numpydoc/tests/hooks/ example_module.py :17: PR07 Parameter "*args" has no description
87+ { example_module !s } :17: PR07 Parameter "*args" has no description
8788
88- numpydoc/tests/hooks/ example_module.py :26: SS05 Summary must start with infinitive verb, not third person (e.g. use "Generate" instead of "Generates")
89+ { example_module !s } :26: SS05 Summary must start with infinitive verb, not third person (e.g. use "Generate" instead of "Generates")
8990
90- numpydoc/tests/hooks/ example_module.py :30: GL08 The object does not have a docstring
91+ { example_module !s } :30: GL08 The object does not have a docstring
9192 """
9293 )
9394
@@ -123,14 +124,14 @@ def test_validate_hook_with_toml_config(example_module, tmp_path, capsys):
123124 )
124125
125126 expected = inspect .cleandoc (
126- """
127- numpydoc/tests/hooks/ example_module.py :4: PR01 Parameters {'name'} not documented
127+ f """
128+ { example_module !s } :4: PR01 Parameters {{ 'name'} } not documented
128129
129- numpydoc/tests/hooks/ example_module.py :17: PR01 Parameters {'**kwargs'} not documented
130+ { example_module !s } :17: PR01 Parameters {{ '**kwargs'} } not documented
130131
131- numpydoc/tests/hooks/ example_module.py :17: PR07 Parameter "*args" has no description
132+ { example_module !s } :17: PR07 Parameter "*args" has no description
132133
133- numpydoc/tests/hooks/ example_module.py :30: GL08 The object does not have a docstring
134+ { example_module !s } :30: GL08 The object does not have a docstring
134135 """
135136 )
136137
@@ -158,14 +159,14 @@ def test_validate_hook_with_setup_cfg(example_module, tmp_path, capsys):
158159 )
159160
160161 expected = inspect .cleandoc (
161- """
162- numpydoc/tests/hooks/ example_module.py :4: PR01 Parameters {'name'} not documented
162+ f """
163+ { example_module !s } :4: PR01 Parameters {{ 'name'} } not documented
163164
164- numpydoc/tests/hooks/ example_module.py :17: PR01 Parameters {'**kwargs'} not documented
165+ { example_module !s } :17: PR01 Parameters {{ '**kwargs'} } not documented
165166
166- numpydoc/tests/hooks/ example_module.py :17: PR07 Parameter "*args" has no description
167+ { example_module !s } :17: PR07 Parameter "*args" has no description
167168
168- numpydoc/tests/hooks/ example_module.py :30: GL08 The object does not have a docstring
169+ { example_module !s } :30: GL08 The object does not have a docstring
169170 """
170171 )
171172
@@ -203,10 +204,10 @@ def test_validate_hook_exclude_option_pyproject(example_module, tmp_path, capsys
203204 )
204205
205206 expected = inspect .cleandoc (
206- """
207- numpydoc/tests/hooks/ example_module.py :4: PR01 Parameters {'name'} not documented
207+ f """
208+ { example_module !s } :4: PR01 Parameters {{ 'name'} } not documented
208209
209- numpydoc/tests/hooks/ example_module.py :30: GL08 The object does not have a docstring
210+ { example_module !s } :30: GL08 The object does not have a docstring
210211 """
211212 )
212213
@@ -234,12 +235,12 @@ def test_validate_hook_exclude_option_setup_cfg(example_module, tmp_path, capsys
234235 )
235236
236237 expected = inspect .cleandoc (
237- """
238- numpydoc/tests/hooks/ example_module.py :4: PR01 Parameters {'name'} not documented
238+ f """
239+ { example_module !s } :4: PR01 Parameters {{ 'name'} } not documented
239240
240- numpydoc/tests/hooks/ example_module.py :17: PR01 Parameters {'**kwargs'} not documented
241+ { example_module !s } :17: PR01 Parameters {{ '**kwargs'} } not documented
241242
242- numpydoc/tests/hooks/ example_module.py :17: PR07 Parameter "*args" has no description
243+ { example_module !s } :17: PR07 Parameter "*args" has no description
243244 """
244245 )
245246
@@ -249,16 +250,17 @@ def test_validate_hook_exclude_option_setup_cfg(example_module, tmp_path, capsys
249250
250251
251252@pytest .mark .parametrize (
252- "regex , expected_code" ,
253- [(".*(/| \\ \\ )example.*\.py" , 0 ), (".*/non_existent_match.*\.py" , 1 )],
253+ "file_exists , expected_code" ,
254+ [(True , 0 ), (False , 1 )],
254255)
255256def test_validate_hook_exclude_files_option_pyproject (
256- example_module , regex , expected_code , tmp_path
257+ example_module , file_exists , expected_code , tmp_path
257258):
258259 """
259260 Test that the hook correctly processes the toml config and either includes
260261 or excludes files based on the `exclude_files` option.
261262 """
263+ exclude = str (example_module ) if file_exists else "does_not_exist.py"
262264
263265 with open (tmp_path / "pyproject.toml" , "w" ) as config_file :
264266 config_file .write (
@@ -276,7 +278,7 @@ def test_validate_hook_exclude_files_option_pyproject(
276278 '^Creates',
277279 ]
278280 exclude_files = [
279- '{ regex } ',
281+ '{ re . escape ( exclude ) } ',
280282 ]"""
281283 )
282284 )
@@ -286,16 +288,17 @@ def test_validate_hook_exclude_files_option_pyproject(
286288
287289
288290@pytest .mark .parametrize (
289- "regex , expected_code" ,
290- [(".*(/| \\ \\ )example.*\.py" , 0 ), (".*/non_existent_match.*\.py" , 1 )],
291+ "file_exists , expected_code" ,
292+ [(True , 0 ), (False , 1 )],
291293)
292294def test_validate_hook_exclude_files_option_setup_cfg (
293- example_module , regex , expected_code , tmp_path
295+ example_module , file_exists , expected_code , tmp_path
294296):
295297 """
296298 Test that the hook correctly processes the setup config and either includes
297299 or excludes files based on the `exclude_files` option.
298300 """
301+ exclude = str (example_module ) if file_exists else "does_not_exist.py"
299302
300303 with open (tmp_path / "setup.cfg" , "w" ) as config_file :
301304 config_file .write (
@@ -305,7 +308,7 @@ def test_validate_hook_exclude_files_option_setup_cfg(
305308 checks = all,EX01,SA01,ES01
306309 exclude = \\ .NewClass$,\\ .__init__$
307310 override_SS05 = ^Creates
308- exclude_files = { regex }
311+ exclude_files = { re . escape ( exclude ) }
309312 """
310313 )
311314 )
0 commit comments