Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non-greedy union support #30

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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