44
55import os
66import sys
7- from typing import (Callable , Dict , NoReturn , Tuple , Union , Any , Iterator , # noqa: F401
7+ from typing import (Callable , Dict , NoReturn , Sequence , Tuple , Union , Any , Iterator , # noqa: F401
88 NamedTuple , TYPE_CHECKING , TypeVar ) # noqa: F401
99
1010if TYPE_CHECKING :
3737Tree_ish = Union ['Commit' , 'Tree' ]
3838Commit_ish = Union ['Commit' , 'TagObject' , 'Blob' , 'Tree' ]
3939
40+ # Config_levels ---------------------------------------------------------
41+
4042Lit_config_levels = Literal ['system' , 'global' , 'user' , 'repository' ]
4143
4244
@@ -47,12 +49,25 @@ def is_config_level(inp: str) -> TypeGuard[Lit_config_levels]:
4749
4850ConfigLevels_Tup = Tuple [Literal ['system' ], Literal ['user' ], Literal ['global' ], Literal ['repository' ]]
4951
52+ #-----------------------------------------------------------------------------------
53+
54+
55+ def assert_never (inp : NoReturn , raise_error : bool = True , exc : Union [Exception , None ] = None ) -> None :
56+ """For use in exhaustive checking of literal or Enum in if/else chain.
57+ Should only be reached if all memebers not handled OR attempt to pass non-members through chain.
58+
59+ If all members handled, type is Empty. Otherwise, will cause mypy error.
60+ If non-members given, should cause mypy error at variable creation.
5061
51- def assert_never (inp : NoReturn , exc : Union [Exception , None ] = None ) -> NoReturn :
52- if exc is None :
53- assert False , f"An unhandled Literal ({ inp } ) in an if/else chain was found"
62+ If raise_error is True, will also raise AssertionError or the Exception passed to exc.
63+ """
64+ if raise_error :
65+ if exc is None :
66+ raise ValueError (f"An unhandled Literal ({ inp } ) in an if/else chain was found" )
67+ else :
68+ raise exc
5469 else :
55- raise exc
70+ pass
5671
5772
5873class Files_TD (TypedDict ):
0 commit comments