Skip to content

Commit d7c382a

Browse files
khsraliagoscinski
andcommitted
A new common funcion:assert_never to assert certain part of the code is never reached.
Following instructions of typing and mypy: https://typing.readthedocs.io/en/latest/guides/unreachable.html#assert-never-and-exhaustiveness-checking https://mypy.readthedocs.io/en/stable/literal_types.html#id3 Co-authored-by: Alexander Goscinski <alex.goscinski@posteo.de>
1 parent c535928 commit d7c382a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/aiida/common/asserts.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
###########################################################################
2+
# Copyright (c), The AiiDA team. All rights reserved. #
3+
# This file is part of the AiiDA code. #
4+
# #
5+
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core #
6+
# For further information on the license, see the LICENSE.txt file #
7+
# For further information please visit http://www.aiida.net #
8+
###########################################################################
9+
"""Module with asserts that can be used in the codebase."""
10+
11+
from typing import Optional
12+
13+
try:
14+
from typing import Never
15+
except ImportError:
16+
# Fallback for Python 3.10 and older
17+
from typing_extensions import Never
18+
19+
20+
def assert_never(arg: Never, message: Optional[str] = None) -> Never:
21+
"""Assert a part of code that should never be reached.
22+
This is useful, especially when adding new variables to an enum,
23+
so to remind the developers to handle the new variable in all the switch cases.
24+
"""
25+
26+
raise AssertionError('This part of code should never be reached' if not message else message)

0 commit comments

Comments
 (0)