From 62b3577c8e496038300ef38a99f43b5e1c3ef002 Mon Sep 17 00:00:00 2001 From: Luke Harold Miles Date: Thu, 25 Jul 2019 09:41:46 -0400 Subject: [PATCH] Workaround methods for mypy type inference problems with converters See https://github.com/python/mypy/issues/5738 and https://github.com/python-attrs/attrs/issues/519 --- immutablecollections/converter_utils.py | 63 +++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 immutablecollections/converter_utils.py diff --git a/immutablecollections/converter_utils.py b/immutablecollections/converter_utils.py new file mode 100644 index 0000000..3757a0f --- /dev/null +++ b/immutablecollections/converter_utils.py @@ -0,0 +1,63 @@ +from typing import Any, Iterable, Mapping, Optional, Tuple, Union + +from immutablecollections import ( + ImmutableDict, + ImmutableListMultiDict, + ImmutableSet, + ImmutableSetMultiDict, + immutabledict, + immutablelistmultidict, + immutableset, + immutablesetmultidict, +) + + +def _to_tuple(val: Iterable[Any]) -> Tuple[Any, ...]: + """Needed until https://github.com/python/mypy/issues/5738 + and https://github.com/python-attrs/attrs/issues/519 are fixed. + """ + return tuple(val) + + +def _to_immutableset(val: Optional[Iterable[Any]]) -> ImmutableSet[Any]: + """Needed until https://github.com/python/mypy/issues/5738 + and https://github.com/python-attrs/attrs/issues/519 are fixed. + """ + return immutableset(val) + + +def _to_immutabledict( + val: Optional[ + Union[Iterable[Tuple[Any, Any]], Mapping[Any, Any], ImmutableDict[Any, Any]] + ] +) -> ImmutableDict[Any, Any]: + """Needed until https://github.com/python/mypy/issues/5738 + and https://github.com/python-attrs/attrs/issues/519 are fixed. + """ + return immutabledict(val) + + +def _to_immutablesetmultidict( + val: Optional[ + Union[ + Iterable[Tuple[Any, Any]], Mapping[Any, Any], ImmutableSetMultiDict[Any, Any] + ] + ] +) -> ImmutableSetMultiDict[Any, Any]: + """Needed until https://github.com/python/mypy/issues/5738 + and https://github.com/python-attrs/attrs/issues/519 are fixed. + """ + return immutablesetmultidict(val) + + +def _to_immutablelistmultidict( + val: Optional[ + Union[ + Iterable[Tuple[Any, Any]], Mapping[Any, Any], ImmutableListMultiDict[Any, Any] + ] + ] +) -> ImmutableListMultiDict[Any, Any]: + """Needed until https://github.com/python/mypy/issues/5738 + and https://github.com/python-attrs/attrs/issues/519 are fixed. + """ + return immutablelistmultidict(val)