Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
'approx' should error immediately if an invalid parameter is passed #3473
Using pytest.approx with an invalid parameter (eg, a string) should fail immediately rather than waiting until it calls repr(). Example:
I would expected it to fail on the second line rather than when it prints it out.
This gets more confusing when you're logging it:
# example.py import pytest import logging x = pytest.approx("abc") logging.basicConfig(level=logging.DEBUG) logging.info(x)
This issue in particular would be solved by putting the math.isinf check in the constructor rather than only checking when printing out the value.
After some digging I have traced the flow through these files:
Approx function is fired off
Providing a string argument to this function defaults the cls selection to approx scalar.
Then we go through the ApproxBase class for instantiation.
Then we go to the ApproxSequence class for evaluation
This class' repr method is fired off. Which is where we get to your issue.
This function is checking for numbers, and thats when it raises the typeerror for it being a string.
Im unsure where to go from here because i cant even seem to understand pythons math library. I think its a wrapper function for the C math library, but Im still confused on how it works.
The developers could check for a string to be passed for the
the initial call to approx should fail, if its just a string, we should raise a type-error instead of accidentally using a scalar base
an alternative would be to actually define approx for strings, but that needs quite some design discussion at best
so i would favour finally failing early from the mistake