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..eecb106b90 100644 --- a/gapic/utils/reserved_names.py +++ b/gapic/utils/reserved_names.py @@ -12,7 +12,9 @@ # 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) +RESERVED_NAMES = frozenset(itertools.chain(keyword.kwlist, dir(builtins))) diff --git a/tests/unit/schema/test_metadata.py b/tests/unit/schema/test_metadata.py index a693e295b8..52aabbfd49 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,11 +161,31 @@ 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(): meta = metadata.Metadata() assert meta.doc == ''