jacebrowning / jacebrowning/datafiles
Arbitrary union type is not supported
- Dominant language
- Python
- Stars
- 213
- Forks
- 23
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 5
Description
I'm seeing an error when using Union type.
```py
from typing import Union
from datafiles import datafile, Missing
@datafile("inventory/items/{self.name}.yml")
class InventoryItem:
"""Class for keeping track of an item in inventory."""
name: str
unit_price: float
quantity_on_hand: Union[int, float] = 0
item = InventoryItem("widget", 3)
```
```
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
[](https://localhost:8080/#) in ()
10 quantity_on_hand: Union[int, float] = 0.0
11
---> 12 item = InventoryItem("widget", 3)
3 frames
[/usr/local/lib/python3.7/dist-packages/datafiles/converters/__init__.py](https://localhost:8080/#) in map_type(cls, name, item_cls)
134 converter = map_type(cls.__args__[0])
135 assert len(cls.__args__) == 2
--> 136 assert cls.__args__[1] == type(None)
137 converter = converter.as_optional()
138
AssertionError:
````
Contributor guide
Research direction
Reproduce the example and inspect datafiles/converters/__init__.py, especially map_type where the Union arguments are asserted. Determine the intended handling for Union[int, float], then add coverage for the reported annotation and verify that creating the InventoryItem no longer raises AssertionError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100