code: def hexstr_if_str(
      to_type: Callable[..., T], hexstr_or_primitive: Union[bytes, int, str]
  ) -> T:
      
      if isinstance(hexstr_or_primitive, str):
          if remove_0x_prefix(hexstr_or_primitive) and not is_hex(hexstr_or_primitive):
              raise ValueError(
                  "when sending a str, it must be a hex string. Got: {0!r}".format(
                      hexstr_or_primitive
                  )
              )
          return to_type(hexstr=hexstr_or_primitive)
      else:
          return to_type(hexstr_or_primitive)
docs: 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