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

Commit e46f443

Browse files
authored
feat: generate code snippets by default (#1044)
1 parent 925b62a commit e46f443

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

gapic/utils/options.py

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

135+
# autogen-snippets is True by default, so make sure users can disable
136+
# by passing `autogen-snippets=false`
137+
autogen_snippets = opts.pop(
138+
"autogen-snippets", ["True"])[0] in ("True", "true", "T", "t", "TRUE")
139+
140+
# NOTE: Snippets are not currently correct for the alternative (Ads) templates
141+
# so always disable snippetgen in that case
142+
# https://github.com/googleapis/gapic-generator-python/issues/1052
143+
if opts.get("old-naming"):
144+
autogen_snippets = False
145+
135146
answer = Options(
136147
name=opts.pop('name', ['']).pop(),
137148
namespace=tuple(opts.pop('namespace', [])),
@@ -143,7 +154,7 @@ def tweak_path(p):
143154
for s in sample_paths
144155
for cfg_path in samplegen_utils.generate_all_sample_fpaths(s)
145156
),
146-
autogen_snippets=bool(opts.pop("autogen-snippets", False)),
157+
autogen_snippets=autogen_snippets,
147158
templates=tuple(path.expanduser(i) for i in templates),
148159
lazy_import=bool(opts.pop('lazy-import', False)),
149160
old_naming=bool(opts.pop('old-naming', False)),

tests/unit/generator/test_generator.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,10 @@ def test_get_response_enumerates_proto():
242242

243243

244244
def test_get_response_divides_subpackages():
245-
g = make_generator()
245+
# NOTE: autogen-snippets is intentionally disabled for this test
246+
# The API schema below is incomplete and will result in errors when the
247+
# snippetgen logic tries to parse it.
248+
g = make_generator("autogen-snippets=false")
246249
api_schema = api.API.build(
247250
[
248251
descriptor_pb2.FileDescriptorProto(
@@ -277,7 +280,7 @@ def test_get_response_divides_subpackages():
277280
""".strip()
278281
)
279282
cgr = g.get_response(api_schema=api_schema,
280-
opts=Options.build(""))
283+
opts=Options.build("autogen-snippets=false"))
281284
assert len(cgr.file) == 6
282285
assert {i.name for i in cgr.file} == {
283286
"foo/types/top.py",
@@ -683,7 +686,12 @@ def test_dont_generate_in_code_samples(mock_gmtime, mock_generate_sample, fs):
683686
),
684687
)
685688

686-
generator = make_generator(f"samples={config_fpath}")
689+
# NOTE: autogen-snippets is intentionally disabled for this test
690+
# The API schema below is incomplete and will result in errors when the
691+
# snippetgen logic attempts to parse it.
692+
generator = make_generator(
693+
f"samples={config_fpath},autogen-snippets=False")
694+
print(generator)
687695
generator._env.loader = jinja2.DictLoader({"sample.py.j2": ""})
688696
api_schema = make_api(
689697
make_proto(
@@ -743,7 +751,7 @@ def test_dont_generate_in_code_samples(mock_gmtime, mock_generate_sample, fs):
743751
expected.supported_features |= CodeGeneratorResponse.Feature.FEATURE_PROTO3_OPTIONAL
744752

745753
actual = generator.get_response(
746-
api_schema=api_schema, opts=Options.build("")
754+
api_schema=api_schema, opts=Options.build("autogen-snippets=False")
747755
)
748756
assert actual == expected
749757

0 commit comments

Comments
 (0)