code: def validate_dtype(termname, dtype, missing_value):
      """
      Validate a `dtype` and `missing_value` passed to Term.__new__.
  
      Ensures that we know how to represent ``dtype``, and that missing_value
      is specified for types without default missing values.
  
      Returns
      -------
      validated_dtype, validated_missing_value : np.dtype, any
          The dtype and missing_value to use for the new term.
  
      Raises
      ------
      DTypeNotSpecified
          When no dtype was passed to the instance, and the class doesn't
          provide a default.
      NotDType
          When either the class or the instance provides a value not
          coercible to a numpy dtype.
      NoDefaultMissingValue
          When dtype requires an explicit missing_value, but
          ``missing_value`` is NotSpecified.
      """
      if dtype is NotSpecified:
          raise DTypeNotSpecified(termname=termname)
  
      try:
          dtype = dtype_class(dtype)
      except TypeError:
          raise NotDType(dtype=dtype, termname=termname)
  
      if not can_represent_dtype(dtype):
          raise UnsupportedDType(dtype=dtype, termname=termname)
  
      if missing_value is NotSpecified:
          missing_value = default_missing_value_for_dtype(dtype)
  
      try:
          if dtype == categorical_dtype:
              # This check is necessary because we use object dtype for
              # categoricals, and numpy will allow us to promote numerical
              # values to object even though we don't support them.
              _assert_valid_categorical_missing_value(missing_value)
  
          # For any other type, we can check if the missing_value is safe by
          # making an array of that value and trying to safely convert it to
          # the desired type.
          # 'same_kind' allows casting between things like float32 and
          # float64, but not str and int.
          array([missing_value]).astype(
              dtype=dtype, casting="same_kind"
          )
      except TypeError as e:
          raise TypeError(
              "Missing value {value!r} is not a valid choice "
              "for term {termname} with dtype {dtype}.\n\n"
              "Coercion attempt failed with: {error}".format(
                  termname=termname,
                  value=missing_value,
                  dtype=dtype,
                  error=e,
              )
          )
  
      return dtype, missing_value
  
imports: from abc import ABCMeta, abstractproperty
  from bisect import insort
  from collections import Mapping
  from weakref import WeakValueDictionary
  from numpy import array, dtype, ndarray, searchsorted
  from six import with_metaclass
  from catalyst.assets import Asset
  from catalyst.errors import DTypeNotSpecified, InvalidOutputName, NonExistentAssetInTimeFrame, NonSliceableTerm, NonWindowSafeInput, NotDType, TermInputsNotSpecified, TermOutputsEmpty, UnsupportedDType, WindowLengthNotSpecified
  from catalyst.lib.adjusted_array import can_represent_dtype
  from catalyst.lib.labelarray import LabelArray
  from catalyst.utils.input_validation import expect_types
  from catalyst.utils.memoize import lazyval
  from catalyst.utils.numpy_utils import bool_dtype, categorical_dtype, datetime64ns_dtype, default_missing_value_for_dtype
  from catalyst.utils.sharedoc import templated_docstring, PIPELINE_ALIAS_NAME_DOC, PIPELINE_DOWNSAMPLING_FREQUENCY_DOC
  from .downsample_helpers import expect_downsample_frequency
  from .sentinels import NotSpecified
label_desc: Use image node_id 1 for calling a global function with example usage: validate_dtype(termname, dtype, missing_value) and returns: dtype,  missing_value