Skip to content

Commit

Permalink
Allow calling getanno with a default value. Failure is still the defa…
Browse files Browse the repository at this point in the history
…ult behavior.

PiperOrigin-RevId: 199349592
  • Loading branch information
Dan Moldovan authored and tensorflower-gardener committed Jun 5, 2018
1 parent 1e92632 commit 70a96b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
19 changes: 15 additions & 4 deletions tensorflow/contrib/autograph/pyct/anno.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ class Basic(NoValue):
'`name_map` allows renaming symbols.')


def getanno(node, key, field_name='___pyct_anno'):
return getattr(node, field_name)[key]
FAIL = object()


def getanno(node, key, default=FAIL, field_name='___pyct_anno'):
if (default is FAIL or
(hasattr(node, field_name) and getattr(node, field_name)[key])):
return getattr(node, field_name)[key]
else:
return default


def hasanno(node, key, field_name='___pyct_anno'):
Expand All @@ -73,5 +80,9 @@ def delanno(node, key, field_name='___pyct_anno'):


def copyanno(from_node, to_node, key, field_name='___pyct_anno'):
if hasanno(from_node, key, field_name):
setanno(to_node, key, getanno(from_node, key, field_name), field_name)
if hasanno(from_node, key, field_name=field_name):
setanno(
to_node,
key,
getanno(from_node, key, field_name=field_name),
field_name=field_name)
1 change: 1 addition & 0 deletions tensorflow/contrib/autograph/pyct/anno_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def test_basic(self):
self.assertFalse(anno.hasanno(node, 'foo'))
with self.assertRaises(AttributeError):
anno.getanno(node, 'foo')
self.assertIsNone(anno.getanno(node, 'foo', default=None))

def test_copyanno(self):
node_1 = ast.Name()
Expand Down

0 comments on commit 70a96b5

Please sign in to comment.