Skip to content

Commit 5cd2cfe

Browse files
authored
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'
1 parent 2035703 commit 5cd2cfe

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

packages/gapic-generator/gapic/schema/api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import collections
2121
import dataclasses
22+
import keyword
2223
import os
2324
import sys
2425
from typing import Callable, Container, Dict, FrozenSet, Mapping, Optional, Sequence, Set, Tuple
@@ -229,7 +230,7 @@ def disambiguate_keyword_fname(
229230
visited_names: Container[str]) -> str:
230231
path, fname = os.path.split(full_path)
231232
name, ext = os.path.splitext(fname)
232-
if name in RESERVED_NAMES or full_path in visited_names:
233+
if name in keyword.kwlist or full_path in visited_names:
233234
name += "_"
234235
full_path = os.path.join(path, name + ext)
235236
if full_path in visited_names:

packages/gapic-generator/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')])

packages/gapic-generator/gapic/utils/reserved_names.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@
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+
# The filter and map builtins are a historical artifact;
21+
# they are not used in modern, idiomatic python,
22+
# nor are they used in the gapic surface.
23+
# They are too useful to reserve.
24+
RESERVED_NAMES = frozenset(
25+
itertools.chain(
26+
keyword.kwlist,
27+
set(dir(builtins)) - {"filter", "map"},
28+
)
29+
)

packages/gapic-generator/tests/unit/schema/test_metadata.py

Lines changed: 22 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,9 +161,28 @@ 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'}
168+
169+
170+
def test_address_name_builtin_keyword():
171+
addr_builtin = metadata.Address(
172+
name="Any",
173+
module="any",
174+
package=("google", "protobuf"),
175+
api_naming=naming.NewNaming(proto_package="foo.bar.baz.v1"),
176+
)
177+
assert addr_builtin.module_alias == "gp_any"
178+
179+
addr_kword = metadata.Address(
180+
name="Class",
181+
module="class",
182+
package=("google", "protobuf"),
183+
api_naming=naming.NewNaming(proto_package="foo.bar.baz.v1"),
184+
)
185+
assert addr_kword.module_alias == "gp_class"
166186

167187

168188
def test_doc_nothing():

0 commit comments

Comments
 (0)