File tree Expand file tree Collapse file tree 4 files changed +67
-2
lines changed
Expand file tree Collapse file tree 4 files changed +67
-2
lines changed Original file line number Diff line number Diff line change 2222python3 -m pip install -U " betterproto[compiler]"
2323```
2424
25+ !!! warning
26+ Make sure that the proto files were generated with a version of ` betterproto2_compiler ` that is compatible with your
27+ version of ` betterproto2 ` .
28+
29+ The version `x.y.z` of `betterproto` is compatible with the version `a.b.c` of the compiler if and only if `a=x` and
30+ `b=y`.
31+
2532## Compiling proto files
2633
2734
Original file line number Diff line number Diff line change 11from __future__ import annotations
22
3- __all__ = ["__version__" ]
3+ __all__ = ["__version__" , "check_compiler_version" ]
44
55import dataclasses
66import enum as builtin_enum
4444from typing_extensions import Self
4545
4646from ._types import T
47- from ._version import __version__
47+ from ._version import __version__ , check_compiler_version
4848from .casing import (
4949 camel_case ,
5050 safe_snake_case ,
Original file line number Diff line number Diff line change 11from importlib import metadata
22
33__version__ = metadata .version ("betterproto2" )
4+
5+
6+ def check_compiler_version (compiler_version : str ) -> None :
7+ """
8+ Checks that the compiled files can be used with this version of the library.
9+
10+ If the versions do not match, the user is suggested to update the library or the compiler. The version x.y.z of the
11+ library matches the version a.b.c of the compiler if and only if a=x and b=y.
12+ """
13+ parsed_lib_version = tuple (int (x ) for x in __version__ .split ("." )[:2 ])
14+ parsed_comp_version = tuple (int (x ) for x in compiler_version .split ("." )[:2 ])
15+
16+ if parsed_lib_version != parsed_comp_version :
17+ error = (
18+ f"Unsupported version. The proto files were compiled with a version of betterproto2_compiler which is not "
19+ "compatible with this version of betterproto2.\n "
20+ f" - betterproto2 version: { __version__ } \n "
21+ f" - betterproto2_compiler version: { compiler_version } \n "
22+ "The version x.y.z of the library matches the version a.b.c of the compiler if and only if a=x and b=y.\n "
23+ )
24+
25+ if parsed_lib_version < parsed_comp_version :
26+ error += (
27+ f"Please upgrade betterproto2 to { parsed_comp_version [0 ]} .{ parsed_comp_version [1 ]} .x (recommended) "
28+ f"or downgrade betterproto2_compiler to { parsed_lib_version [0 ]} .{ parsed_lib_version [1 ]} .x and "
29+ "recompile your proto files."
30+ )
31+ else :
32+ error += (
33+ f"Please upgrade betterproto2_compiler to { parsed_lib_version [0 ]} .{ parsed_lib_version [1 ]} .x and "
34+ "recompile your proto files (recommended) or downgrade betterproto2 to "
35+ f"{ parsed_comp_version [0 ]} .{ parsed_comp_version [1 ]} .x."
36+ )
37+
38+ raise ImportError (error )
Original file line number Diff line number Diff line change 1+ import pytest
2+
3+
4+ def test_check_compiler_version ():
5+ from betterproto2 import __version__ , check_compiler_version
6+
7+ x , y , z = (int (x ) for x in __version__ .split ("." ))
8+
9+ check_compiler_version (__version__ )
10+ check_compiler_version (f"{ x } .{ y } .{ z - 1 } " )
11+ check_compiler_version (f"{ x } .{ y } .{ z + 1 } " )
12+
13+ with pytest .raises (ImportError ):
14+ check_compiler_version (f"{ x } .{ y - 1 } .{ z } " )
15+
16+ with pytest .raises (ImportError ):
17+ check_compiler_version (f"{ x } .{ y + 1 } .{ z } " )
18+
19+ with pytest .raises (ImportError ):
20+ check_compiler_version (f"{ x + 1 } .{ y } .{ z } " )
21+
22+ with pytest .raises (ImportError ):
23+ check_compiler_version (f"{ x - 1 } .{ y } .{ z } " )
You can’t perform that action at this time.
0 commit comments