Skip to content

Commit b5ffc42

Browse files
fix: Fix type hints in paths now that string projects are allowed. (#75)
* fix: Fix type hints in paths now that string projects are allowed. * fix: quickstart_test.py
1 parent d636182 commit b5ffc42

File tree

2 files changed

+22
-38
lines changed

2 files changed

+22
-38
lines changed

google/cloud/pubsublite/types/paths.py

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

15-
from typing import NamedTuple
15+
from typing import NamedTuple, Union
1616

1717
from google.api_core.exceptions import InvalidArgument
1818

1919
from google.cloud.pubsublite.types.location import CloudZone
2020

2121

2222
class LocationPath(NamedTuple):
23-
project_number: int
23+
project: Union[int, str]
2424
location: CloudZone
2525

2626
def __str__(self):
27-
return f"projects/{self.project_number}/locations/{self.location}"
27+
return f"projects/{self.project}/locations/{self.location}"
2828

2929

3030
class TopicPath(NamedTuple):
31-
project_number: int
31+
project: Union[int, str]
3232
location: CloudZone
3333
name: str
3434

3535
def __str__(self):
36-
return f"projects/{self.project_number}/locations/{self.location}/topics/{self.name}"
36+
return f"projects/{self.project}/locations/{self.location}/topics/{self.name}"
3737

3838
def to_location_path(self):
39-
return LocationPath(self.project_number, self.location)
39+
return LocationPath(self.project, self.location)
4040

4141
@staticmethod
4242
def parse(to_parse: str) -> "TopicPath":
@@ -51,27 +51,19 @@ def parse(to_parse: str) -> "TopicPath":
5151
"Topic path must be formatted like projects/{project_number}/locations/{location}/topics/{name} but was instead "
5252
+ to_parse
5353
)
54-
project_number: int
55-
try:
56-
project_number = int(splits[1])
57-
except ValueError:
58-
raise InvalidArgument(
59-
"Topic path must be formatted like projects/{project_number}/locations/{location}/topics/{name} but was instead "
60-
+ to_parse
61-
)
62-
return TopicPath(project_number, CloudZone.parse(splits[3]), splits[5])
54+
return TopicPath(splits[1], CloudZone.parse(splits[3]), splits[5])
6355

6456

6557
class SubscriptionPath(NamedTuple):
66-
project_number: int
58+
project: Union[int, str]
6759
location: CloudZone
6860
name: str
6961

7062
def __str__(self):
71-
return f"projects/{self.project_number}/locations/{self.location}/subscriptions/{self.name}"
63+
return f"projects/{self.project}/locations/{self.location}/subscriptions/{self.name}"
7264

7365
def to_location_path(self):
74-
return LocationPath(self.project_number, self.location)
66+
return LocationPath(self.project, self.location)
7567

7668
@staticmethod
7769
def parse(to_parse: str) -> "SubscriptionPath":
@@ -86,12 +78,4 @@ def parse(to_parse: str) -> "SubscriptionPath":
8678
"Subscription path must be formatted like projects/{project_number}/locations/{location}/subscriptions/{name} but was instead "
8779
+ to_parse
8880
)
89-
project_number: int
90-
try:
91-
project_number = int(splits[1])
92-
except ValueError:
93-
raise InvalidArgument(
94-
"Subscription path must be formatted like projects/{project_number}/locations/{location}/subscriptions/{name} but was instead "
95-
+ to_parse
96-
)
97-
return SubscriptionPath(project_number, CloudZone.parse(splits[3]), splits[5])
81+
return SubscriptionPath(splits[1], CloudZone.parse(splits[3]), splits[5])

samples/snippets/quickstart_test.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_create_lite_topic_example(topic_path, capsys):
7373

7474
topic_path_object = TopicPath.parse(topic_path)
7575
create_lite_topic_example.create_lite_topic(
76-
topic_path_object.project_number,
76+
topic_path_object.project,
7777
topic_path_object.location.region.name,
7878
topic_path_object.location.zone_id,
7979
topic_path_object.name,
@@ -88,7 +88,7 @@ def test_update_lite_topic_example(topic_path, capsys):
8888

8989
topic_path_object = TopicPath.parse(topic_path)
9090
update_lite_topic_example.update_lite_topic(
91-
topic_path_object.project_number,
91+
topic_path_object.project,
9292
topic_path_object.location.region.name,
9393
topic_path_object.location.zone_id,
9494
topic_path_object.name,
@@ -102,7 +102,7 @@ def test_get_lite_topic_example(topic_path, capsys):
102102

103103
topic_path_object = TopicPath.parse(topic_path)
104104
get_lite_topic_example.get_lite_topic(
105-
topic_path_object.project_number,
105+
topic_path_object.project,
106106
topic_path_object.location.region.name,
107107
topic_path_object.location.zone_id,
108108
topic_path_object.name,
@@ -116,7 +116,7 @@ def test_list_lite_topics_example(topic_path, capsys):
116116

117117
topic_path_object = TopicPath.parse(topic_path)
118118
list_lite_topics_example.list_lite_topics(
119-
topic_path_object.project_number,
119+
topic_path_object.project,
120120
topic_path_object.location.region.name,
121121
topic_path_object.location.zone_id,
122122
)
@@ -131,7 +131,7 @@ def test_create_lite_subscription(subscription_path, topic_path, capsys):
131131
topic_path_object = TopicPath.parse(topic_path)
132132

133133
create_lite_subscription_example.create_lite_subscription(
134-
subscription_path_object.project_number,
134+
subscription_path_object.project,
135135
subscription_path_object.location.region.name,
136136
subscription_path_object.location.zone_id,
137137
topic_path_object.name,
@@ -147,7 +147,7 @@ def test_update_lite_subscription_example(subscription_path, capsys):
147147
subscription_path_object = SubscriptionPath.parse(subscription_path)
148148

149149
update_lite_subscription_example.update_lite_subscription(
150-
subscription_path_object.project_number,
150+
subscription_path_object.project,
151151
subscription_path_object.location.region.name,
152152
subscription_path_object.location.zone_id,
153153
subscription_path_object.name,
@@ -162,7 +162,7 @@ def test_get_lite_subscription(subscription_path, capsys):
162162
subscription_path_object = SubscriptionPath.parse(subscription_path)
163163

164164
get_lite_subscription_example.get_lite_subscription(
165-
subscription_path_object.project_number,
165+
subscription_path_object.project,
166166
subscription_path_object.location.region.name,
167167
subscription_path_object.location.zone_id,
168168
subscription_path_object.name,
@@ -177,7 +177,7 @@ def test_list_lite_subscriptions_in_project(subscription_path, capsys):
177177
subscription_path_object = SubscriptionPath.parse(subscription_path)
178178

179179
list_lite_subscriptions_in_project_example.list_lite_subscriptions_in_project(
180-
subscription_path_object.project_number,
180+
subscription_path_object.project,
181181
subscription_path_object.location.region.name,
182182
subscription_path_object.location.zone_id,
183183
)
@@ -192,7 +192,7 @@ def test_list_lite_subscriptions_in_topic(topic_path, subscription_path, capsys)
192192
topic_path_object = TopicPath.parse(topic_path)
193193

194194
list_lite_subscriptions_in_topic_example.list_lite_subscriptions_in_topic(
195-
subscription_path_object.project_number,
195+
subscription_path_object.project,
196196
subscription_path_object.location.region.name,
197197
subscription_path_object.location.zone_id,
198198
topic_path_object.name,
@@ -267,7 +267,7 @@ def test_delete_lite_subscription_example(subscription_path, capsys):
267267
@backoff.on_exception(backoff.expo, AssertionError, max_time=MAX_TIME)
268268
def eventually_consistent_test():
269269
delete_lite_subscription_example.delete_lite_subscription(
270-
subscription_path_object.project_number,
270+
subscription_path_object.project,
271271
subscription_path_object.location.region.name,
272272
subscription_path_object.location.zone_id,
273273
subscription_path_object.name,
@@ -286,7 +286,7 @@ def test_delete_lite_topic_example(topic_path, capsys):
286286
@backoff.on_exception(backoff.expo, AssertionError, max_time=MAX_TIME)
287287
def eventually_consistent_test():
288288
delete_lite_topic_example.delete_lite_topic(
289-
topic_path_object.project_number,
289+
topic_path_object.project,
290290
topic_path_object.location.region.name,
291291
topic_path_object.location.zone_id,
292292
topic_path_object.name,

0 commit comments

Comments
 (0)