Skip to content

Commit 5c96be0

Browse files
Add lib / compiler compatibility check (#40)
* Add lib / compiler compatibility check * Add warning in documentation
1 parent 922c31f commit 5c96be0

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

docs/quick-start.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ e.g.
2222
python3 -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

src/betterproto2/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
__all__ = ["__version__"]
3+
__all__ = ["__version__", "check_compiler_version"]
44

55
import dataclasses
66
import enum as builtin_enum
@@ -44,7 +44,7 @@
4444
from typing_extensions import Self
4545

4646
from ._types import T
47-
from ._version import __version__
47+
from ._version import __version__, check_compiler_version
4848
from .casing import (
4949
camel_case,
5050
safe_snake_case,

src/betterproto2/_version.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,38 @@
11
from 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)

tests/test_version_check.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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}")

0 commit comments

Comments
 (0)