Skip to content

Commit 2d1d2a5

Browse files
committed
fix(generator): add type ignore to requests import in compat template and update goldens
1 parent 2001c79 commit 2d1d2a5

10 files changed

Lines changed: 402 additions & 2 deletions

File tree

  • packages/gapic-generator

packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/_compat.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import uuid
44

55
try:
6-
from google.api_core.gapic_v1.requests import setup_request_id
6+
from google.api_core.gapic_v1.requests import setup_request_id # type: ignore
77
except ImportError:
88
# TODO: Remove this fallback when google-api-core >= 2.18.0 is the minimum required version.
99
def setup_request_id(request, field_name: str, is_proto3_optional: bool):
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
import uuid
18+
19+
try:
20+
from google.api_core.gapic_v1.requests import setup_request_id # type: ignore
21+
except ImportError:
22+
# TODO: Remove this fallback when google-api-core >= 2.18.0 is the minimum required version.
23+
def setup_request_id(request, field_name: str, is_proto3_optional: bool):
24+
"""Populate a UUID4 field in the request if it is not already set.
25+
26+
Args:
27+
request (Union[google.protobuf.message.Message, dict]): The request object.
28+
field_name (str): The name of the field to populate.
29+
is_proto3_optional (bool): Whether the field is proto3 optional.
30+
"""
31+
if isinstance(request, dict):
32+
if is_proto3_optional:
33+
if field_name not in request:
34+
request[field_name] = str(uuid.uuid4())
35+
elif not request.get(field_name):
36+
request[field_name] = str(uuid.uuid4())
37+
return
38+
39+
if is_proto3_optional:
40+
try:
41+
# Pure protobuf messages
42+
if not request.HasField(field_name):
43+
setattr(request, field_name, str(uuid.uuid4()))
44+
except (AttributeError, ValueError):
45+
# Proto-plus messages or other objects
46+
if field_name not in request:
47+
setattr(request, field_name, str(uuid.uuid4()))
48+
else:
49+
if not getattr(request, field_name):
50+
setattr(request, field_name, str(uuid.uuid4()))
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
import uuid
18+
19+
try:
20+
from google.api_core.gapic_v1.requests import setup_request_id # type: ignore
21+
except ImportError:
22+
# TODO: Remove this fallback when google-api-core >= 2.18.0 is the minimum required version.
23+
def setup_request_id(request, field_name: str, is_proto3_optional: bool):
24+
"""Populate a UUID4 field in the request if it is not already set.
25+
26+
Args:
27+
request (Union[google.protobuf.message.Message, dict]): The request object.
28+
field_name (str): The name of the field to populate.
29+
is_proto3_optional (bool): Whether the field is proto3 optional.
30+
"""
31+
if isinstance(request, dict):
32+
if is_proto3_optional:
33+
if field_name not in request:
34+
request[field_name] = str(uuid.uuid4())
35+
elif not request.get(field_name):
36+
request[field_name] = str(uuid.uuid4())
37+
return
38+
39+
if is_proto3_optional:
40+
try:
41+
# Pure protobuf messages
42+
if not request.HasField(field_name):
43+
setattr(request, field_name, str(uuid.uuid4()))
44+
except (AttributeError, ValueError):
45+
# Proto-plus messages or other objects
46+
if field_name not in request:
47+
setattr(request, field_name, str(uuid.uuid4()))
48+
else:
49+
if not getattr(request, field_name):
50+
setattr(request, field_name, str(uuid.uuid4()))
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
import uuid
18+
19+
try:
20+
from google.api_core.gapic_v1.requests import setup_request_id # type: ignore
21+
except ImportError:
22+
# TODO: Remove this fallback when google-api-core >= 2.18.0 is the minimum required version.
23+
def setup_request_id(request, field_name: str, is_proto3_optional: bool):
24+
"""Populate a UUID4 field in the request if it is not already set.
25+
26+
Args:
27+
request (Union[google.protobuf.message.Message, dict]): The request object.
28+
field_name (str): The name of the field to populate.
29+
is_proto3_optional (bool): Whether the field is proto3 optional.
30+
"""
31+
if isinstance(request, dict):
32+
if is_proto3_optional:
33+
if field_name not in request:
34+
request[field_name] = str(uuid.uuid4())
35+
elif not request.get(field_name):
36+
request[field_name] = str(uuid.uuid4())
37+
return
38+
39+
if is_proto3_optional:
40+
try:
41+
# Pure protobuf messages
42+
if not request.HasField(field_name):
43+
setattr(request, field_name, str(uuid.uuid4()))
44+
except (AttributeError, ValueError):
45+
# Proto-plus messages or other objects
46+
if field_name not in request:
47+
setattr(request, field_name, str(uuid.uuid4()))
48+
else:
49+
if not getattr(request, field_name):
50+
setattr(request, field_name, str(uuid.uuid4()))
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
import uuid
18+
19+
try:
20+
from google.api_core.gapic_v1.requests import setup_request_id # type: ignore
21+
except ImportError:
22+
# TODO: Remove this fallback when google-api-core >= 2.18.0 is the minimum required version.
23+
def setup_request_id(request, field_name: str, is_proto3_optional: bool):
24+
"""Populate a UUID4 field in the request if it is not already set.
25+
26+
Args:
27+
request (Union[google.protobuf.message.Message, dict]): The request object.
28+
field_name (str): The name of the field to populate.
29+
is_proto3_optional (bool): Whether the field is proto3 optional.
30+
"""
31+
if isinstance(request, dict):
32+
if is_proto3_optional:
33+
if field_name not in request:
34+
request[field_name] = str(uuid.uuid4())
35+
elif not request.get(field_name):
36+
request[field_name] = str(uuid.uuid4())
37+
return
38+
39+
if is_proto3_optional:
40+
try:
41+
# Pure protobuf messages
42+
if not request.HasField(field_name):
43+
setattr(request, field_name, str(uuid.uuid4()))
44+
except (AttributeError, ValueError):
45+
# Proto-plus messages or other objects
46+
if field_name not in request:
47+
setattr(request, field_name, str(uuid.uuid4()))
48+
else:
49+
if not getattr(request, field_name):
50+
setattr(request, field_name, str(uuid.uuid4()))
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
import uuid
18+
19+
try:
20+
from google.api_core.gapic_v1.requests import setup_request_id # type: ignore
21+
except ImportError:
22+
# TODO: Remove this fallback when google-api-core >= 2.18.0 is the minimum required version.
23+
def setup_request_id(request, field_name: str, is_proto3_optional: bool):
24+
"""Populate a UUID4 field in the request if it is not already set.
25+
26+
Args:
27+
request (Union[google.protobuf.message.Message, dict]): The request object.
28+
field_name (str): The name of the field to populate.
29+
is_proto3_optional (bool): Whether the field is proto3 optional.
30+
"""
31+
if isinstance(request, dict):
32+
if is_proto3_optional:
33+
if field_name not in request:
34+
request[field_name] = str(uuid.uuid4())
35+
elif not request.get(field_name):
36+
request[field_name] = str(uuid.uuid4())
37+
return
38+
39+
if is_proto3_optional:
40+
try:
41+
# Pure protobuf messages
42+
if not request.HasField(field_name):
43+
setattr(request, field_name, str(uuid.uuid4()))
44+
except (AttributeError, ValueError):
45+
# Proto-plus messages or other objects
46+
if field_name not in request:
47+
setattr(request, field_name, str(uuid.uuid4()))
48+
else:
49+
if not getattr(request, field_name):
50+
setattr(request, field_name, str(uuid.uuid4()))
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
import uuid
18+
19+
try:
20+
from google.api_core.gapic_v1.requests import setup_request_id # type: ignore
21+
except ImportError:
22+
# TODO: Remove this fallback when google-api-core >= 2.18.0 is the minimum required version.
23+
def setup_request_id(request, field_name: str, is_proto3_optional: bool):
24+
"""Populate a UUID4 field in the request if it is not already set.
25+
26+
Args:
27+
request (Union[google.protobuf.message.Message, dict]): The request object.
28+
field_name (str): The name of the field to populate.
29+
is_proto3_optional (bool): Whether the field is proto3 optional.
30+
"""
31+
if isinstance(request, dict):
32+
if is_proto3_optional:
33+
if field_name not in request:
34+
request[field_name] = str(uuid.uuid4())
35+
elif not request.get(field_name):
36+
request[field_name] = str(uuid.uuid4())
37+
return
38+
39+
if is_proto3_optional:
40+
try:
41+
# Pure protobuf messages
42+
if not request.HasField(field_name):
43+
setattr(request, field_name, str(uuid.uuid4()))
44+
except (AttributeError, ValueError):
45+
# Proto-plus messages or other objects
46+
if field_name not in request:
47+
setattr(request, field_name, str(uuid.uuid4()))
48+
else:
49+
if not getattr(request, field_name):
50+
setattr(request, field_name, str(uuid.uuid4()))
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
import uuid
18+
19+
try:
20+
from google.api_core.gapic_v1.requests import setup_request_id # type: ignore
21+
except ImportError:
22+
# TODO: Remove this fallback when google-api-core >= 2.18.0 is the minimum required version.
23+
def setup_request_id(request, field_name: str, is_proto3_optional: bool):
24+
"""Populate a UUID4 field in the request if it is not already set.
25+
26+
Args:
27+
request (Union[google.protobuf.message.Message, dict]): The request object.
28+
field_name (str): The name of the field to populate.
29+
is_proto3_optional (bool): Whether the field is proto3 optional.
30+
"""
31+
if isinstance(request, dict):
32+
if is_proto3_optional:
33+
if field_name not in request:
34+
request[field_name] = str(uuid.uuid4())
35+
elif not request.get(field_name):
36+
request[field_name] = str(uuid.uuid4())
37+
return
38+
39+
if is_proto3_optional:
40+
try:
41+
# Pure protobuf messages
42+
if not request.HasField(field_name):
43+
setattr(request, field_name, str(uuid.uuid4()))
44+
except (AttributeError, ValueError):
45+
# Proto-plus messages or other objects
46+
if field_name not in request:
47+
setattr(request, field_name, str(uuid.uuid4()))
48+
else:
49+
if not getattr(request, field_name):
50+
setattr(request, field_name, str(uuid.uuid4()))

0 commit comments

Comments
 (0)