Skip to content

Commit

Permalink
Merge pull request #30 from kodemore/fix-union-force-mode
Browse files Browse the repository at this point in the history
Add non-greedy union support
  • Loading branch information
dkraczkowski authored Nov 14, 2023
2 parents e537a8f + 3981594 commit 4af8bee
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8, 3.9]
python-version: ["3.8", "3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v2
Expand Down
28 changes: 22 additions & 6 deletions chili/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,30 @@ def decode(self, value: Any) -> Any:

if passed_type is dict:
provided_fields = set(value.keys())
for class_name, decoder in self._type_decoders.items():
if self.force:
decoders = self._type_decoders
else:
decoders = {
class_name: decoder
for class_name, decoder in self._type_decoders.items()
if is_decodable(class_name) or is_dataclass(class_name)
}
# Greedy matching
for class_name, decoder in decoders.items():
expected_fields = set(get_non_optional_fields(class_name))
if not provided_fields.issubset(expected_fields):
continue
try:
if is_decodable(class_name) or is_dataclass(class_name) or self.force:
expected_fields = set(get_non_optional_fields(class_name))
if provided_fields.issubset(expected_fields):
return decoder.decode(value)
continue
return decoder.decode(value)
except Exception:
continue
# Non-greedy matching
for class_name, decoder in decoders.items():
expected_fields = set(get_non_optional_fields(class_name))
if not expected_fields.issubset(provided_fields):
continue
try:
return decoder.decode(value)
except Exception:
continue

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ license = "MIT"
name = "chili"
readme = "README.md"
repository = "https://github.com/kodemore/chili"
version = "2.8.1"
version = "2.8.2"

[tool.poetry.dependencies]
gaffe = ">=0.3.0"
Expand Down

0 comments on commit 4af8bee

Please sign in to comment.