Skip to content

Commit

Permalink
Add ignore_docs decorator to all internal utils
Browse files Browse the repository at this point in the history
  • Loading branch information
fnesveda committed Aug 1, 2023
1 parent 058472e commit 7b377ac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

[1.0.1](../../releases/tag/v1.0.1) - 2023-08-01

Added the `ignore_docs` decorator to all internal utilities.

[1.0.0](../../releases/tag/v1.0.0) - 2023-07-25

Initial release of the package.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ license = {text = "Apache Software License"}
name = "apify_shared"
readme = "README.md"
requires-python = ">=3.8"
version = "1.0.0"
version = "1.0.1"

[project.optional-dependencies]
dev = [
Expand All @@ -37,7 +37,7 @@ dev = [
"flake8-quotes ~= 3.3.2",
"flake8-unused-arguments ~= 0.0.13",
"isort ~= 5.12.0",
"mypy ~= 1.3.0",
"mypy ~= 1.4.0",
"pep8-naming ~= 0.13.3",
"pre-commit ~= 3.3.2",
"pytest ~= 7.3.1",
Expand Down
21 changes: 15 additions & 6 deletions src/apify_shared/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@
T = TypeVar('T')


def ignore_docs(method: T) -> T:
"""Mark that a method's documentation should not be rendered. Functionally, this decorator is a noop."""
return method


@ignore_docs
def filter_out_none_values_recursively(dictionary: Dict) -> Dict:
"""Return copy of the dictionary, recursively omitting all keys for which values are None."""
return cast(dict, _filter_out_none_values_recursively_internal(dictionary))


@ignore_docs
def _filter_out_none_values_recursively_internal(
dictionary: Dict,
remove_empty_dicts: Optional[bool] = None,
Expand All @@ -37,26 +44,25 @@ def _filter_out_none_values_recursively_internal(
return result


def ignore_docs(method: T) -> T:
"""Mark that a method's documentation should not be rendered. Functionally, this decorator is a noop."""
return method


@ignore_docs
def is_content_type_json(content_type: str) -> bool:
"""Check if the given content type is JSON."""
return bool(re.search(r'^application/json', content_type, flags=re.IGNORECASE))


@ignore_docs
def is_content_type_xml(content_type: str) -> bool:
"""Check if the given content type is XML."""
return bool(re.search(r'^application/.*xml$', content_type, flags=re.IGNORECASE))


@ignore_docs
def is_content_type_text(content_type: str) -> bool:
"""Check if the given content type is text."""
return bool(re.search(r'^text/', content_type, flags=re.IGNORECASE))


@ignore_docs
def is_file_or_bytes(value: Any) -> bool:
"""Check if the input value is a file-like object or bytes.
Expand All @@ -67,18 +73,21 @@ def is_file_or_bytes(value: Any) -> bool:
return isinstance(value, (bytes, bytearray, io.IOBase))


@ignore_docs
def json_dumps(obj: Any) -> str:
"""Dump JSON to a string with the correct settings and serializer."""
return json.dumps(obj, ensure_ascii=False, indent=2, default=str)


@ignore_docs
def maybe_extract_enum_member_value(maybe_enum_member: Any) -> Any:
"""Extract the value of an enumeration member if it is an Enum, otherwise return the original value."""
if isinstance(maybe_enum_member, Enum):
return maybe_enum_member.value
return maybe_enum_member


@ignore_docs
def parse_date_fields(data: ListOrDict, max_depth: int = PARSE_DATE_FIELDS_MAX_DEPTH) -> ListOrDict:
"""Recursively parse date fields in a list or dictionary up to the specified depth."""
if max_depth < 0:
Expand All @@ -98,7 +107,7 @@ def parse(key: str, value: object) -> object:
elif isinstance(value, dict):
parsed_value = parse_date_fields(value, max_depth - 1)
elif isinstance(value, list):
parsed_value = parse_date_fields(value, max_depth)
parsed_value = parse_date_fields(value, max_depth) # type: ignore # mypy doesn't work with decorators and recursive calls well
return parsed_value

return {key: parse(key, value) for (key, value) in data.items()}
Expand Down

0 comments on commit 7b377ac

Please sign in to comment.