Question presented to candidate:
"typing.assert_never takes a parameter of type Never. What is Never, and why does annotating that one parameter let a type checker prove a match statement covers every case?"
What a strong answer should cover:
Neveris the bottom type: the type with no values. A function returningNevernever returns normally (it raises, exits or loops forever); a parameter of typeNevercan accept no argument.Neverwas added in Python 3.11 (typing_extensionsbefore);NoReturnis older and checkers treat the two as the same type. At runtime they are distinct special forms that cannot be instantiated or used withisinstance.- Narrowing: the checker subtracts each handled type from the subject's declared union; once everything is handled, the remaining type is
Neverand that branch is unreachable. assert_never(arg: Never) -> Neverturns "the leftover type is notNever" into an argument-type error that names exactly the unhandled type; when the match is exhaustive the call is fine (and mypy does not flag it as unreachable).- Because a call returning
Neverends the path, code after it is unreachable and functions likefail()orsys.exit()satisfy any declared return type.
Code / implementation expected: Yes β a match over a union ending in case _: assert_never(x), and the checker output with and without the missing case.