From 99090f64569228ce400ed9540dc7a6cb401fc356 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Sat, 8 Nov 2025 01:22:03 +0530 Subject: [PATCH 1/3] Refactor __exit__ method in name_scope.py Refactor exit behavior to check _pop_on_exit as a callable --- keras/src/backend/common/name_scope.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/keras/src/backend/common/name_scope.py b/keras/src/backend/common/name_scope.py index 71a8408767b6..bfa8d86d37d2 100644 --- a/keras/src/backend/common/name_scope.py +++ b/keras/src/backend/common/name_scope.py @@ -54,12 +54,17 @@ def __enter__(self): return self def __exit__(self, *args, **kwargs): - if self._pop_on_exit: - name_scope_stack = global_state.get_global_attribute( - "name_scope_stack" - ) + if not self._pop_on_exit(): + return + name_scope_stack = global_state.get_global_attribute( + "name_scope_stack" + ) + if not name_scope_stack: + return + try: name_scope_stack.pop() - + except (AttributeError, IndexError): + return def current_path(): name_scope_stack = global_state.get_global_attribute("name_scope_stack") From 4c4088a103c8ec652e300728f7faf8f9ad923571 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Sat, 8 Nov 2025 06:39:59 +0000 Subject: [PATCH 2/3] fix --- keras/src/backend/common/name_scope.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/keras/src/backend/common/name_scope.py b/keras/src/backend/common/name_scope.py index bfa8d86d37d2..1fe73bf7ee25 100644 --- a/keras/src/backend/common/name_scope.py +++ b/keras/src/backend/common/name_scope.py @@ -54,17 +54,12 @@ def __enter__(self): return self def __exit__(self, *args, **kwargs): - if not self._pop_on_exit(): - return - name_scope_stack = global_state.get_global_attribute( - "name_scope_stack" - ) - if not name_scope_stack: - return - try: - name_scope_stack.pop() - except (AttributeError, IndexError): - return + if self._pop_on_exit: + name_scope_stack = global_state.get_global_attribute( + "name_scope_stack" + ) + if name_scope_stack: + name_scope_stack.pop() def current_path(): name_scope_stack = global_state.get_global_attribute("name_scope_stack") From 85f02eac5effba00886d526a22c44cbf08962e9e Mon Sep 17 00:00:00 2001 From: Abhinav Date: Sat, 8 Nov 2025 06:56:39 +0000 Subject: [PATCH 3/3] remove linting error and added test cases --- keras/src/backend/common/name_scope.py | 13 +++--- keras/src/backend/common/name_scope_test.py | 51 +++++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/keras/src/backend/common/name_scope.py b/keras/src/backend/common/name_scope.py index 1fe73bf7ee25..9eb1dd8985f6 100644 --- a/keras/src/backend/common/name_scope.py +++ b/keras/src/backend/common/name_scope.py @@ -54,12 +54,13 @@ def __enter__(self): return self def __exit__(self, *args, **kwargs): - if self._pop_on_exit: - name_scope_stack = global_state.get_global_attribute( - "name_scope_stack" - ) - if name_scope_stack: - name_scope_stack.pop() + if self._pop_on_exit: + name_scope_stack = global_state.get_global_attribute( + "name_scope_stack" + ) + if name_scope_stack: + name_scope_stack.pop() + def current_path(): name_scope_stack = global_state.get_global_attribute("name_scope_stack") diff --git a/keras/src/backend/common/name_scope_test.py b/keras/src/backend/common/name_scope_test.py index 2e79f2146958..79d9472638e0 100644 --- a/keras/src/backend/common/name_scope_test.py +++ b/keras/src/backend/common/name_scope_test.py @@ -1,4 +1,5 @@ from keras.src import testing +from keras.src.backend.common import global_state from keras.src.backend.common.name_scope import current_path from keras.src.backend.common.name_scope import name_scope @@ -46,3 +47,53 @@ def test_override_parent(self): current_path(), "absolute/path/middle/inner" ) self.assertEqual(current_path(), "outer") + + def test_exit_with_empty_stack(self): + global_state.set_global_attribute("name_scope_stack", []) + + scope = name_scope("test") + scope._pop_on_exit = True + + try: + scope.__exit__() + success = True + except (AttributeError, IndexError): + success = False + + self.assertTrue(success) + + def test_exit_with_none_stack(self): + global_state.set_global_attribute("name_scope_stack", None) + + scope = name_scope("test") + scope._pop_on_exit = True + + try: + scope.__exit__() + success = True + except (AttributeError, IndexError): + success = False + + self.assertTrue(success) + + def test_exit_without_pop_on_exit(self): + global_state.set_global_attribute("name_scope_stack", ["dummy"]) + + scope = name_scope("test") + scope._pop_on_exit = False + + scope.__exit__() + + name_scope_stack = global_state.get_global_attribute("name_scope_stack") + self.assertEqual(len(name_scope_stack), 1) + + def test_normal_exit_still_works(self): + self.assertEqual(current_path(), "") + + with name_scope("test1"): + self.assertEqual(current_path(), "test1") + with name_scope("test2"): + self.assertEqual(current_path(), "test1/test2") + self.assertEqual(current_path(), "test1") + + self.assertEqual(current_path(), "")