Skip to content

Commit

Permalink
fix: Fix type hints in paths now that string projects are allowed. (#75)
Browse files Browse the repository at this point in the history
* fix: Fix type hints in paths now that string projects are allowed.

* fix: quickstart_test.py
  • Loading branch information
dpcollins-google committed Dec 14, 2020
1 parent d636182 commit b5ffc42
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 38 deletions.
38 changes: 11 additions & 27 deletions google/cloud/pubsublite/types/paths.py
Expand Up @@ -12,31 +12,31 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import NamedTuple
from typing import NamedTuple, Union

from google.api_core.exceptions import InvalidArgument

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


class LocationPath(NamedTuple):
project_number: int
project: Union[int, str]
location: CloudZone

def __str__(self):
return f"projects/{self.project_number}/locations/{self.location}"
return f"projects/{self.project}/locations/{self.location}"


class TopicPath(NamedTuple):
project_number: int
project: Union[int, str]
location: CloudZone
name: str

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

def to_location_path(self):
return LocationPath(self.project_number, self.location)
return LocationPath(self.project, self.location)

@staticmethod
def parse(to_parse: str) -> "TopicPath":
Expand All @@ -51,27 +51,19 @@ def parse(to_parse: str) -> "TopicPath":
"Topic path must be formatted like projects/{project_number}/locations/{location}/topics/{name} but was instead "
+ to_parse
)
project_number: int
try:
project_number = int(splits[1])
except ValueError:
raise InvalidArgument(
"Topic path must be formatted like projects/{project_number}/locations/{location}/topics/{name} but was instead "
+ to_parse
)
return TopicPath(project_number, CloudZone.parse(splits[3]), splits[5])
return TopicPath(splits[1], CloudZone.parse(splits[3]), splits[5])


class SubscriptionPath(NamedTuple):
project_number: int
project: Union[int, str]
location: CloudZone
name: str

def __str__(self):
return f"projects/{self.project_number}/locations/{self.location}/subscriptions/{self.name}"
return f"projects/{self.project}/locations/{self.location}/subscriptions/{self.name}"

def to_location_path(self):
return LocationPath(self.project_number, self.location)
return LocationPath(self.project, self.location)

@staticmethod
def parse(to_parse: str) -> "SubscriptionPath":
Expand All @@ -86,12 +78,4 @@ def parse(to_parse: str) -> "SubscriptionPath":
"Subscription path must be formatted like projects/{project_number}/locations/{location}/subscriptions/{name} but was instead "
+ to_parse
)
project_number: int
try:
project_number = int(splits[1])
except ValueError:
raise InvalidArgument(
"Subscription path must be formatted like projects/{project_number}/locations/{location}/subscriptions/{name} but was instead "
+ to_parse
)
return SubscriptionPath(project_number, CloudZone.parse(splits[3]), splits[5])
return SubscriptionPath(splits[1], CloudZone.parse(splits[3]), splits[5])
22 changes: 11 additions & 11 deletions samples/snippets/quickstart_test.py
Expand Up @@ -73,7 +73,7 @@ def test_create_lite_topic_example(topic_path, capsys):

topic_path_object = TopicPath.parse(topic_path)
create_lite_topic_example.create_lite_topic(
topic_path_object.project_number,
topic_path_object.project,
topic_path_object.location.region.name,
topic_path_object.location.zone_id,
topic_path_object.name,
Expand All @@ -88,7 +88,7 @@ def test_update_lite_topic_example(topic_path, capsys):

topic_path_object = TopicPath.parse(topic_path)
update_lite_topic_example.update_lite_topic(
topic_path_object.project_number,
topic_path_object.project,
topic_path_object.location.region.name,
topic_path_object.location.zone_id,
topic_path_object.name,
Expand All @@ -102,7 +102,7 @@ def test_get_lite_topic_example(topic_path, capsys):

topic_path_object = TopicPath.parse(topic_path)
get_lite_topic_example.get_lite_topic(
topic_path_object.project_number,
topic_path_object.project,
topic_path_object.location.region.name,
topic_path_object.location.zone_id,
topic_path_object.name,
Expand All @@ -116,7 +116,7 @@ def test_list_lite_topics_example(topic_path, capsys):

topic_path_object = TopicPath.parse(topic_path)
list_lite_topics_example.list_lite_topics(
topic_path_object.project_number,
topic_path_object.project,
topic_path_object.location.region.name,
topic_path_object.location.zone_id,
)
Expand All @@ -131,7 +131,7 @@ def test_create_lite_subscription(subscription_path, topic_path, capsys):
topic_path_object = TopicPath.parse(topic_path)

create_lite_subscription_example.create_lite_subscription(
subscription_path_object.project_number,
subscription_path_object.project,
subscription_path_object.location.region.name,
subscription_path_object.location.zone_id,
topic_path_object.name,
Expand All @@ -147,7 +147,7 @@ def test_update_lite_subscription_example(subscription_path, capsys):
subscription_path_object = SubscriptionPath.parse(subscription_path)

update_lite_subscription_example.update_lite_subscription(
subscription_path_object.project_number,
subscription_path_object.project,
subscription_path_object.location.region.name,
subscription_path_object.location.zone_id,
subscription_path_object.name,
Expand All @@ -162,7 +162,7 @@ def test_get_lite_subscription(subscription_path, capsys):
subscription_path_object = SubscriptionPath.parse(subscription_path)

get_lite_subscription_example.get_lite_subscription(
subscription_path_object.project_number,
subscription_path_object.project,
subscription_path_object.location.region.name,
subscription_path_object.location.zone_id,
subscription_path_object.name,
Expand All @@ -177,7 +177,7 @@ def test_list_lite_subscriptions_in_project(subscription_path, capsys):
subscription_path_object = SubscriptionPath.parse(subscription_path)

list_lite_subscriptions_in_project_example.list_lite_subscriptions_in_project(
subscription_path_object.project_number,
subscription_path_object.project,
subscription_path_object.location.region.name,
subscription_path_object.location.zone_id,
)
Expand All @@ -192,7 +192,7 @@ def test_list_lite_subscriptions_in_topic(topic_path, subscription_path, capsys)
topic_path_object = TopicPath.parse(topic_path)

list_lite_subscriptions_in_topic_example.list_lite_subscriptions_in_topic(
subscription_path_object.project_number,
subscription_path_object.project,
subscription_path_object.location.region.name,
subscription_path_object.location.zone_id,
topic_path_object.name,
Expand Down Expand Up @@ -267,7 +267,7 @@ def test_delete_lite_subscription_example(subscription_path, capsys):
@backoff.on_exception(backoff.expo, AssertionError, max_time=MAX_TIME)
def eventually_consistent_test():
delete_lite_subscription_example.delete_lite_subscription(
subscription_path_object.project_number,
subscription_path_object.project,
subscription_path_object.location.region.name,
subscription_path_object.location.zone_id,
subscription_path_object.name,
Expand All @@ -286,7 +286,7 @@ def test_delete_lite_topic_example(topic_path, capsys):
@backoff.on_exception(backoff.expo, AssertionError, max_time=MAX_TIME)
def eventually_consistent_test():
delete_lite_topic_example.delete_lite_topic(
topic_path_object.project_number,
topic_path_object.project,
topic_path_object.location.region.name,
topic_path_object.location.zone_id,
topic_path_object.name,
Expand Down

0 comments on commit b5ffc42

Please sign in to comment.