Skip to content

Add Generic Hook Types#83

Open
channelcat wants to merge 2 commits intokonradhalas:masterfrom
channelcat:master
Open

Add Generic Hook Types#83
channelcat wants to merge 2 commits intokonradhalas:masterfrom
channelcat:master

Conversation

@channelcat
Copy link

Added in the ability to hook all of a major type, and added tests/docs. From the Readme:

To target all types use Any. Targeting collections without their sub-types
will target all collections of those types, such as List and Dict.

@dataclass
class ShoppingCart:
    store: str
    item_ids: List[int]

data = {
    'store': '7-Eleven',
    'item_ids': [1, 2, 3],
}

def print_value(value):
    print(value)
    return value

def print_collection(collection):
    for item in collection:
        print(item)
    return collection

result = from_dict(
    data_class=ShoppingCart, 
    data=data, 
    config=Config(
        type_hooks={
            Any: print_value, 
            List: print_collection
        }
    )
)

prints

7-Eleven
[1, 2, 3]
1
2
3

@konradhalas
Copy link
Owner

Hi @channelcat - thank you for PR :)

It looks that it makes sense - let me think about it for a moment. I'll try to review your code during my next "open source sprint" ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants