Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/autograph/converters/asserts.py: 44%
16 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 2016 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"""Converts assert statements to their corresponding TF calls."""
17import gast
19from tensorflow.python.autograph.core import converter
20from tensorflow.python.autograph.pyct import templates
23class AssertTransformer(converter.Base):
24 """Transforms Assert nodes to Call so they can be handled as functions."""
26 def visit_Assert(self, node):
27 self.generic_visit(node)
29 # Note: The lone tf.Assert call will be wrapped with control_dependencies
30 # by side_effect_guards.
31 template = """
32 ag__.assert_stmt(test, lambda: msg)
33 """
35 if node.msg is None:
36 return templates.replace(
37 template,
38 test=node.test,
39 msg=gast.Constant('Assertion error', kind=None))
40 elif isinstance(node.msg, gast.Constant):
41 return templates.replace(template, test=node.test, msg=node.msg)
42 else:
43 raise NotImplementedError('can only convert string messages for now.')
46def transform(node, ctx):
47 node = AssertTransformer(ctx).visit(node)
48 return node