From 960d550c4a8fd09b052cce785d76243a5d4525d7 Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Tue, 15 Sep 2020 16:01:48 -0700 Subject: [PATCH 1/2] fix: module names can no longer collide with keywords or builtins (#595) E.g. protobuf/any.proto will be imported as from google.protobuf import any_pb2 as gp_any # Was previously 'as any' --- gapic/schema/api.py | 3 ++- gapic/schema/metadata.py | 4 ++++ gapic/utils/reserved_names.py | 13 ++++++++++++- tests/unit/schema/test_metadata.py | 24 ++++++++++++++++++++++-- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/gapic/schema/api.py b/gapic/schema/api.py index db91c46159..e303db8d88 100644 --- a/gapic/schema/api.py +++ b/gapic/schema/api.py @@ -19,6 +19,7 @@ import collections import dataclasses +import keyword import os import sys from typing import Callable, Container, Dict, FrozenSet, Mapping, Optional, Sequence, Set, Tuple @@ -229,7 +230,7 @@ def disambiguate_keyword_fname( visited_names: Container[str]) -> str: path, fname = os.path.split(full_path) name, ext = os.path.splitext(fname) - if name in RESERVED_NAMES or full_path in visited_names: + if name in keyword.kwlist or full_path in visited_names: name += "_" full_path = os.path.join(path, name + ext) if full_path in visited_names: diff --git a/gapic/schema/metadata.py b/gapic/schema/metadata.py index 349d7f13f9..9459bb5ef1 100644 --- a/gapic/schema/metadata.py +++ b/gapic/schema/metadata.py @@ -34,6 +34,7 @@ from gapic.schema import imp from gapic.schema import naming from gapic.utils import cached_property +from gapic.utils import RESERVED_NAMES @dataclasses.dataclass(frozen=True) @@ -48,6 +49,9 @@ class Address: ) collisions: FrozenSet[str] = dataclasses.field(default_factory=frozenset) + def __post_init__(self): + super().__setattr__("collisions", self.collisions | RESERVED_NAMES) + def __eq__(self, other) -> bool: return all([getattr(self, i) == getattr(other, i) for i in ('name', 'module', 'module_path', 'package', 'parent')]) diff --git a/gapic/utils/reserved_names.py b/gapic/utils/reserved_names.py index b146ce08e5..866a867663 100644 --- a/gapic/utils/reserved_names.py +++ b/gapic/utils/reserved_names.py @@ -12,7 +12,18 @@ # See the License for the specific language governing permissions and # limitations under the License. +import builtins +import itertools import keyword -RESERVED_NAMES = frozenset(keyword.kwlist) +# The filter and map builtins are a historical artifact; +# they are not used in modern, idiomatic python, +# nor are they used in the gapic surface. +# They are too useful to reserve. +RESERVED_NAMES = frozenset( + itertools.chain( + keyword.kwlist, + set(dir(builtins)) - {"filter", "map"}, + ) +) diff --git a/tests/unit/schema/test_metadata.py b/tests/unit/schema/test_metadata.py index a693e295b8..62cd957cbd 100644 --- a/tests/unit/schema/test_metadata.py +++ b/tests/unit/schema/test_metadata.py @@ -20,6 +20,7 @@ from gapic.schema import metadata from gapic.schema import naming +from gapic.utils import RESERVED_NAMES def test_address_str(): @@ -160,9 +161,28 @@ def test_address_subpackage_empty(): def test_metadata_with_context(): meta = metadata.Metadata() - assert meta.with_context( + collisions = meta.with_context( collisions={'foo', 'bar'}, - ).address.collisions == {'foo', 'bar'} + ).address.collisions - RESERVED_NAMES + assert collisions == {'foo', 'bar'} + + +def test_address_name_builtin_keyword(): + addr_builtin = metadata.Address( + name="Any", + module="any", + package=("google", "protobuf"), + api_naming=naming.NewNaming(proto_package="foo.bar.baz.v1"), + ) + assert addr_builtin.module_alias == "gp_any" + + addr_kword = metadata.Address( + name="Class", + module="class", + package=("google", "protobuf"), + api_naming=naming.NewNaming(proto_package="foo.bar.baz.v1"), + ) + assert addr_kword.module_alias == "gp_class" def test_doc_nothing(): From 258762c1d8c405ac99ee41de230d1ae1ae9e865e Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Tue, 15 Sep 2020 16:06:25 -0700 Subject: [PATCH 2/2] chore: release 0.33.3 (#599) Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com> --- CHANGELOG.md | 7 +++++++ setup.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e13e55ff16..b31f5515c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +### [0.33.3](https://www.github.com/googleapis/gapic-generator-python/compare/v0.33.2...v0.33.3) (2020-09-15) + + +### Bug Fixes + +* module names can no longer collide with keywords or builtins ([#595](https://www.github.com/googleapis/gapic-generator-python/issues/595)) ([960d550](https://www.github.com/googleapis/gapic-generator-python/commit/960d550c4a8fd09b052cce785d76243a5d4525d7)) + ### [0.33.2](https://www.github.com/googleapis/gapic-generator-python/compare/v0.33.1...v0.33.2) (2020-09-15) diff --git a/setup.py b/setup.py index 3664409113..ab32647cd9 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ PACKAGE_ROOT = os.path.abspath(os.path.dirname(__file__)) -version = "0.33.2" +version = "0.33.3" with io.open(os.path.join(PACKAGE_ROOT, "README.rst")) as file_obj: README = file_obj.read()