Adds assert_never helper (#3294)

* Adds `assert_never` helper

* Update utils.py

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
This commit is contained in:
Nikita Sobolev
2022-08-09 12:15:04 +03:00
committed by GitHub
parent f6c74a55d5
commit 10508f7259
+15 -1
View File
@@ -18,6 +18,7 @@ from typing import (
List,
Mapping,
MutableMapping,
NoReturn,
Optional,
Set,
Tuple,
@@ -606,7 +607,10 @@ class ValueItems(Representation):
items = dict.fromkeys(items, ...)
else:
class_name = getattr(items, '__class__', '???')
raise TypeError(f'Unexpected type of exclude value {class_name}')
assert_never(
items,
f'Unexpected type of exclude value {class_name}',
)
return items
@classmethod
@@ -726,6 +730,16 @@ def all_identical(left: Iterable[Any], right: Iterable[Any]) -> bool:
return True
def assert_never(obj: NoReturn, msg: str) -> NoReturn:
"""
Helper to make sure that we have covered all possible types.
This is mostly useful for ``mypy``, docs:
https://mypy.readthedocs.io/en/latest/literal_types.html#exhaustive-checks
"""
raise TypeError(msg)
def get_unique_discriminator_alias(all_aliases: Collection[str], discriminator_key: str) -> str:
"""Validate that all aliases are the same and if that's the case return the alias"""
unique_aliases = set(all_aliases)