For example, in our application code, we'll use a warning for some type coercion for example
if isinstance(obj,Bar):
warnings.warn('Using a Bar as a Foo!')
In production, this will just print a message to the log. So this is useful for things like phasing out old behavior, where we try to stop using it but don't want hard failure if it's used for now.
But in our tests, we do want hard failure (so we can phase out the behavior as much as possible before removing the functionality). So we do something like the following:
if TEST:
warnings.filterwarning('error','Using a Bar as a Foo!',RuntimeWarning)
Which basically says "if I receive a warning matching the regex given, then raise an error instead of just logging". So if I hit that code path during tests it will fail the test.
It's very useful, even if warnings is a bit too much about regex matching for my tastes
For example, in our application code, we'll use a warning for some type coercion for example
In production, this will just print a message to the log. So this is useful for things like phasing out old behavior, where we try to stop using it but don't want hard failure if it's used for now.But in our tests, we do want hard failure (so we can phase out the behavior as much as possible before removing the functionality). So we do something like the following:
Which basically says "if I receive a warning matching the regex given, then raise an error instead of just logging". So if I hit that code path during tests it will fail the test.It's very useful, even if warnings is a bit too much about regex matching for my tastes