Skip to content

Commit

Permalink
Merge branch 'release/0.5.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
Xowap committed Jul 10, 2023
2 parents cb4c264 + 0637baf commit 347bb4d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
5 changes: 5 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Pre-1.0 Releases

Before version 1.0, everything is experimental and can change at any time.

Version 0.5.5
~~~~~~~~~~~~~

Disregard field type when injecting

Version 0.5.4
~~~~~~~~~~~~~

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]

name = "typefit"
version = "0.5.4"
version = "0.5.5"

description = "Fits JSON values into Python type-anotated objects"
license = "WTFPL"
Expand Down
4 changes: 3 additions & 1 deletion src/typefit/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,13 @@ def fit_node(self, t: Type[T], value: Node) -> T:

origin = get_origin(t)

if isinstance(value, LiteralNode):
return value.fit(t)
if origin is Union or origin is UnionType:
return self._fit_union(t, value)
elif t is Any:
return self._fit_any(t, value)
elif isinstance(value, (MappingNode, ListNode, LiteralNode)):
elif isinstance(value, (MappingNode, ListNode)):
return value.fit(t)
elif t is None or t is None.__class__:
return self._fit_none(t, value)
Expand Down
20 changes: 19 additions & 1 deletion tests/issue_000050/test_context.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass, field
from typing import Mapping
from typing import Any, Mapping

from pytest import raises

Expand Down Expand Up @@ -32,6 +32,12 @@ class A:
val: Mapping[str, Mapping[str, B]]


@dataclass
class DoubleFit:
a: int = field(metadata=meta(context="a"))
b: Any = field(metadata=meta(context="b"))


class TestErrorReporter(ErrorReporter):
def __init__(self):
self.formatter = PrettyJson5Formatter(colors="")
Expand Down Expand Up @@ -114,3 +120,15 @@ def test_context_injection_fail():
},
}"""
)


def test_double_fit():
context = dict(a=1, b=2)
x: DoubleFit = typefit(
DoubleFit,
dict(),
context=context,
)

assert x.a == 1
assert x.b == 2

0 comments on commit 347bb4d

Please sign in to comment.