From fdd06b68e6800a9d43d50cfea9afcca4c2efc27d Mon Sep 17 00:00:00 2001 From: Jonas Otten Date: Fri, 17 Apr 2026 15:07:40 +0200 Subject: [PATCH] Fix KeyError in is_binding_model when models_package is not set #1144 added a sys.modules check to avoid KeyError, but it was only reached when models_package was configured. Without it, the original KeyError still came back. Move the check out so it runs in both cases. --- xsdata/formats/dataclass/context.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/xsdata/formats/dataclass/context.py b/xsdata/formats/dataclass/context.py index 9707400f..28e1d82e 100644 --- a/xsdata/formats/dataclass/context.py +++ b/xsdata/formats/dataclass/context.py @@ -132,13 +132,19 @@ def is_binding_model(self, clazz: type[T]) -> bool: if not self.class_type.is_model(clazz): return False - return not self.models_package or ( + has_valid_module = ( hasattr(clazz, "__module__") and isinstance(clazz.__module__, str) - and clazz.__module__.startswith(self.models_package) and clazz.__module__ in sys.modules ) + if not has_valid_module: + return False + + return not self.models_package or clazz.__module__.startswith( + self.models_package + ) + def find_types(self, qname: str) -> list[type[T]]: """Find all classes that match the given xsi:type qname.