Skip to content

Commit

Permalink
Workaround methods for mypy type inference problems with converters
Browse files Browse the repository at this point in the history
  • Loading branch information
qpwo authored and Ryan Gabbard committed Jul 25, 2019
1 parent f198390 commit 62b3577
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions immutablecollections/converter_utils.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 62b3577

Please sign in to comment.