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

Parse error when StrEnum is unioned with int in dataclass field #118

Closed
BrendanMartin opened this issue Jun 23, 2024 · 1 comment
Closed

Comments

@BrendanMartin
Copy link

  • Dataclass Wizard version: 0.22.3
  • Python version: 3.11
  • Operating System: Windows 10

Description

I'm trying to have a dataclass field take on either a StrEnum or an int. Using from_dict() works when StrEnum is used, but unioned with int throws a parse error.

What I Did

In the examples below, Example works but Example2 throws a parse error:

class Value(StrEnum):
    DEFAULT = 'default'

@dataclass
class Example(JSONWizard):
    test: Value

# Works
d = {'test': 'default'}
example = Example.from_dict(d)


@dataclass
class Example2(JSONWizard):
    test: Value | int

# Error
d2 = {'test': 'default'}
example2 = Example2.from_dict(d2)

Here's the error output:

>       raise ParseError(
            TypeError('Object was not in any of Union types'),
            o, [p.base_type for p in self.parsers],
            tag_key=self.tag_key
        )
E       dataclass_wizard.errors.ParseError: Failure parsing field `test` in class `Example2`. Expected a type [<enum 'Value'>, <class 'int'>], got str.
E         value: 'default'
E         error: Object was not in any of Union types
E         tag_key: '__tag__'
E         json_object: '{"test": "default"}'
@rnag
Copy link
Owner

rnag commented Nov 4, 2024

It's a known issue. I commented on #67, which is AFAIK the same problem.

As it's the same issue, I am going to close this in favor of above. Will add a note that Union with StrEnum / Enum behaves the same way.

@rnag rnag closed this as completed Nov 4, 2024
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

No branches or pull requests

2 participants