Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 469024f

Browse files
committed
Revert "fix: don't enable snippetgen by default (#1078)"
This reverts commit 8bdb709.
1 parent 0c8b642 commit 469024f

3 files changed

Lines changed: 38 additions & 7 deletions

File tree

gapic/utils/options.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Options:
3838
warehouse_package_name: str = ''
3939
retry: Optional[Dict[str, Any]] = None
4040
sample_configs: Tuple[str, ...] = dataclasses.field(default=())
41-
autogen_snippets: bool = False
41+
autogen_snippets: bool = True
4242
templates: Tuple[str, ...] = dataclasses.field(default=('DEFAULT',))
4343
lazy_import: bool = False
4444
old_naming: bool = False
@@ -146,6 +146,17 @@ def tweak_path(p):
146146
# Build the options instance.
147147
sample_paths = opts.pop('samples', [])
148148

149+
# autogen-snippets is True by default, so make sure users can disable
150+
# by passing `autogen-snippets=false`
151+
autogen_snippets = opts.pop(
152+
"autogen-snippets", ["True"])[0] in ("True", "true", "T", "t", "TRUE")
153+
154+
# NOTE: Snippets are not currently correct for the alternative (Ads) templates
155+
# so always disable snippetgen in that case
156+
# https://github.com/googleapis/gapic-generator-python/issues/1052
157+
if opts.get("old-naming"):
158+
autogen_snippets = False
159+
149160
answer = Options(
150161
name=opts.pop('name', ['']).pop(),
151162
namespace=tuple(opts.pop('namespace', [])),
@@ -157,7 +168,7 @@ def tweak_path(p):
157168
for s in sample_paths
158169
for cfg_path in samplegen_utils.generate_all_sample_fpaths(s)
159170
),
160-
autogen_snippets=bool(opts.pop("autogen-snippets", False)),
171+
autogen_snippets=autogen_snippets,
161172
templates=tuple(path.expanduser(i) for i in templates),
162173
lazy_import=bool(opts.pop('lazy-import', False)),
163174
old_naming=bool(opts.pop('old-naming', False)),

tests/unit/generator/test_generator.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,10 @@ def test_get_response_enumerates_proto():
255255

256256

257257
def test_get_response_divides_subpackages():
258-
g = make_generator()
258+
# NOTE: autogen-snippets is intentionally disabled for this test
259+
# The API schema below is incomplete and will result in errors when the
260+
# snippetgen logic tries to parse it.
261+
g = make_generator("autogen-snippets=false")
259262
api_schema = api.API.build(
260263
[
261264
descriptor_pb2.FileDescriptorProto(
@@ -290,7 +293,7 @@ def test_get_response_divides_subpackages():
290293
""".strip()
291294
)
292295
cgr = g.get_response(api_schema=api_schema,
293-
opts=Options.build(""))
296+
opts=Options.build("autogen-snippets=false"))
294297
assert len(cgr.file) == 6
295298
assert {i.name for i in cgr.file} == {
296299
"foo/types/top.py",

tests/unit/generator/test_options.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,7 @@ def test_options_service_yaml_config(fs):
171171

172172

173173
def test_options_bool_flags():
174-
# All these options are default False.
175-
# If new options violate this assumption,
176-
# this test may need to be tweaked.
174+
# Most options are default False.
177175
# New options should follow the dash-case/snake_case convention.
178176
opt_str_to_attr_name = {
179177
name: re.sub(r"-", "_", name)
@@ -191,3 +189,22 @@ def test_options_bool_flags():
191189

192190
options = Options.build(opt)
193191
assert getattr(options, attr)
192+
193+
# Check autogen-snippets separately, as it is default True
194+
options = Options.build("")
195+
assert options.autogen_snippets
196+
197+
options = Options.build("autogen-snippets=False")
198+
assert not options.autogen_snippets
199+
200+
201+
def test_options_autogen_snippets_false_for_old_naming():
202+
# NOTE: Snippets are not currently correct for the alternative (Ads) templates
203+
# so always disable snippetgen in that case
204+
# https://github.com/googleapis/gapic-generator-python/issues/1052
205+
options = Options.build("old-naming")
206+
assert not options.autogen_snippets
207+
208+
# Even if autogen-snippets is set to True, do not enable snippetgen
209+
options = Options.build("old-naming,autogen-snippets=True")
210+
assert not options.autogen_snippets

0 commit comments

Comments
 (0)