Skip to content

Commit 91111ab

Browse files
committed
Make plugin import errors more helpful
This addresses an issue where if the user happens to have black installed in their environment but not the other dependencies when running the protoc plugin then the resulting import error (No module named 'google') is not very helpful.
1 parent 5c4969f commit 91111ab

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

betterproto/plugin.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
#!/usr/bin/env python
22

3+
from collections import defaultdict
34
import itertools
45
import os.path
6+
import stringcase
57
import sys
68
import textwrap
7-
from collections import defaultdict
89
from typing import Dict, List, Optional, Type
10+
from betterproto.casing import safe_snake_case
911

1012
try:
13+
# betterproto[compiler] specific dependencies
1114
import black
12-
except ImportError:
15+
from google.protobuf.compiler import plugin_pb2 as plugin
16+
from google.protobuf.descriptor_pb2 import (
17+
DescriptorProto,
18+
EnumDescriptorProto,
19+
FieldDescriptorProto,
20+
)
21+
import google.protobuf.wrappers_pb2 as google_wrappers
22+
import jinja2
23+
except ImportError as err:
24+
missing_import = err.args[0][17:-1]
1325
print(
14-
"Unable to import `black` formatter. Did you install the compiler feature with `pip install betterproto[compiler]`?"
26+
"\033[31m"
27+
f"Unable to import `{missing_import}` from betterproto plugin! "
28+
"Please ensure that you've installed betterproto as "
29+
'`pip install "betterproto[compiler]"` so that compiler dependencies '
30+
"are included."
31+
"\033[0m"
1532
)
1633
raise SystemExit(1)
1734

18-
import jinja2
19-
import stringcase
20-
21-
from google.protobuf.compiler import plugin_pb2 as plugin
22-
from google.protobuf.descriptor_pb2 import (
23-
DescriptorProto,
24-
EnumDescriptorProto,
25-
FieldDescriptorProto,
26-
)
27-
28-
from betterproto.casing import safe_snake_case
29-
30-
import google.protobuf.wrappers_pb2 as google_wrappers
3135

3236
WRAPPER_TYPES: Dict[str, Optional[Type]] = defaultdict(
3337
lambda: None,

betterproto/tests/inputs/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"googletypes_service_returns_empty", # 9
1212
"casing_message_field_uppercase", # 11
1313
"namespace_keywords", # 70
14-
"namespace_builtin_types" # 53
14+
"namespace_builtin_types", # 53
1515
}
1616

1717
services = {

0 commit comments

Comments
 (0)