Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/autograph/operators/exceptions.py: 33%
21 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Exception handling statements: assert, etc."""
17from tensorflow.python.framework import tensor_util
18from tensorflow.python.ops import control_flow_assert
19from tensorflow.python.util import tf_inspect
22def assert_stmt(expression1, expression2):
23 """Functional form of an assert statement.
25 This follows the semantics of the Python assert statement, however the
26 concrete implementations may deviate from it. See the respective
27 implementation for details.
29 In general, the assert statement should not be used for control flow.
30 Furthermore, it is encouraged that the assertion expressions should not have
31 side effects.
33 Args:
34 expression1: Any
35 expression2: Callable[[], Any], returns the expression to include in the
36 error message when expression1 evaluates to False. When expression1 is
37 True, the result of expression2 will not be evaluated, however,
38 expression2 itself may be evaluated in some implementations.
40 Returns:
41 Any, implementation-dependent.
43 Raises:
44 ValueError: if any arguments are illegal.
45 """
46 if not callable(expression2):
47 raise ValueError('{} must be a callable'.format(expression2))
48 args, _, keywords, _ = tf_inspect.getargspec(expression2)
49 if args or keywords:
50 raise ValueError('{} may not have any arguments'.format(expression2))
52 if tensor_util.is_tf_type(expression1):
53 return _tf_assert_stmt(expression1, expression2)
54 else:
55 return _py_assert_stmt(expression1, expression2)
58def _tf_assert_stmt(expression1, expression2):
59 """Overload of assert_stmt that stages a TF Assert.
61 This implementation deviates from Python semantics as follows:
62 (1) the assertion is verified regardless of the state of __debug__
63 (2) on assertion failure, the graph execution will fail with
64 tensorflow.errors.ValueError, rather than AssertionError.
66 Args:
67 expression1: tensorflow.Tensor, must evaluate to a tf.bool scalar
68 expression2: Callable[[], Union[tensorflow.Tensor, List[tensorflow.Tensor]]]
70 Returns:
71 tensorflow.Operation
72 """
73 expression2_tensors = expression2()
74 if not isinstance(expression2_tensors, list):
75 expression2_tensors = [expression2_tensors]
76 return control_flow_assert.Assert(expression1, expression2_tensors)
79def _py_assert_stmt(expression1, expression2):
80 """Overload of assert_stmt that executes a Python assert statement."""
81 assert expression1, expression2()
82 return None