diff --git a/pydantic/utils.py b/pydantic/utils.py index 51c0253..a1c6192 100644 --- a/pydantic/utils.py +++ b/pydantic/utils.py @@ -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)