Refactor Union conversion.
Do None to NoneType conversion only once when getting types. It seems
`typing.get_type_hints` does that for us but we still need to handle
cases like `@keyword` where types are given directly.
@@ -422,7 +422,7 @@ class CombinedConverter(TypeConverter):
type = Union
def __init__(self, union=None):
- self.types = self._get_types(union)
+ self.types = self._none_to_nonetype(self._get_types(union))
def _get_types(self, union):
if not union:
@@ -437,6 +437,9 @@ class CombinedConverter(TypeConverter):
# when Python 3.5 support is dropped
return union.__union_params__
+ def _none_to_nonetype(self, types):
+ return tuple(t if t is not None else type(None) for t in types)
+
@property
def type_name(self):
return ' or '.join(type_name(t) for t in self.types) if self.types else None
@@ -451,9 +454,7 @@ class CombinedConverter(TypeConverter):
return True
def _no_conversion_needed(self, value):
- NoneType = type(None)
- types = tuple(t if t is not None else NoneType for t in self.types)
- return isinstance(value, types)
+ return isinstance(value, self.types)
def _convert(self, value, explicit_type=True):
for typ in self.types:
