From 53ed3c24437909397b9ff283d4bf0e329bcc940b Mon Sep 17 00:00:00 2001 From: Chris Jewell Date: Tue, 30 Jul 2024 22:58:24 +0100 Subject: [PATCH] Field name checks for StructTuple This PR introduces a new check for field names in a StructTuple, preventing any that over-ride existing members of the tuple base class. It is intended to trap a gotcha in JointDistributionCoroutine if a user names a variable "index" or "count". --- tensorflow_probability/python/internal/structural_tuple.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tensorflow_probability/python/internal/structural_tuple.py b/tensorflow_probability/python/internal/structural_tuple.py index 9199a7dd1e..e95f244eb8 100644 --- a/tensorflow_probability/python/internal/structural_tuple.py +++ b/tensorflow_probability/python/internal/structural_tuple.py @@ -51,6 +51,9 @@ def _validate_field_names(field_names): if name.startswith('_'): raise ValueError( 'Field names cannot start with an underscore: {}'.format(name)) + if name in dir(tuple): + raise ValueError( + 'Field name {} is already a member of StructTuple'.format(name)) if name in seen: raise ValueError('Encountered duplicate field name: {}'.format(name)) seen.add(name)