Skip to content

Commit

Permalink
Support total_ordering for waiters and paginators
Browse files Browse the repository at this point in the history
  • Loading branch information
vemel committed Oct 26, 2024
1 parent 76eea70 commit afd7436
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
32 changes: 32 additions & 0 deletions mypy_boto3_builder/structures/paginator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
Boto3 client Paginator.
"""

import functools
from typing import Self

from botocore.paginate import Paginator as BotocorePaginator

from mypy_boto3_builder.enums.service_module_name import ServiceModuleName
from mypy_boto3_builder.exceptions import BuildInternalError
from mypy_boto3_builder.import_helpers.import_string import ImportString
from mypy_boto3_builder.service_name import ServiceName
from mypy_boto3_builder.structures.argument import Argument
Expand All @@ -15,6 +19,7 @@
from mypy_boto3_builder.type_annotations.type_literal import TypeLiteral


@functools.total_ordering
class Paginator(ClassRecord):
"""
Boto3 client Paginator.
Expand All @@ -35,6 +40,33 @@ def __init__(
self.paginator_name = paginator_name
self.service_name = service_name

def __hash__(self) -> int:
"""
Hash paginators by name.
"""
return hash(self.get_sort_key())

def get_sort_key(self) -> str:
"""
Sort paginators by name.
"""
return self.name

def __eq__(self, other: object) -> bool:
"""
Whether two annotations are equal.
"""
if not isinstance(other, Paginator):
raise BuildInternalError(f"{other} is not Paginator")

return self.get_sort_key() == other.get_sort_key()

def __gt__(self: Self, other: Self) -> bool:
"""
Compare two annotations for sorting.
"""
return self.get_sort_key() > other.get_sort_key()

@property
def boto3_doc_link(self) -> str:
"""
Expand Down
32 changes: 32 additions & 0 deletions mypy_boto3_builder/structures/waiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
Boto3 client Waiter.
"""

import functools
from typing import Self

from botocore.waiter import Waiter as BotocoreWaiter

from mypy_boto3_builder.enums.service_module_name import ServiceModuleName
from mypy_boto3_builder.exceptions import BuildInternalError
from mypy_boto3_builder.import_helpers.import_string import ImportString
from mypy_boto3_builder.service_name import ServiceName
from mypy_boto3_builder.structures.argument import Argument
Expand All @@ -15,6 +19,7 @@
from mypy_boto3_builder.type_annotations.type_literal import TypeLiteral


@functools.total_ordering
class Waiter(ClassRecord):
"""
Boto3 client Waiter.
Expand All @@ -33,6 +38,33 @@ def __init__(
self.waiter_name = waiter_name
self.service_name = service_name

def __hash__(self) -> int:
"""
Hash paginators by name.
"""
return hash(self.get_sort_key())

def get_sort_key(self) -> str:
"""
Sort paginators by name.
"""
return self.name

def __eq__(self, other: object) -> bool:
"""
Whether two annotations are equal.
"""
if not isinstance(other, Waiter):
raise BuildInternalError(f"{other} is not Waiter")

return self.get_sort_key() == other.get_sort_key()

def __gt__(self: Self, other: Self) -> bool:
"""
Compare two annotations for sorting.
"""
return self.get_sort_key() > other.get_sort_key()

@property
def boto3_doc_link(self) -> str:
"""
Expand Down

0 comments on commit afd7436

Please sign in to comment.