mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
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:
+15
-1
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user