Skip to content

Commit 881f77f

Browse files
committed
chore(gapic): apply blacken/ruff format to generator
1 parent 926a96f commit 881f77f

10 files changed

Lines changed: 246 additions & 187 deletions

File tree

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

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,29 @@
3636
Set,
3737
Tuple,
3838
)
39-
import yaml
4039

40+
import grpc # type: ignore
41+
import yaml
42+
from gapic.schema import metadata, mixins, wrappers
43+
from gapic.schema import naming as api_naming
44+
from gapic.utils import RESERVED_NAMES, Options, cached_property, nth, to_snake_case
45+
from google.api import (
46+
annotations_pb2, # type: ignore
47+
client_pb2, # type: ignore
48+
http_pb2, # type: ignore
49+
resource_pb2, # type: ignore
50+
service_pb2, # type: ignore
51+
)
4152
from google.api_core import exceptions
42-
from google.api import client_pb2 # type: ignore
43-
from google.api import http_pb2 # type: ignore
44-
from google.api import resource_pb2 # type: ignore
45-
from google.api import service_pb2 # type: ignore
4653
from google.cloud import extended_operations_pb2 as ex_ops_pb2 # type: ignore
54+
from google.cloud.location import locations_pb2 # type: ignore
4755
from google.gapic.metadata import gapic_metadata_pb2 # type: ignore
48-
from google.longrunning import operations_pb2 # type: ignore
4956
from google.iam.v1 import iam_policy_pb2 # type: ignore
50-
from google.cloud.location import locations_pb2 # type: ignore
57+
from google.longrunning import operations_pb2 # type: ignore
5158
from google.protobuf import descriptor_pb2 # type: ignore
52-
from google.protobuf.json_format import MessageToJson
53-
from google.protobuf.json_format import ParseDict
5459
from google.protobuf.descriptor import ServiceDescriptor
55-
import grpc # type: ignore
5660
from google.protobuf.descriptor_pb2 import MethodDescriptorProto
57-
from google.api import annotations_pb2 # type: ignore
58-
from gapic.schema import metadata
59-
from gapic.schema import mixins
60-
from gapic.schema import wrappers
61-
from gapic.schema import naming as api_naming
62-
from gapic.utils import cached_property
63-
from gapic.utils import nth
64-
from gapic.utils import Options
65-
from gapic.utils import to_snake_case
66-
from gapic.utils import RESERVED_NAMES
67-
61+
from google.protobuf.json_format import MessageToJson, ParseDict
6862

6963
TRANSPORT_GRPC = "grpc"
7064
TRANSPORT_GRPC_ASYNC = "grpc-async"
@@ -160,7 +154,12 @@ def messages(self) -> Mapping[str, wrappers.MessageType]:
160154
def resource_messages(self) -> Mapping[str, wrappers.MessageType]:
161155
"""Return the file level resources of the proto."""
162156
file_resource_messages = (
163-
(res.type, wrappers.CommonResource.build(res, aliases=self.resource_name_aliases).message_type)
157+
(
158+
res.type,
159+
wrappers.CommonResource.build(
160+
res, aliases=self.resource_name_aliases
161+
).message_type,
162+
)
164163
for res in self.file_pb2.options.Extensions[
165164
resource_pb2.resource_definition
166165
]
@@ -179,7 +178,7 @@ def resource_messages(self) -> Mapping[str, wrappers.MessageType]:
179178
file_resource_messages,
180179
resource_messages,
181180
),
182-
key=lambda item: item[0]
181+
key=lambda item: item[0],
183182
)
184183
)
185184

@@ -319,7 +318,8 @@ def with_selective_generation(
319318
new_v = v.with_selective_generation(
320319
generate_omitted_as_internal=generate_omitted_as_internal,
321320
public_methods=public_methods,
322-
excluded_addresses=excluded_addresses)
321+
excluded_addresses=excluded_addresses,
322+
)
323323
if new_v:
324324
services[k] = new_v
325325

@@ -331,7 +331,9 @@ def with_selective_generation(
331331
# This ensures that shared messages, messages not attached to any RPC,
332332
# and messages reachable via other paths (like LRO response types) are KEPT.
333333
all_messages = {
334-
k: v for k, v in self.all_messages.items() if v.ident not in excluded_addresses
334+
k: v
335+
for k, v in self.all_messages.items()
336+
if v.ident not in excluded_addresses
335337
}
336338

337339
all_enums = {
@@ -340,7 +342,12 @@ def with_selective_generation(
340342

341343
# If the proto becomes empty after pruning, we return None to signal
342344
# that it should be excluded from generation.
343-
if not generate_omitted_as_internal and not services and not all_messages and not all_enums:
345+
if (
346+
not generate_omitted_as_internal
347+
and not services
348+
and not all_messages
349+
and not all_enums
350+
):
344351
return None
345352

346353
return dataclasses.replace(
@@ -521,9 +528,14 @@ def disambiguate_keyword_sanitize_fname(
521528
excluded_addresses=set([]),
522529
)
523530
else:
524-
all_resource_messages = dict(collections.ChainMap(
525-
*(proto.resource_messages for proto in api.all_protos.values())
526-
))
531+
all_resource_messages = dict(
532+
collections.ChainMap(
533+
*(
534+
proto.resource_messages
535+
for proto in api.all_protos.values()
536+
)
537+
)
538+
)
527539

528540
# Create a global map of services to support cross-proto lookup
529541
# for extended LROs.
@@ -550,19 +562,31 @@ def disambiguate_keyword_sanitize_fname(
550562
for method in service.methods.values():
551563
if method.ident.proto not in selective_gapic_methods:
552564
# Candidate for exclusion: the method itself and its direct request/response types.
553-
candidate_excluded_addresses.add(method.meta.address)
565+
candidate_excluded_addresses.add(
566+
method.meta.address
567+
)
554568
candidate_excluded_addresses.add(method.input.ident)
555-
candidate_excluded_addresses.add(method.output.ident)
569+
candidate_excluded_addresses.add(
570+
method.output.ident
571+
)
556572

557573
# If this is an LRO, add its response and metadata types to candidates.
558574
if method.lro:
559-
candidate_excluded_addresses.add(method.lro.response_type.ident)
560-
candidate_excluded_addresses.add(method.lro.metadata_type.ident)
575+
candidate_excluded_addresses.add(
576+
method.lro.response_type.ident
577+
)
578+
candidate_excluded_addresses.add(
579+
method.lro.metadata_type.ident
580+
)
561581

562582
# If this is an extended LRO, add its request and operation types to candidates.
563583
if method.extended_lro:
564-
candidate_excluded_addresses.add(method.extended_lro.request_type.ident)
565-
candidate_excluded_addresses.add(method.extended_lro.operation_type.ident)
584+
candidate_excluded_addresses.add(
585+
method.extended_lro.request_type.ident
586+
)
587+
candidate_excluded_addresses.add(
588+
method.extended_lro.operation_type.ident
589+
)
566590

567591
# Calculate publicly reachable addresses (API-wide).
568592
# This includes all types reachable from the allowlisted (public) methods.
@@ -581,7 +605,9 @@ def disambiguate_keyword_sanitize_fname(
581605
# This set difference effectively "vets" the candidates. If a candidate
582606
# message is actually reachable from a public RPC, it's removed from
583607
# the exclusion list.
584-
excluded_addresses = candidate_excluded_addresses - public_rpc_addresses
608+
excluded_addresses = (
609+
candidate_excluded_addresses - public_rpc_addresses
610+
)
585611

586612
for name, proto in api.protos.items():
587613
proto_to_generate = proto.with_selective_generation(

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

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,39 +37,38 @@
3737
from itertools import chain
3838
from typing import (
3939
Any,
40-
cast,
40+
ClassVar,
4141
Dict,
4242
FrozenSet,
43-
Iterator,
4443
Iterable,
44+
Iterator,
4545
List,
4646
Mapping,
47-
ClassVar,
4847
Optional,
48+
Pattern,
4949
Sequence,
5050
Set,
5151
Tuple,
5252
Union,
53-
Pattern,
53+
cast,
5454
)
55-
from google.api import annotations_pb2 # type: ignore
56-
from google.api import client_pb2
57-
from google.api import field_behavior_pb2
58-
from google.api import field_info_pb2
59-
from google.api import http_pb2
60-
from google.api import resource_pb2
61-
from google.api import routing_pb2
62-
from google.api_core import exceptions
63-
from google.api_core import path_template
64-
from google.cloud import extended_operations_pb2 as ex_ops_pb2 # type: ignore
65-
from google.protobuf import descriptor_pb2 # type: ignore
66-
from google.protobuf.json_format import MessageToDict # type: ignore
6755

6856
from gapic import utils
6957
from gapic.schema import metadata
70-
from gapic.utils import cached_proto_context
71-
from gapic.utils import uri_sample
72-
from gapic.utils import make_private
58+
from gapic.utils import cached_proto_context, make_private, uri_sample
59+
from google.api import (
60+
annotations_pb2, # type: ignore
61+
client_pb2,
62+
field_behavior_pb2,
63+
field_info_pb2,
64+
http_pb2,
65+
resource_pb2,
66+
routing_pb2,
67+
)
68+
from google.api_core import exceptions, path_template
69+
from google.cloud import extended_operations_pb2 as ex_ops_pb2 # type: ignore
70+
from google.protobuf import descriptor_pb2 # type: ignore
71+
from google.protobuf.json_format import MessageToDict # type: ignore
7372

7473

7574
@dataclasses.dataclass(frozen=True)
@@ -691,7 +690,7 @@ def resource_type(self) -> Optional[str]:
691690
resource = self.options.Extensions[resource_pb2.resource]
692691
if not resource.type:
693692
return None
694-
693+
695694
default_type = resource.type[resource.type.find("/") + 1 :]
696695

697696
return self.resource_name_aliases.get(resource.type, default_type)
@@ -2091,16 +2090,23 @@ def with_selective_generation(
20912090
return None
20922091

20932092

2094-
20952093
@dataclasses.dataclass(frozen=True)
20962094
class CommonResource:
20972095
type_name: str
20982096
pattern: str
20992097
resource_name_aliases: Mapping[str, str] = dataclasses.field(default_factory=dict)
21002098

21012099
@classmethod
2102-
def build(cls, resource: resource_pb2.ResourceDescriptor, aliases: Optional[Mapping[str, str]] = None):
2103-
return cls(type_name=resource.type, pattern=next(iter(resource.pattern)), resource_name_aliases=aliases or {})
2100+
def build(
2101+
cls,
2102+
resource: resource_pb2.ResourceDescriptor,
2103+
aliases: Optional[Mapping[str, str]] = None,
2104+
):
2105+
return cls(
2106+
type_name=resource.type,
2107+
pattern=next(iter(resource.pattern)),
2108+
resource_name_aliases=aliases or {},
2109+
)
21042110

21052111
@utils.cached_property
21062112
def message_type(self):
@@ -2295,7 +2301,7 @@ def names(self) -> FrozenSet[str]:
22952301
return frozenset(answer)
22962302

22972303
@utils.cached_property
2298-
def resource_messages(self) -> Sequence['MessageType']:
2304+
def resource_messages(self) -> Sequence["MessageType"]:
22992305
"""Returns all the resource message types used in all
23002306
request and response fields in the service."""
23012307

@@ -2336,8 +2342,7 @@ def gen_indirect_resources_used(message):
23362342
# Convert the set to a sorted tuple using the resource path or message name.
23372343
# This is needed to prevent non-deterministic code generation.
23382344
sorted_messages = sorted(
2339-
unique_messages,
2340-
key=lambda m: m.resource_type_full_path or m.name
2345+
unique_messages, key=lambda m: m.resource_type_full_path or m.name
23412346
)
23422347

23432348
# Fail-fast collision detection
@@ -2350,7 +2355,7 @@ def gen_indirect_resources_used(message):
23502355
f"\n\nFatal: Message '{msg.name}' defines a resource pattern but is missing a resource type. "
23512356
f"This violates AIP-123 (https://google.aip.dev/123). Please define a 'type' in the google.api.resource option."
23522357
)
2353-
2358+
23542359
if res_type in seen_types:
23552360
incumbent = seen_types[res_type]
23562361
raise ValueError(
@@ -2500,7 +2505,8 @@ def with_selective_generation(
25002505
new_v = v.with_selective_generation(
25012506
generate_omitted_as_internal=generate_omitted_as_internal,
25022507
public_methods=public_methods,
2503-
excluded_addresses=excluded_addresses)
2508+
excluded_addresses=excluded_addresses,
2509+
)
25042510
if new_v:
25052511
methods[k] = new_v
25062512

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from collections import defaultdict
16-
from os import path
17-
from typing import Any, DefaultDict, Dict, FrozenSet, List, Optional, Tuple
18-
1915
import dataclasses
2016
import json
2117
import os
2218
import warnings
23-
import yaml
19+
from collections import defaultdict
20+
from os import path
21+
from typing import Any, DefaultDict, Dict, FrozenSet, List, Optional, Tuple
2422

23+
import yaml
2524
from gapic.samplegen_utils import utils as samplegen_utils
2625

2726

@@ -74,7 +73,7 @@ class Options:
7473
# For example, 'google.cloud.api.v1+google.cloud.anotherapi.v2'
7574
"proto-plus-deps",
7675
"gapic-version", # A version string following https://peps.python.org/pep-0440,
77-
# Resolves method name collisions by mapping a fully qualified
76+
# Resolves method name collisions by mapping a fully qualified
7877
# resource path to a custom TitleCase alias.
7978
# Format: resource.path/Name:AliasName
8079
"resource-name-alias",
@@ -196,26 +195,26 @@ def tweak_path(p):
196195
# Parse the resource name aliases dictionary (Format: "path/to/Resource:AliasName")
197196
resource_name_aliases = {}
198197
raw_aliases = opts.pop("resource-name-alias", [])
199-
198+
200199
# Parse explicitly and safely
201200
for mapping in raw_aliases:
202201
if not mapping:
203-
# We only need to check `not mapping` because the top-level
202+
# We only need to check `not mapping` because the top-level
204203
# opt_string parser already stripped trailing whitespaces
205204
continue
206-
205+
207206
try:
208-
# split(":", 1) ensures we only split on the FIRST colon
207+
# split(":", 1) ensures we only split on the FIRST colon
209208
res_path, alias_name = mapping.split(":", 1)
210-
209+
211210
clean_path = res_path.strip()
212211
clean_alias = alias_name.strip()
213-
212+
214213
if not clean_path or not clean_alias:
215214
raise ValueError()
216-
215+
217216
resource_name_aliases[clean_path] = clean_alias
218-
217+
219218
except ValueError:
220219
warnings.warn(
221220
f"Ignored malformed resource-name-alias: '{mapping}'. "

0 commit comments

Comments
 (0)