When applying the Python function type() to a single object, the type of the object is returned.
num = 5
type(num)
Out[1]: int
When embedding this output into a string and printing the result, this seems to behave as expected.
num = 5
print(f"type of {num} is {type(num)}")
type of 5 is <class 'int'>
However, when using this exact message as an assertion error message, the type disappears from the message output.
num = 5
assert isinstance(num,str), f"type of {num} is {type(num)}"
AssertionError: type of 5 is
I am running the code through a notebook on a Databricks cluster, which is displayed in a web browser.
The expected output would be: AssertionError: type of 5 is <class 'int'>
.
What is the reason for this and how can it be avoided?
Edit: As mentioned by several commenters, the code works just fine running outside of the databricks environment. I have successfully verified this too using Python 3.10.6.
The character <
in the output is being interpreted as the beginning of an HTML tag. By applying replace("<","<")
to the f-string, this can be avoided. The full code then becomes:
num = 5
assert isinstance(num,str), f"type of {num} is {type(num)}".replace("<", "<")