Skip to content

Commit

Permalink
Add missing docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
vemel committed Apr 16, 2024
1 parent e7a789a commit f6adc8a
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 3 deletions.
3 changes: 3 additions & 0 deletions mypy_boto3_builder/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class Product(Enum):
aioboto3_docs = "aioboto3-docs"

def __str__(self) -> str:
"""
Get string representation for debugging.
"""
return self.value

def get_library(self) -> ProductLibrary:
Expand Down
17 changes: 17 additions & 0 deletions mypy_boto3_builder/import_helpers/import_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def __init__(
self.fallback = fallback

def __bool__(self) -> bool:
"""
Whether import record is an empty string.
"""
return not self.is_empty()

def is_empty(self) -> bool:
Expand Down Expand Up @@ -76,18 +79,32 @@ def render(self) -> str:
return ""

def __str__(self) -> str:
"""
Render as a valid Python import statement.
"""
return self.render()

def __hash__(self) -> int:
"""
Calculate hash value based on source, name and alias.
"""
return hash(str(self))

def __eq__(self, other: object) -> bool:
"""
Whether two import records produce the same render.
"""
if not isinstance(other, ImportRecord):
return False

return str(self) == str(other)

def __gt__(self: Self, other: Self) -> bool:
"""
Compare two import records for sorting.
Emulates `isort` logic.
"""
if self.fallback is not None and other.fallback is None:
return True

Expand Down
20 changes: 20 additions & 0 deletions mypy_boto3_builder/import_helpers/import_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,41 @@ def parent(cls: type[Self]) -> Self:
return result

def __bool__(self) -> bool:
"""
Whether import string is not empty.
"""
return bool(self.parts)

def __str__(self) -> str:
"""
Render as a part of a valid Python import statement.
"""
return self.render()

def __hash__(self) -> int:
"""
Calculate hash value based on all parts.
"""
return hash(str(self))

def __eq__(self, other: object) -> bool:
"""
Whether import strings produce the same render.
"""
return str(self) == str(other)

def __gt__(self, other: object) -> bool:
"""
Compare import strings for sorting.
Emulates `isort` logic.
"""
return str(self) > str(other)

def __add__(self: Self, other: Self | str) -> Self:
"""
Create a new import string by adding another import string parts to the end.
"""
other_import_string = other if isinstance(other, ImportString) else ImportString(other)
result = self.__class__.empty()
result.parts = self.parts + other_import_string.parts
Expand Down
6 changes: 6 additions & 0 deletions mypy_boto3_builder/service_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ def __init__(self, name: str, class_name: str, override_boto3_name: str = "") ->
self.override_boto3_name = override_boto3_name

def __hash__(self) -> int:
"""
Calculate hash value based on service name.
"""
return hash(self.name)

def __str__(self) -> str:
"""
Represent as string for debugging.
"""
return f"<ServiceName {self.name} {self.class_name}>"

@property
Expand Down
3 changes: 3 additions & 0 deletions mypy_boto3_builder/structures/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def __init__(
self.is_async = is_async

def __repr__(self) -> str:
"""
Represent as a valid Python function signature.
"""
return (
f"{'async ' if self.is_async else ''}def"
f" {self.name}({', '.join(argument.render() for argument in self.arguments)}) ->"
Expand Down
3 changes: 3 additions & 0 deletions mypy_boto3_builder/structures/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def directory_name(self) -> str:
return f"{underscore_package_name}_package"

def __str__(self) -> str:
"""
Get string representation for debugging.
"""
return f"{self.name} {self.version} ({self.library_name} {self.library_version})"

def get_local_doc_link(self, service_name: ServiceName | None = None) -> str:
Expand Down
3 changes: 3 additions & 0 deletions mypy_boto3_builder/structures/service_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def __init__(
self.sub_resources: list[Resource] = []

def __hash__(self) -> int:
"""
Calculate hash value based on service name.
"""
return hash(self.service_name)

@property
Expand Down
3 changes: 3 additions & 0 deletions mypy_boto3_builder/type_annotations/external_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ def import_record(self) -> ImportRecord:
return ImportRecord(source=self.source, name=self.name, alias=self.alias)

def __hash__(self) -> int:
"""
Calcualte hash value based on import record.
"""
return hash(self.import_record.render())

def render(self) -> str:
Expand Down
15 changes: 15 additions & 0 deletions mypy_boto3_builder/type_annotations/fake_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,30 @@ class FakeAnnotation(ABC):
_sys_import_record = ImportRecord(ImportString("sys"))

def __hash__(self) -> int:
"""
Calculate hash value based on string render.
"""
return hash(self.render())

def __eq__(self, other: object) -> bool:
"""
Whether two annotations are equal.
"""
if not isinstance(other, FakeAnnotation):
return False

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

def __lt__(self: Self, other: Self) -> bool:
"""
Compare two annotations for sorting.
"""
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()

def get_sort_key(self) -> str:
Expand All @@ -40,6 +52,9 @@ def get_sort_key(self) -> str:
return str(self)

def __str__(self) -> str:
"""
Render annotation usage as a valid Python statement.
"""
return self.render()

@abstractmethod
Expand Down
12 changes: 10 additions & 2 deletions mypy_boto3_builder/type_annotations/type_def_sortable.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@ class TypeDefSortable(Protocol):
name: str
_stringify: bool

def __gt__(self, other: FakeAnnotation) -> bool: ...
def __gt__(self, other: FakeAnnotation) -> bool:
"""
Compare with another TypeDefSortable. Has to be implemented.
"""
...

def __lt__(self, other: FakeAnnotation) -> bool: ...
def __lt__(self, other: FakeAnnotation) -> bool:
"""
Compare with another TypeDefSortable. Has to be implemented.
"""
...

def get_sortable_children(self) -> list["TypeDefSortable"]:
"""
Expand Down
3 changes: 3 additions & 0 deletions mypy_boto3_builder/type_annotations/type_subscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def __init__(
self._stringify = stringify

def __hash__(self) -> int:
"""
Calculate hash value based on parent and children.
"""
return hash(f"{self.parent}.{self.children}")

def render(self) -> str:
Expand Down
6 changes: 6 additions & 0 deletions mypy_boto3_builder/type_annotations/type_typed_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def __init__(self, name: str, type_annotation: FakeAnnotation, required: bool) -
self.type_annotation = type_annotation

def __hash__(self) -> int:
"""
Calculate hash value based on name, required and type annotation.
"""
return hash((self.name, self.required, self.type_annotation.get_sort_key()))

def get_type_annotation(self) -> FakeAnnotation:
Expand Down Expand Up @@ -109,6 +112,9 @@ def get_sort_key(self) -> str:
return self.name

def __hash__(self) -> int:
"""
Calculate hash value based on name and children.
"""
return hash((self.name, *[hash(i) for i in self.children]))

def render(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,5 @@ ignore_errors = true
[tool.flake8]
max-line-length = 100
exclude = [".git", "__pycache__", "docs", "build", "dist", "test_*.py", "*.pyi"]
ignore = ["E704", "W503", "D200", "D107", "D105"]
ignore = ["E704", "W503", "D200", "D107"]
max-complexity = 14

0 comments on commit f6adc8a

Please sign in to comment.