FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.15] gh-158936: Resolve lazy imports consistently for dataclass annotations (GH-158939) by miss-islington · Pull Request #158946 · python/cpython · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (4) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 5 additions & 1 deletion Lib/dataclasses.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,11 @@ def _get_type_from_annotation(annotation, cls):
path = match[1].split(".")
root = sys.modules.get(cls.__module__)
for path_item in path:
root = getattr(root, path_item.strip(), None)
try:
root = getattr(root, path_item.strip(), None)
except Exception:
# Like annotationlib, ignore lazy imports that cannot be resolved.
return None
if root is None:
return None

Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_dataclasses/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4525,6 +4525,11 @@ def test_classvar_module_level_import(self):
# won't exist on the instance.
self.assertNotIn('not_iv4', c.__dict__)

def test_lazy_import_annotations(self):
from test.test_dataclasses import dataclass_lazy_str as m
self.assertEqual([f.name for f in fields(m.C)], ['x', 'y'])
self.assertNotIsInstance(vars(m)['t'], types.LazyImportType)

def test_text_annotations(self):
from test.test_dataclasses import dataclass_textanno

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
raise RuntimeError("module fails to import")
12 changes: 12 additions & 0 deletions Lib/test/test_dataclasses/dataclass_lazy_str.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from __future__ import annotations

import dataclasses
lazy import typing as t
lazy from _dataclass_test_missing_module import Missing
lazy from test.test_dataclasses.dataclass_lazy_broken import Broken

@dataclasses.dataclass
class C:
x: Missing | None = None
y: Broken | None = None
cv: t.ClassVar[int] = 0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:func:`~dataclasses.dataclass` no longer fails when a string annotation names a
:ref:`lazy import <lazy-imports>` that cannot be resolved, matching the
behavior for non-string annotations.
Loading

Back | FazBrowse Home | New Git URL