Skip to content

Commit 69a10e6

Browse files
NiklasRosensteinGitHub Action
andauthored
databind.json/: fix: Evaluate forward references when creating schema for NamedTuples (#50)
* databind.json/: fix: Evaluate forward references when creating schema for NamedTuples * Updated PR references in 1 changelogs. skip-checks: true --------- Co-authored-by: GitHub Action <[email protected]>
1 parent efa7745 commit 69a10e6

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[[entries]]
2+
id = "eee787ce-d9d3-442e-acee-b274edc72176"
3+
type = "fix"
4+
description = "Evaluate forward references when creating schema for NamedTuples"
5+
author = "@NiklasRosenstein"
6+
pr = "https://github.com/NiklasRosenstein/python-databind/pull/50"

databind.json/src/databind/json/converters.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ def convert(self, ctx: Context) -> t.Any:
9797
schema = Schema(
9898
fields={
9999
name: Field(
100-
datatype=TypeHint(type_),
100+
# We need to evaluate the type hint to remove forward references. The source is needed to
101+
# understand the context in which forward references must be evaluated.
102+
datatype=TypeHint(type_, source=datatype.type).evaluate(),
101103
)
102104
for name, type_ in getattr(datatype.type, "__annotations__").items()
103105
},

databind.json/src/databind/json/tests/converters_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ def test__typing_NamedTuple() -> None:
539539

540540
class Nt(t.NamedTuple):
541541
a: int
542-
b: str
542+
b: "str"
543543

544544
assert mapper.serialize(Nt(1, "2"), Nt) == {"a": 1, "b": "2"}
545545
assert mapper.deserialize({"a": 1, "b": "2"}, Nt) == Nt(1, "2")

0 commit comments

Comments
 (0)