Coverage for cc_modules/cc_pythonversion.py: 44%

1"""

2 camcops_server/cc_modules/cc_pythonversion.py

3

4===============================================================================

5

6 Copyright (C) 2012, University of Cambridge, Department of Psychiatry.

7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).

8

9 This file is part of CamCOPS.

10

11 CamCOPS is free software: you can redistribute it and/or modify

12 it under the terms of the GNU General Public License as published by

13 the Free Software Foundation, either version 3 of the License, or

14 (at your option) any later version.

15

16 CamCOPS is distributed in the hope that it will be useful,

17 but WITHOUT ANY WARRANTY; without even the implied warranty of

18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

19 GNU General Public License for more details.

20

21 You should have received a copy of the GNU General Public License

22 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.

23

24===============================================================================

25

26**Single place to determine the Python version required.**

27

28 Python v3.6 was required as of CamCOPS v2.3.1, 2019. That enables:

29

30- f-strings (v3.6)

31

32 Python v3.7 was required as of CamCOPS v2.4.12, 2021. That enables:

33

34- dataclasses (v3.7)

35

36 Python 3.8 was required as of CamCOPS v2.4.15, 2022. That enables:

37

38- assignment expressions, the "walrus" operator, ``:=`` (v3.8)

39- positional-only parameters, ``/`` (v3.8)

40- f-string ``=`` syntax to debug a variable (v3.8)

41

42 Not yet available:

43

44- new dictionary merge/update syntax (v3.9)

45- string prefix/suffix removal functions (v3.9)

46- use of generics like ``list`` (not just ``List``) for type hinting (v3.9)

47

48- ``match/case`` statement, like C++'s ``switch`` (v3.10)

49- ``|`` as well as ``Union`` for type hints (v3.10)

50- explicit ``typing.TypeAlias`` annotation (v3.10)

51

52 Note that one can set the environment variable ``PYTHONDEVMODE=1`` to enable

53 extra checks, such as whether there are deprecation warnings with newer Python

54 versions.

55

56 Note that Python versions are referred to in:

57

58- this file

59- ``.github/workflows/*``

60- ``server/setup.py``

61- ``server/docker/dockerfiles/camcops.Dockerfile``

62- ``server/tools/MAKE_LINUX_PACKAGES.py``

63

64 and separately (not necessarily within a CamCOPS virtual environment) in

65

66- ``server/tools/install_virtualenv.py``

67- ``tablet_qt/tools/build_qt.py``

68

69"""

70

71 import sys

72

73 MINIMUM_PYTHON_VERSION = (3, 8)

74

75

76 def assert_minimum_python_version() -> None:

77 """

78 Asserts that this version of Python meets our minimum requirements.

79 This function should be used except in installation environments where

80 CamCOPS modules are unavailable.

81

82 Raises:

83 AssertionError

84

85 Note that this module/function should use only Python 2 syntax!

86

87 """

88 if sys.version_info < MINIMUM_PYTHON_VERSION:

89 required = ".".join(str(x) for x in MINIMUM_PYTHON_VERSION)

90 actual = ".".join(str(x) for x in sys.version_info)

91 raise AssertionError(

92 "Need Python %s or higher; this is %s" % (required, actual)

93 )

94

95

96 if __name__ == "__main__":

97 assert_minimum_python_version()