2
2
3
3
import re
4
4
from abc import abstractmethod
5
+ from collections .abc import Callable
5
6
from pathlib import Path
6
7
from typing import Any , Literal , Protocol , TypeAlias
7
8
@@ -104,6 +105,10 @@ class _NoConfigValue:
104
105
pass
105
106
106
107
108
+ def _get_no_config_value () -> _NoConfigValue :
109
+ return _NoConfigValue ()
110
+
111
+
107
112
class ConfigEntry (BaseModel ):
108
113
"""A configuration entry in a config file associated with a tool.
109
114
@@ -117,7 +122,7 @@ class ConfigEntry(BaseModel):
117
122
"""
118
123
119
124
keys : list [str ]
120
- value : Any | InstanceOf [ _NoConfigValue ] = _NoConfigValue ()
125
+ get_value : Callable [[], Any ] = _get_no_config_value
121
126
122
127
123
128
class ConfigItem (BaseModel ):
@@ -390,7 +395,7 @@ def add_configs(self) -> None:
390
395
391
396
(entry ,) = config_entries
392
397
393
- if isinstance (entry .value , _NoConfigValue ):
398
+ if isinstance (entry .get_value () , _NoConfigValue ):
394
399
# No value to add, so skip this config item.
395
400
continue
396
401
@@ -413,7 +418,7 @@ def add_configs(self) -> None:
413
418
f"Adding { self .name } config to '{ used_file_manager .relative_path } '."
414
419
)
415
420
first_addition = False
416
- used_file_manager [entry .keys ] = entry .value
421
+ used_file_manager [entry .keys ] = entry .get_value ()
417
422
418
423
def remove_configs (self ) -> None :
419
424
"""Remove the tool's configuration sections.
@@ -587,15 +592,15 @@ def get_config_spec(self) -> ConfigSpec:
587
592
root = {
588
593
Path (".codespellrc" ): ConfigEntry (
589
594
keys = ["codespell" , "ignore-regex" ],
590
- value = "[A-Za-z0-9+/]{100,}" ,
595
+ get_value = lambda : "[A-Za-z0-9+/]{100,}" ,
591
596
),
592
597
Path ("setup.cfg" ): ConfigEntry (
593
598
keys = ["codespell" , "ignore-regex" ],
594
- value = "[A-Za-z0-9+/]{100,}" ,
599
+ get_value = lambda : "[A-Za-z0-9+/]{100,}" ,
595
600
),
596
601
Path ("pyproject.toml" ): ConfigEntry (
597
602
keys = ["tool" , "codespell" , "ignore-regex" ],
598
- value = ["[A-Za-z0-9+/]{100,}" ],
603
+ get_value = lambda : ["[A-Za-z0-9+/]{100,}" ],
599
604
),
600
605
},
601
606
),
@@ -658,18 +663,18 @@ def get_test_deps(self, *, unconditional: bool = False) -> list[Dependency]:
658
663
def get_config_spec (self ) -> ConfigSpec :
659
664
# https://coverage.readthedocs.io/en/latest/config.html#configuration-reference
660
665
661
- run = { "source" : [ get_source_dir_str ()]}
662
- report = {
663
- "exclude_also" : [
664
- "if TYPE_CHECKING: " ,
665
- "raise AssertionError " ,
666
- "raise NotImplementedError " ,
667
- "assert_never(.*) " ,
668
- "class .* \\ bProtocol \\ ):" ,
669
- "@(abc \\ .)?abstractmethod" ,
670
- ],
671
- "omit" : [ "*/pytest-of-*/*" ],
672
- }
666
+ exclude_also = [
667
+ "if TYPE_CHECKING:" ,
668
+ "raise AssertionError" ,
669
+ "raise NotImplementedError " ,
670
+ "assert_never(.*) " ,
671
+ "class .* \\ bProtocol \\ ): " ,
672
+ "@(abc \\ .)?abstractmethod " ,
673
+ ]
674
+ omit = [ "*/pytest-of-*/*" ]
675
+
676
+ def _get_source ():
677
+ return [ get_source_dir_str ()]
673
678
674
679
return ConfigSpec .from_flat (
675
680
file_managers = [
@@ -693,28 +698,79 @@ def get_config_spec(self) -> ConfigSpec:
693
698
ConfigItem (
694
699
description = "Run Configuration" ,
695
700
root = {
696
- Path (".coveragerc" ): ConfigEntry (keys = ["run" ], value = run ),
701
+ Path (".coveragerc" ): ConfigEntry (keys = ["run" ]),
702
+ Path ("setup.cfg" ): ConfigEntry (keys = ["coverage:run" ]),
703
+ Path ("tox.ini" ): ConfigEntry (keys = ["coverage:run" ]),
704
+ Path ("pyproject.toml" ): ConfigEntry (
705
+ keys = ["tool" , "coverage" , "run" ]
706
+ ),
707
+ },
708
+ ),
709
+ ConfigItem (
710
+ description = "Source Configuration" ,
711
+ root = {
712
+ Path (".coveragerc" ): ConfigEntry (
713
+ keys = ["run" , "source" ], get_value = _get_source
714
+ ),
697
715
Path ("setup.cfg" ): ConfigEntry (
698
- keys = ["coverage:run" ], value = run
716
+ keys = ["coverage:run" , "source" ], get_value = _get_source
717
+ ),
718
+ Path ("tox.ini" ): ConfigEntry (
719
+ keys = ["coverage:run" , "source" ], get_value = _get_source
699
720
),
700
- Path ("tox.ini" ): ConfigEntry (keys = ["coverage:run" ], value = run ),
701
721
Path ("pyproject.toml" ): ConfigEntry (
702
- keys = ["tool" , "coverage" , "run" ], value = run
722
+ keys = ["tool" , "coverage" , "run" , "source" ],
723
+ get_value = _get_source ,
703
724
),
704
725
},
705
726
),
706
727
ConfigItem (
707
728
description = "Report Configuration" ,
708
729
root = {
709
- Path (".coveragerc" ): ConfigEntry (keys = ["report" ], value = report ),
730
+ Path (".coveragerc" ): ConfigEntry (keys = ["report" ]),
731
+ Path ("setup.cfg" ): ConfigEntry (keys = ["coverage:report" ]),
732
+ Path ("tox.ini" ): ConfigEntry (keys = ["coverage:report" ]),
733
+ Path ("pyproject.toml" ): ConfigEntry (
734
+ keys = ["tool" , "coverage" , "report" ]
735
+ ),
736
+ },
737
+ ),
738
+ ConfigItem (
739
+ description = "Exclude Also Configuration" ,
740
+ root = {
741
+ Path (".coveragerc" ): ConfigEntry (
742
+ keys = ["report" , "exclude_also" ],
743
+ get_value = lambda : exclude_also ,
744
+ ),
710
745
Path ("setup.cfg" ): ConfigEntry (
711
- keys = ["coverage:report" ], value = report
746
+ keys = ["coverage:report" , "exclude_also" ],
747
+ get_value = lambda : exclude_also ,
712
748
),
713
749
Path ("tox.ini" ): ConfigEntry (
714
- keys = ["coverage:report" ], value = report
750
+ keys = ["coverage:report" , "exclude_also" ],
751
+ get_value = lambda : exclude_also ,
715
752
),
716
753
Path ("pyproject.toml" ): ConfigEntry (
717
- keys = ["tool" , "coverage" , "report" ], value = report
754
+ keys = ["tool" , "coverage" , "report" , "exclude_also" ],
755
+ get_value = lambda : exclude_also ,
756
+ ),
757
+ },
758
+ ),
759
+ ConfigItem (
760
+ description = "Omit Configuration" ,
761
+ root = {
762
+ Path (".coveragerc" ): ConfigEntry (
763
+ keys = ["report" , "omit" ], get_value = lambda : omit
764
+ ),
765
+ Path ("setup.cfg" ): ConfigEntry (
766
+ keys = ["coverage:report" , "omit" ], get_value = lambda : omit
767
+ ),
768
+ Path ("tox.ini" ): ConfigEntry (
769
+ keys = ["coverage:report" , "omit" ], get_value = lambda : omit
770
+ ),
771
+ Path ("pyproject.toml" ): ConfigEntry (
772
+ keys = ["tool" , "coverage" , "report" , "omit" ],
773
+ get_value = lambda : omit ,
718
774
),
719
775
},
720
776
),
@@ -964,14 +1020,22 @@ def get_config_spec(self) -> ConfigSpec:
964
1020
resolution = "first" ,
965
1021
config_items = [
966
1022
ConfigItem (
967
- description = "Overall config " ,
1023
+ description = "Overall Config " ,
968
1024
root = {
969
1025
Path ("pyproject.toml" ): ConfigEntry (
970
- keys = ["tool" , "pyproject-fmt" ],
971
- value = {"keep_full_version" : True },
1026
+ keys = ["tool" , "pyproject-fmt" ]
972
1027
)
973
1028
},
974
- )
1029
+ ),
1030
+ ConfigItem (
1031
+ description = "Keep Full Version" ,
1032
+ root = {
1033
+ Path ("pyproject.toml" ): ConfigEntry (
1034
+ keys = ["tool" , "pyproject-fmt" , "keep_full_version" ],
1035
+ get_value = lambda : True ,
1036
+ )
1037
+ },
1038
+ ),
975
1039
],
976
1040
)
977
1041
@@ -1114,17 +1178,20 @@ def get_config_spec(self) -> ConfigSpec:
1114
1178
description = "INI-Style Options" ,
1115
1179
root = {
1116
1180
Path ("pytest.ini" ): ConfigEntry (
1117
- keys = ["pytest" ], value = value_ini
1181
+ keys = ["pytest" ], get_value = lambda : value_ini
1118
1182
),
1119
1183
Path (".pytest.ini" ): ConfigEntry (
1120
- keys = ["pytest" ], value = value_ini
1184
+ keys = ["pytest" ], get_value = lambda : value_ini
1121
1185
),
1122
1186
Path ("pyproject.toml" ): ConfigEntry (
1123
- keys = ["tool" , "pytest" , "ini_options" ], value = value
1187
+ keys = ["tool" , "pytest" , "ini_options" ],
1188
+ get_value = lambda : value ,
1189
+ ),
1190
+ Path ("tox.ini" ): ConfigEntry (
1191
+ keys = ["pytest" ], get_value = lambda : value_ini
1124
1192
),
1125
- Path ("tox.ini" ): ConfigEntry (keys = ["pytest" ], value = value_ini ),
1126
1193
Path ("setup.cfg" ): ConfigEntry (
1127
- keys = ["tool:pytest" ], value = value_ini
1194
+ keys = ["tool:pytest" ], get_value = lambda : value_ini
1128
1195
),
1129
1196
},
1130
1197
),
@@ -1316,14 +1383,14 @@ def get_config_spec(self) -> ConfigSpec:
1316
1383
description = "Line length" ,
1317
1384
root = {
1318
1385
Path (".ruff.toml" ): ConfigEntry (
1319
- keys = ["line-length" ], value = line_length
1386
+ keys = ["line-length" ], get_value = lambda : line_length
1320
1387
),
1321
1388
Path ("ruff.toml" ): ConfigEntry (
1322
- keys = ["line-length" ], value = line_length
1389
+ keys = ["line-length" ], get_value = lambda : line_length
1323
1390
),
1324
1391
Path ("pyproject.toml" ): ConfigEntry (
1325
1392
keys = ["tool" , "ruff" , "line-length" ],
1326
- value = line_length ,
1393
+ get_value = lambda : line_length ,
1327
1394
),
1328
1395
},
1329
1396
),
0 commit comments