Skip to content

Conversation

@jootten
Copy link
Contributor

@jootten jootten commented May 21, 2025

Description

For certain packages installed alongside xsdata entries in sys.modules are missing for classes from those packages. This leads to a KeyError when trying to get the module.

One of the nvidia packages pytorch depends on leads to this error for me. I dont know which one exactly.

Trace:

   return XmlParser().from_bytes(raw, cvat_xml.Annotations)
  File "/code/.venv/lib/python3.10/site-packages/xsdata/formats/dataclass/parsers/mixins.py", line 88, in from_bytes
    return self.parse(io.BytesIO(source), clazz, ns_map)
  File "/code/.venv/lib/python3.10/site-packages/xsdata/formats/dataclass/parsers/bases.py", line 58, in parse
    result = handler.parse(source, ns_map)
  File "/code/.venv/lib/python3.10/site-packages/xsdata/formats/dataclass/parsers/handlers/lxml.py", line 42, in parse
    return self.process_context(ctx, ns_map)
  File "/code/.venv/lib/python3.10/site-packages/xsdata/formats/dataclass/parsers/handlers/lxml.py", line 60, in process_context
    self.parser.start(
  File "/code/.venv/lib/python3.10/site-packages/xsdata/formats/dataclass/parsers/bases.py", line 94, in start
    child = item.child(qname, attrs, ns_map, len(objects))
  File "/code/.venv/lib/python3.10/site-packages/xsdata/formats/dataclass/parsers/nodes/element.py", line 459, in child
    node = self.build_node(qname, var, attrs, ns_map, position)
  File "/code/.venv/lib/python3.10/site-packages/xsdata/formats/dataclass/parsers/nodes/element.py", line 559, in build_node
    clazz = self.context.find_type(qname)
  File "/code/.venv/lib/python3.10/site-packages/xsdata/formats/dataclass/context.py", line 168, in find_type
    types: list[type] = self.find_types(qname)
  File "/code/.venv/lib/python3.10/site-packages/xsdata/formats/dataclass/context.py", line 153, in find_types
    self.build_xsi_cache()
  File "/code/.venv/lib/python3.10/site-packages/xsdata/formats/dataclass/context.py", line 112, in build_xsi_cache
    meta = builder.build_class_meta(clazz)
  File "/code/.venv/lib/python3.10/site-packages/xsdata/formats/dataclass/models/builders.py", line 248, in build_class_meta
    module = sys.modules[clazz.__module__]
KeyError: ''

🔗 What I've Done

Added a simple check whether the clazz.module exists in sys.modules.

🛫 Checklist

I think there is no need to update docs or tests for this small fix.

@codecov
Copy link

codecov bot commented May 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.94%. Comparing base (2512c1f) to head (22f236b).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1144   +/-   ##
=======================================
  Coverage   99.94%   99.94%           
=======================================
  Files         116      116           
  Lines        9370     9370           
  Branches     1430     1430           
=======================================
  Hits         9365     9365           
  Misses          5        5           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jootten jootten changed the title Fix: KeyError for certain modules Fix: KeyError in XmlMetaBuilder.build_class_meta for certain packages May 21, 2025
@tefra
Copy link
Owner

tefra commented May 22, 2025

Let's add this to the rest of the exceptions here please

https://github.com/tefra/xsdata/blob/main/xsdata/formats/dataclass/context.py#L135

@jootten jootten force-pushed the fix-build-class-meta branch from 0797b0c to 22f236b Compare May 26, 2025 08:51
@sonarqubecloud
Copy link

Copy link
Owner

@tefra tefra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jootten

@tefra tefra merged commit eaaa9c6 into tefra:main Jun 5, 2025
22 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants