# tf.OptionalSpec

<br />

|---|---|
| [![](https://www.tensorflow.org/images/tf_logo_32px.png) TensorFlow 1 version](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/OptionalSpec) | [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.3.0/tensorflow/python/data/ops/optional_ops.py#L206-L262) |

Type specification for [`tf.experimental.Optional`](https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/experimental/Optional).

Inherits From: [`TypeSpec`](https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/TypeSpec)

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.OptionalSpec`](https://www.tensorflow.org/api_docs/python/tf/OptionalSpec), [`tf.compat.v1.data.experimental.OptionalStructure`](https://www.tensorflow.org/api_docs/python/tf/OptionalSpec)

<br />

    tf.OptionalSpec(
        element_spec
    )

For instance, [`tf.OptionalSpec`](https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/OptionalSpec) can be used to define a tf.function that takes
[`tf.experimental.Optional`](https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/experimental/Optional) as an input argument:

    @tf.function(input_signature=[tf.OptionalSpec(
      tf.TensorSpec(shape=(), dtype=tf.int32, name=None))])
    def maybe_square(optional):
      if optional.has_value():
        x = optional.get_value()
        return x * x
      return -1
    optional = tf.experimental.Optional.from_value(5)
    print(maybe_square(optional))
    tf.Tensor(25, shape=(), dtype=int32)

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `element_spec` | A nested structure of `TypeSpec` objects that represents the type specification of the optional element. |
| `value_type` | The Python type for values that are compatible with this TypeSpec. <br /> In particular, all values that are compatible with this TypeSpec must be an instance of this type. |

<br />

## Methods

### `from_value`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.3.0/tensorflow/python/data/ops/optional_ops.py#L251-L253)

    @staticmethod
    from_value(
        value
    )

### `is_compatible_with`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.3.0/tensorflow/python/framework/type_spec.py#L93-L108)

    is_compatible_with(
        spec_or_value
    )

Returns true if `spec_or_value` is compatible with this TypeSpec.

### `most_specific_compatible_type`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.3.0/tensorflow/python/framework/type_spec.py#L110-L132)

    most_specific_compatible_type(
        other
    )

Returns the most specific TypeSpec compatible with `self` and `other`.

<br />

<br />

<br />

| Args ||
|---|---|
| `other` | A `TypeSpec`. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If there is no TypeSpec that is compatible with both `self` and `other`. |

<br />

### `__eq__`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.3.0/tensorflow/python/framework/type_spec.py#L293-L296)

    __eq__(
        other
    )

Return self==value.

### `__ne__`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.3.0/tensorflow/python/framework/type_spec.py#L298-L299)

    __ne__(
        other
    )

Return self!=value.