Skip to content

Commit

Permalink
Merge branch 'release/0.5.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
Xowap committed Jul 8, 2023
2 parents 68b879c + c3ed5bb commit a6b218a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 75 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.3
~~~~~~~~~~~~~

Minor bug fixes

Version 0.5.2
~~~~~~~~~~~~~

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.2"
version = "0.5.3"

description = "Fits JSON values into Python type-anotated objects"
license = "WTFPL"
Expand Down
72 changes: 0 additions & 72 deletions requirements.txt

This file was deleted.

4 changes: 2 additions & 2 deletions src/typefit/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def _fit_literal(self, t: Type[T], value: Node) -> T:
value.fit_success = True
return value.value

raise value.fail(f"No match in literal {t!r} (got {value!r})")
raise value.fail(f"No match in literal {t!r} (got {value.value!r})")

def fit_node(self, t: Type[T], value: Node) -> T:
"""
Expand Down Expand Up @@ -184,7 +184,7 @@ def fit_node(self, t: Type[T], value: Node) -> T:
return self._fit_union(t, value)
elif t is Any:
return self._fit_any(t, value)
elif isinstance(value, (MappingNode, ListNode)):
elif isinstance(value, (MappingNode, ListNode, LiteralNode)):
return value.fit(t)
elif t is None or t is None.__class__:
return self._fit_none(t, value)
Expand Down
31 changes: 31 additions & 0 deletions tests/issue_000050/test_context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import dataclass, field
from typing import Mapping

from pytest import raises

Expand All @@ -20,6 +21,17 @@ class Root:
_foo: int = field(metadata=meta(context="foo"))


@dataclass
class B:
val: int
foo: Mapping[str, str] = field(metadata=meta(context="foo"))


@dataclass
class A:
val: Mapping[str, Mapping[str, B]]


class TestErrorReporter(ErrorReporter):
def __init__(self):
self.formatter = PrettyJson5Formatter(colors="")
Expand Down Expand Up @@ -59,6 +71,25 @@ def test_root_injection():
assert x.child._root is x


def test_context_mapping_injection():
a: A = typefit(
A,
dict(
val=dict(
foo=dict(
bar=dict(
val=42,
)
),
),
),
context=dict(foo=dict(hello="world")),
)

assert a.val["foo"]["bar"].val == 42
assert a.val["foo"]["bar"].foo["hello"] == "world"


def test_context_injection_fail():
rep = TestErrorReporter()
fitter = Fitter(error_reporter=rep)
Expand Down

0 comments on commit a6b218a

Please sign in to comment.