22
33import sys
44import warnings
5+ from typing import Any , Literal
56
67from pytest import Config , Parser
78
1213
1314
1415def pytest_addoption (parser : Parser ) -> None :
16+ def add_ini_option (
17+ opt_type : (
18+ Literal ["string" , "paths" , "pathlist" , "args" , "linelist" , "bool" ] | None
19+ )
20+ ) -> None :
21+ parser .addini (
22+ group .options [- 1 ].names ()[0 ][2 :],
23+ group .options [- 1 ].attrs ()["help" ],
24+ opt_type ,
25+ )
26+
1527 group = parser .getgroup ("typeguard" )
1628 group .addoption (
1729 "--typeguard-packages" ,
1830 action = "store" ,
1931 help = "comma separated name list of packages and modules to instrument for "
2032 "type checking, or :all: to instrument all modules loaded after typeguard" ,
2133 )
34+ add_ini_option ("linelist" )
35+
2236 group .addoption (
2337 "--typeguard-debug-instrumentation" ,
2438 action = "store_true" ,
2539 help = "print all instrumented code to stderr" ,
2640 )
41+ add_ini_option ("bool" )
42+
2743 group .addoption (
2844 "--typeguard-typecheck-fail-callback" ,
2945 action = "store" ,
@@ -33,6 +49,8 @@ def pytest_addoption(parser: Parser) -> None:
3349 "handle a TypeCheckError"
3450 ),
3551 )
52+ add_ini_option ("string" )
53+
3654 group .addoption (
3755 "--typeguard-forward-ref-policy" ,
3856 action = "store" ,
@@ -42,21 +60,31 @@ def pytest_addoption(parser: Parser) -> None:
4260 "annotations"
4361 ),
4462 )
63+ add_ini_option ("string" )
64+
4565 group .addoption (
4666 "--typeguard-collection-check-strategy" ,
4767 action = "store" ,
4868 choices = list (CollectionCheckStrategy .__members__ ),
4969 help = "determines how thoroughly to check collections (list, dict, etc)" ,
5070 )
71+ add_ini_option ("string" )
5172
5273
5374def pytest_configure (config : Config ) -> None :
54- packages_option = config .getoption ("typeguard_packages" )
55- if packages_option :
56- if packages_option == ":all:" :
57- packages : list [str ] | None = None
75+ def getoption (name : str ) -> Any :
76+ return config .getoption (name .replace ("-" , "_" )) or config .getini (name )
77+
78+ packages : list [str ] | None = []
79+ if packages_option := config .getoption ("typeguard_packages" ):
80+ packages = [pkg .strip () for pkg in packages_option .split ("," )]
81+ elif packages_ini := config .getini ("typeguard-packages" ):
82+ packages = packages_ini
83+
84+ if packages :
85+ if packages == [":all:" ]:
86+ packages = None
5887 else :
59- packages = [pkg .strip () for pkg in packages_option .split ("," )]
6088 already_imported_packages = sorted (
6189 package for package in packages if package in sys .modules
6290 )
@@ -70,11 +98,11 @@ def pytest_configure(config: Config) -> None:
7098
7199 install_import_hook (packages = packages )
72100
73- debug_option = config . getoption ("typeguard_debug_instrumentation " )
101+ debug_option = getoption ("typeguard-debug-instrumentation " )
74102 if debug_option :
75103 global_config .debug_instrumentation = True
76104
77- fail_callback_option = config . getoption ("typeguard_typecheck_fail_callback " )
105+ fail_callback_option = getoption ("typeguard-typecheck-fail-callback " )
78106 if fail_callback_option :
79107 callback = resolve_reference (fail_callback_option )
80108 if not callable (callback ):
@@ -85,14 +113,12 @@ def pytest_configure(config: Config) -> None:
85113
86114 global_config .typecheck_fail_callback = callback
87115
88- forward_ref_policy_option = config . getoption ("typeguard_forward_ref_policy " )
116+ forward_ref_policy_option = getoption ("typeguard-forward-ref-policy " )
89117 if forward_ref_policy_option :
90118 forward_ref_policy = ForwardRefPolicy .__members__ [forward_ref_policy_option ]
91119 global_config .forward_ref_policy = forward_ref_policy
92120
93- collection_check_strategy_option = config .getoption (
94- "typeguard_collection_check_strategy"
95- )
121+ collection_check_strategy_option = getoption ("typeguard-collection-check-strategy" )
96122 if collection_check_strategy_option :
97123 collection_check_strategy = CollectionCheckStrategy .__members__ [
98124 collection_check_strategy_option
0 commit comments