Allow undefined symbols to be queried for attributes and slices. An upcoming change will create situations when this can happen before the verification for undefined symbols that currently precludes such operations.

PiperOrigin-RevId: 288336945
Change-Id: I5bb1e02a9b3dacb557749ada4aaffe402c17cd82
This commit is contained in:
Dan Moldovan 2020-01-06 11:13:34 -08:00 committed by TensorFlower Gardener
parent b3d978711f
commit a4b4a8d251
2 changed files with 23 additions and 1 deletions

View File

@ -46,10 +46,25 @@ class Undefined(object):
symbol_name: Text, identifier for the undefined symbol
"""
__slots__ = ('symbol_name',)
def __init__(self, symbol_name):
# TODO(aqj) Possibly remove this after Symbols are fully integrated.
self.symbol_name = symbol_name
def __repr__(self):
return self.symbol_name
def __getattribute__(self, name):
try:
# If it's an existing attribute, return it.
return object.__getattribute__(self, name)
except AttributeError:
# Otherwise return Undefined.
return self
def __getitem__(self, i):
return self
def is_undefined(value):
"""Checks whether Autograph has determined that a given value is undefined.

View File

@ -34,5 +34,12 @@ class SpecialValuesTest(test.TestCase):
self.assertTrue(special_values.is_undefined(undefined_symbol))
self.assertTrue(special_values.is_undefined(undefined_symbol2))
def test_undefined_operations(self):
undefined_symbol = special_values.Undefined('name')
self.assertTrue(special_values.is_undefined(undefined_symbol.foo))
self.assertTrue(special_values.is_undefined(undefined_symbol[0]))
self.assertFalse(special_values.is_undefined(undefined_symbol.__class__))
if __name__ == '__main__':
test.main()