leonardt / hwtypes

Python implementations of fixed size hardware types (Bit, BitVector, UInt, SInt, ...) based on the SMT-LIB2 semantics
BSD 3-Clause "New" or "Revised" License
18 stars 4 forks source link

Set warnings.warn stacklevel to 3 #141

Closed leonardt closed 4 years ago

leonardt commented 4 years ago

With this change, instead of a message like this:

  /Users/lenny/repos/hwtypes/hwtypes/bit_vector.py:143: UserWarning: Truncating value from <class 'hwtypes.bit_vector.BitVector[32]'> to <class 'hwtypes.bit_vector.BitVector[21]'>
    warnings.warn('Truncating value from {} to {}'.format(type(value), type(self)))

we now get something like this:

  /Users/lenny/repos/magma_riscv_mini/tests/utils.py:11: UserWarning: Truncating value from <class 'hwtypes.bit_vector.BitVector[32]'> to <class 'hwtypes.bit_vector.BitVector[21]'>
    return BitVector[21](x & ((1 << 20) - 1))

which is much more helpful. We set stacklevel to 3 instead of 2 because we need to also ignore the metaclass call method, or else we get a message like this:

  /Users/lenny/repos/hwtypes/hwtypes/bit_vector_abc.py:28: UserWarning: Truncating value from <class 'hwtypes.bit_vector.BitVector[32]'> to <class 'hwtypes.bit_vector.BitVector[21]'>
    return super().__call__(value, *args, **kwargs)