diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index b98f21dcbe9220..3e94a7234a3a7b 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -759,12 +759,16 @@ def _is_type(annotation, cls, a_module, a_type, is_type_predicate): if not module_name: # No module name, assume the class's module did # "from dataclasses import InitVar". - ns = sys.modules.get(cls.__module__).__dict__ + module = sys.modules.get(cls.__module__) + if module: + ns = module.__dict__ else: # Look up module_name in the class's module. module = sys.modules.get(cls.__module__) if module and module.__dict__.get(module_name) is a_module: - ns = sys.modules.get(a_type.__module__).__dict__ + module = sys.modules.get(a_type.__module__) + if module: + ns = module.__dict__ if ns and is_type_predicate(ns.get(match.group(2)), a_module): return True return False diff --git a/Misc/NEWS.d/next/Library/2025-10-28-14-36-29.gh-issue-140704.WNP7iG.rst b/Misc/NEWS.d/next/Library/2025-10-28-14-36-29.gh-issue-140704.WNP7iG.rst new file mode 100644 index 00000000000000..ee0c802774dc1c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-10-28-14-36-29.gh-issue-140704.WNP7iG.rst @@ -0,0 +1,2 @@ +Fix :exc:`AttributeError` in :mod:`dataclasses` when using complex +annotation with a compiled class.