Dacite throws an error while using Optional on NewType
See original GitHub issueExample
import dataclasses
from typing import NewType, Optional
CUSTOM_TYPE = NewType('CUSTOM_TYPE', str)
@dataclasses.dataclass
class Test:
usual_field: str
custom_type_field: CUSTOM_TYPE
optional_custom_type_field: Optional[CUSTOM_TYPE]
test = Test('usual', CUSTOM_TYPE('custom'), CUSTOM_TYPE('optional_custom'))
test_dict = dataclasses.asdict(test)
dacite.from_dict(Test, test_dict)
Output
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-22-c59ddd955c43> in <module>
8 test = Test('usual', CUSTOM_TYPE('custom'))
9 test_dict = dataclasses.asdict(test)
---> 10 dacite.from_dict(Test, test_dict)
.venv/lib/python3.7/site-packages/dacite/core.py in from_dict(data_class, data, config)
43 error.update_path(field.name)
44 raise
---> 45 if config.check_types and not is_instance(value, field.type):
46 raise WrongTypeError(
47 field_path=field.name,
.venv/lib/python3.7/site-packages/dacite/types.py in is_instance(value, t)
53 elif is_union(t):
54 types = tuple(extract_origin_collection(t) if is_generic(t) else t for t in extract_generic(t))
---> 55 return isinstance(value, types)
56 elif is_generic_collection(t):
57 return isinstance(value, extract_origin_collection(t))
TypeError: isinstance() arg 2 must be a type or tuple of types
This is happens only if I use Optional[NewType]
. Without Optional
dacite works like charm.
>>> dataclasses.fields(Test)[0].type, dataclasses.fields(Test)[1].type, dataclasses.fields(Test)[2].type
(str,
<function typing.NewType.<locals>.new_type(x)>,
typing.Union[CUSTOM_TYPE, NoneType])
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
12 recipes for using the Optional class as it's meant to be used
Therefore, in Java 8, a new type was added called Optional<T> , which indicates the presence or absence of a value of type...
Read more >Java Optional Tutorial with Examples - CalliCoder
Java 8 introduced a new type called Optional<T> to help developers ... You can use orElseThrow() to throw an exception if Optional is...
Read more >How can I return an optional instead of throwing an exception?
You can use code like this to wrap code that returns a value with something that captures exceptions and returns an empty optional:...
Read more >Intention Revealing Code With Optional - nipafx.dev
Write intention revealing code with Java 8's new type Optional and ... than using Optional - it just claims that Optional beats null...
Read more >10 Java Optional Best Practices - CLIMB
1. Do not use Optional as a return type if the method can return null · 2. Use isPresent() to check for values...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@AlwxSin thank you for reporting this issue! It’s definitely a bug. I’ve fixed it in the latest commit - 574015d
@GreyZmeem thank you for your suggestion - this was a problematic function.
@GreyZmeem hmm, interesting, I will check.