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

Union type is not supported #267

Open
tsuga opened this issue Jul 28, 2022 · 3 comments
Open

Union type is not supported #267

tsuga opened this issue Jul 28, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@tsuga
Copy link

tsuga commented Jul 28, 2022

I'm seeing an error when using Union type.

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)
[<ipython-input-4-9790d08ed9c3>](https://localhost:8080/#) in <module>()
     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:
@tsuga
Copy link
Author

tsuga commented Jul 28, 2022

#239 might be related.

@jacebrowning
Copy link
Owner

jacebrowning commented Jul 28, 2022

For classes under your control, Number should work instead of Union[int, float].

How would you expect serialization to work for disparate types such as Union[str, bool, float]?

@jacebrowning jacebrowning added the bug Something isn't working label Jul 28, 2022
@jacebrowning
Copy link
Owner

While it's not full support for Union types, #272 will handle Union[str, ...] (treated as the broader str type) and Union[int, float] (treated as the broader float type).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants