def hexstr_if_str(<EOL>to_type: Callable[..., T], hexstr_or_primitive: Union[bytes, int, str]<EOL>) -> T:
if isinstance(hexstr_or_primitive, str):<EOL><INDENT>if remove_0x_prefix(hexstr_or_primitive) and not is_hex(hexstr_or_primitive):<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>".format(<EOL>hexstr_or_primitive<EOL>)<EOL>)<EOL><DEDENT>return to_type(hexstr=hexstr_or_primitive)<EOL><DEDENT>else:<EOL><INDENT>return to_type(hexstr_or_primitive)<EOL><DEDENT>
Convert to a type, assuming that strings can be only hexstr (not unicode text)

:param to_type function: takes the arguments (primitive, hexstr=hexstr, text=text),
    eg~ to_bytes, to_text, to_hex, to_int, etc
:param hexstr_or_primitive bytes, str, int: value to convert