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

Serialization strategy for TypeVar does not work in BaseConfig #61

Closed
timrulebosch opened this issue Dec 10, 2021 · 3 comments
Closed
Labels
bug Something isn't working

Comments

@timrulebosch
Copy link

I find when I define a TypeVar variables of that type are always serialized with the first type listed in the TypeVar definition.

In an attempt to fix that I tired to set a global serialization strategy, however that strategy was never selected, rather, the selection seems to be also based on the first type listed in the TypeVar. Quite a bit of the code in metaprograming.py seems to handle a TypeVar so I wonder if I have something else missing?

The basic code is:

Numeric = TypeVar('Numeric', float, int)  # <== here float is first in the list, seems to be selected as the key for serialization strategy.

class NumericStrategy(SerializationStrategy):
    def serialize(self, value):
        return int(value)

    def deserialize(self, value):
        return int(value)

@dataclass
class foo(DataClassYAMLMixin):
    bar: Numeric = 1
    class Config(BaseConfig):
            serialization_strategy = {
                Numeric: NumericStrategy(),  # <== this is the line I think should activate the strategy
                int: NumericStrategy(),
                #float: NumericStrategy(),  # <== it seems that only this line would activate the strategy
            }
@Fatal1ty
Copy link
Owner

Hi @timrulebosch

Seems like a bug, thank you for spotting it! I'll see what I can do with it.

@Fatal1ty Fatal1ty added the bug Something isn't working label Dec 10, 2021
@Fatal1ty
Copy link
Owner

@timrulebosch I fixed it in a develop branch. You can check it and give me a feedback.

@timrulebosch
Copy link
Author

Works! Very nice.

Thanks.

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