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

Commit e17d88e

Browse files
committed
fix: module names can no longer collide with keywords or builtinsg
E.g. protobuf/any.proto will be imported as from google.protobuf import any_pb2 as gp_any # Was previously 'as any'
1 parent b5279ef commit e17d88e

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

gapic/schema/metadata.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from gapic.schema import imp
3535
from gapic.schema import naming
3636
from gapic.utils import cached_property
37+
from gapic.utils import RESERVED_NAMES
3738

3839

3940
@dataclasses.dataclass(frozen=True)
@@ -48,6 +49,9 @@ class Address:
4849
)
4950
collisions: FrozenSet[str] = dataclasses.field(default_factory=frozenset)
5051

52+
def __post_init__(self):
53+
super().__setattr__("collisions", self.collisions | RESERVED_NAMES)
54+
5155
def __eq__(self, other) -> bool:
5256
return all([getattr(self, i) == getattr(other, i) for i
5357
in ('name', 'module', 'module_path', 'package', 'parent')])

gapic/utils/reserved_names.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import builtins
16+
import itertools
1517
import keyword
1618

1719

18-
RESERVED_NAMES = frozenset(keyword.kwlist)
20+
RESERVED_NAMES = frozenset(itertools.chain(keyword.kwlist, dir(builtins)))

tests/unit/schema/test_metadata.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from gapic.schema import metadata
2222
from gapic.schema import naming
23+
from gapic.utils import RESERVED_NAMES
2324

2425

2526
def test_address_str():
@@ -160,11 +161,31 @@ def test_address_subpackage_empty():
160161

161162
def test_metadata_with_context():
162163
meta = metadata.Metadata()
163-
assert meta.with_context(
164+
collisions = meta.with_context(
164165
collisions={'foo', 'bar'},
165-
).address.collisions == {'foo', 'bar'}
166+
).address.collisions - RESERVED_NAMES
167+
assert collisions == {'foo', 'bar'}
166168

167169

170+
171+
def test_address_name_builtin_keyword():
172+
addr_builtin = metadata.Address(
173+
name="Any",
174+
module="any",
175+
package=("google", "protobuf"),
176+
api_naming=naming.NewNaming(proto_package="foo.bar.baz.v1"),
177+
)
178+
assert addr_builtin.module_alias == "gp_any"
179+
180+
addr_kword = metadata.Address(
181+
name="Class",
182+
module="class",
183+
package=("google", "protobuf"),
184+
api_naming=naming.NewNaming(proto_package="foo.bar.baz.v1"),
185+
)
186+
assert addr_kword.module_alias == "gp_class"
187+
188+
168189
def test_doc_nothing():
169190
meta = metadata.Metadata()
170191
assert meta.doc == ''

0 commit comments

Comments
 (0)