Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix type hints in paths now that string projects are allowed. #75

Merged
merged 2 commits into from Dec 14, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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])