Skip to content
New issue

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

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

Already on GitHub? # to your account

Add Generic Hook Types #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

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" ;)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants