diff --git a/gapic/utils/options.py b/gapic/utils/options.py index d7bbe2473d..d6c692c8fe 100644 --- a/gapic/utils/options.py +++ b/gapic/utils/options.py @@ -37,7 +37,7 @@ class Options: warehouse_package_name: str = '' retry: Optional[Dict[str, Any]] = None sample_configs: Tuple[str, ...] = dataclasses.field(default=()) - autogen_snippets: bool = False + autogen_snippets: bool = True templates: Tuple[str, ...] = dataclasses.field(default=('DEFAULT',)) lazy_import: bool = False old_naming: bool = False @@ -132,6 +132,17 @@ def tweak_path(p): # Build the options instance. sample_paths = opts.pop('samples', []) + # autogen-snippets is True by default, so make sure users can disable + # by passing `autogen-snippets=false` + autogen_snippets = opts.pop( + "autogen-snippets", ["True"])[0] in ("True", "true", "T", "t", "TRUE") + + # NOTE: Snippets are not currently correct for the alternative (Ads) templates + # so always disable snippetgen in that case + # https://github.com/googleapis/gapic-generator-python/issues/1052 + if opts.get("old-naming"): + autogen_snippets = False + answer = Options( name=opts.pop('name', ['']).pop(), namespace=tuple(opts.pop('namespace', [])), @@ -143,7 +154,7 @@ def tweak_path(p): for s in sample_paths for cfg_path in samplegen_utils.generate_all_sample_fpaths(s) ), - autogen_snippets=bool(opts.pop("autogen-snippets", False)), + autogen_snippets=autogen_snippets, templates=tuple(path.expanduser(i) for i in templates), lazy_import=bool(opts.pop('lazy-import', False)), old_naming=bool(opts.pop('old-naming', False)), diff --git a/tests/unit/generator/test_generator.py b/tests/unit/generator/test_generator.py index d068250e97..26873b1d33 100644 --- a/tests/unit/generator/test_generator.py +++ b/tests/unit/generator/test_generator.py @@ -242,7 +242,10 @@ def test_get_response_enumerates_proto(): def test_get_response_divides_subpackages(): - g = make_generator() + # NOTE: autogen-snippets is intentionally disabled for this test + # The API schema below is incomplete and will result in errors when the + # snippetgen logic tries to parse it. + g = make_generator("autogen-snippets=false") api_schema = api.API.build( [ descriptor_pb2.FileDescriptorProto( @@ -277,7 +280,7 @@ def test_get_response_divides_subpackages(): """.strip() ) cgr = g.get_response(api_schema=api_schema, - opts=Options.build("")) + opts=Options.build("autogen-snippets=false")) assert len(cgr.file) == 6 assert {i.name for i in cgr.file} == { "foo/types/top.py", @@ -683,7 +686,12 @@ def test_dont_generate_in_code_samples(mock_gmtime, mock_generate_sample, fs): ), ) - generator = make_generator(f"samples={config_fpath}") + # NOTE: autogen-snippets is intentionally disabled for this test + # The API schema below is incomplete and will result in errors when the + # snippetgen logic attempts to parse it. + generator = make_generator( + f"samples={config_fpath},autogen-snippets=False") + print(generator) generator._env.loader = jinja2.DictLoader({"sample.py.j2": ""}) api_schema = make_api( make_proto( @@ -743,7 +751,7 @@ def test_dont_generate_in_code_samples(mock_gmtime, mock_generate_sample, fs): expected.supported_features |= CodeGeneratorResponse.Feature.FEATURE_PROTO3_OPTIONAL actual = generator.get_response( - api_schema=api_schema, opts=Options.build("") + api_schema=api_schema, opts=Options.build("autogen-snippets=False") ) assert actual == expected