@@ -85,12 +85,16 @@ def __post_init__(self) -> None:
8585 )
8686
8787 if self .alias is not None :
88- assert (
89- self .default is _UNSET_SENTINEL
90- and self .justknob is None
91- and self .env_name_default is None
92- and self .env_name_force is None
93- ), "if alias is set, none of {default, justknob and env var} can be set"
88+ if (
89+ self .default is not _UNSET_SENTINEL
90+ or self .justknob is not None
91+ or self .env_name_default is not None
92+ or self .env_name_force is not None
93+ ):
94+ raise AssertionError (
95+ "if alias is set, none of {default, justknob, \
96+ env_name_default and env_name_force} can be set"
97+ )
9498
9599 @staticmethod
96100 def string_or_list_of_string_to_list (
@@ -100,7 +104,8 @@ def string_or_list_of_string_to_list(
100104 return None
101105 if isinstance (val , str ):
102106 return [val ]
103- assert isinstance (val , list )
107+ if not isinstance (val , list ):
108+ raise AssertionError (f"val is not a list, got { type (val )} " )
104109 return val
105110
106111
@@ -193,7 +198,10 @@ def visit(
193198 if dest is module :
194199 delattr (module , key )
195200 elif isinstance (value , type ):
196- assert value .__module__ == module .__name__
201+ if value .__module__ != module .__name__ :
202+ raise AssertionError (
203+ f"subconfig class { value } must be defined in module { module .__name__ } "
204+ )
197205 # a subconfig with `class Blah:` syntax
198206 proxy = SubConfigProxy (module , f"{ name } ." )
199207 visit (value , proxy , f"{ name } ." )
@@ -234,10 +242,8 @@ def get_assignments_with_compile_ignored_comments(module: ModuleType) -> set[str
234242 prev_name = ""
235243 maybe_current = token .string .strip ()
236244 if COMPILE_IGNORED_MARKER in maybe_current :
237- assert current_comment == (
238- "" ,
239- - 1 ,
240- ), f"unconsumed { COMPILE_IGNORED_MARKER } "
245+ if current_comment != ("" , - 1 ):
246+ raise AssertionError (f"unconsumed { COMPILE_IGNORED_MARKER } " )
241247 current_comment = maybe_current , token .start [0 ]
242248 elif token .type == tokenize .NAME :
243249 # Only accept the first name token, to handle if you have
@@ -254,7 +260,8 @@ def get_assignments_with_compile_ignored_comments(module: ModuleType) -> set[str
254260 assignments .add (prev_name )
255261 current_comment = "" , - 1 # reset
256262 prev_name = ""
257- assert current_comment == ("" , - 1 ), f"unconsumed { COMPILE_IGNORED_MARKER } "
263+ if current_comment != ("" , - 1 ):
264+ raise AssertionError (f"unconsumed { COMPILE_IGNORED_MARKER } " )
258265 return assignments
259266
260267
@@ -306,20 +313,22 @@ def __init__(self, config: _Config):
306313
307314 # Ensure justknobs and envvars are allowlisted types
308315 if self .justknob is not None and self .default is not None :
309- assert isinstance (self .default , bool ), (
310- f"justknobs only support booleans, { self .default } is not a boolean"
311- )
316+ if not isinstance (self .default , bool ):
317+ raise AssertionError (
318+ f"justknobs only support booleans, { self .default } is not a boolean"
319+ )
312320 if self .value_type is not None and (
313321 config .env_name_default is not None or config .env_name_force is not None
314322 ):
315- assert self .value_type in (
323+ if self .value_type not in (
316324 bool ,
317325 str ,
318326 Optional [bool ],
319327 Optional [str ],
320- ), (
321- f"envvar configs only support (optional) booleans or strings, { self .value_type } is neither"
322- )
328+ ):
329+ raise AssertionError (
330+ f"envvar configs only support (optional) booleans or strings, { self .value_type } is neither"
331+ )
323332
324333
325334class ConfigModule (ModuleType ):
@@ -417,7 +426,10 @@ def _get_alias_val(self, entry: _ConfigEntry) -> Any:
417426
418427 def _set_alias_val (self , entry : _ConfigEntry , val : Any ) -> None :
419428 data = self ._get_alias_module_and_name (entry )
420- assert data is not None
429+ if data is None :
430+ raise AssertionError (
431+ "alias data should not be None when setting alias value"
432+ )
421433 module , constant_name = data
422434 setattr (module , constant_name , val )
423435
@@ -642,19 +654,32 @@ def foo(...):
642654 changes : dict [str , Any ]
643655 if arg1 is not None :
644656 if arg2 is not None :
645- assert isinstance (arg1 , str )
657+ if not isinstance (arg1 , str ):
658+ raise AssertionError (
659+ "first argument must be a string when passing 2 positional args to patch"
660+ )
646661 # patch("key", True) syntax
647662 changes = {arg1 : arg2 }
648663 else :
649- assert isinstance (arg1 , dict )
664+ if not isinstance (arg1 , dict ):
665+ raise AssertionError (
666+ "first argument must be a dict when passing a single positional arg to patch"
667+ )
650668 # patch({"key": True}) syntax
651669 changes = arg1
652- assert not kwargs
670+ if kwargs :
671+ raise AssertionError (
672+ "cannot pass both positional and keyword arguments to patch"
673+ )
653674 else :
654675 # patch(key=True) syntax
655676 changes = kwargs
656- assert arg2 is None
657- assert isinstance (changes , dict ), f"expected `dict` got { type (changes )} "
677+ if arg2 is not None :
678+ raise AssertionError (
679+ "second positional argument is only valid when first argument is a key string"
680+ )
681+ if not isinstance (changes , dict ):
682+ raise AssertionError (f"expected `dict` got { type (changes )} " )
658683 prior : dict [str , Any ] = {}
659684 config = self
660685
@@ -663,7 +688,10 @@ def __init__(self) -> None:
663688 self .changes = changes
664689
665690 def __enter__ (self ) -> None :
666- assert not prior
691+ if prior :
692+ raise AssertionError (
693+ "prior should be empty when entering ConfigPatch"
694+ )
667695 for key in self .changes .keys ():
668696 # KeyError on invalid entry
669697 prior [key ] = config .__getattr__ (key )
0 commit comments