Skip to content

Commit 52bb570

Browse files
dhirukumarrudransh-shrivastavaarkid15r
authored
Add type hints to utils.py files (#2670)
* Add type hints to utils.py files * fix: added type hints to utility functions across common, github, and slack apps * Add type hints to utils.py files (common, github, slack) * make all changes according to issue and the suggistationfrom rudransh-shrivastava * Update code * Update code * Update code * Fix timeout type annotation to match requests library * Update code --------- Co-authored-by: Rudransh Shrivastava <[email protected]> Co-authored-by: Arkadii Yakovets <[email protected]>
1 parent 97e5e44 commit 52bb570

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

backend/apps/common/utils.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
import re
77
from datetime import UTC, datetime
8+
from typing import TYPE_CHECKING
89
from urllib.parse import urlparse
910

1011
from django.conf import settings
@@ -13,6 +14,9 @@
1314
from django.utils.text import slugify as django_slugify
1415
from humanize import intword, naturaltime
1516

17+
if TYPE_CHECKING:
18+
from django.http import HttpRequest
19+
1620

1721
def convert_to_camel_case(text: str) -> str:
1822
"""Convert a string to camelCase.
@@ -47,11 +51,11 @@ def convert_to_snake_case(text: str) -> str:
4751
return re.sub(r"(?<!^)(?=[A-Z])", "_", text).lower()
4852

4953

50-
def clean_url(url: str) -> str | None:
54+
def clean_url(url: str | None) -> str | None:
5155
"""Clean a URL by removing whitespace and trailing punctuation.
5256
5357
Args:
54-
url (str): Raw URL string.
58+
url (str, optional): Raw URL string.
5559
5660
Returns:
5761
str | None: Cleaned URL string or None if empty.
@@ -79,14 +83,14 @@ def get_absolute_url(path: str) -> str:
7983
def get_nest_user_agent() -> str:
8084
"""Return the user agent string for the Nest application.
8185
82-
Returns
86+
Returns:
8387
str: The user agent string.
8488
8589
"""
8690
return settings.APP_NAME.replace(" ", "-").lower()
8791

8892

89-
def get_user_ip_address(request) -> str:
93+
def get_user_ip_address(request: HttpRequest) -> str:
9094
"""Retrieve the user's IP address from the request.
9195
9296
Args:
@@ -134,11 +138,11 @@ def join_values(fields: list, delimiter: str = " ") -> str:
134138
return delimiter.join(field for field in fields if field)
135139

136140

137-
def natural_date(value: int | str) -> str:
141+
def natural_date(value: int | str | datetime) -> str:
138142
"""Convert a date or timestamp into a human-readable format.
139143
140144
Args:
141-
value (str or int or datetime): The date or timestamp to convert.
145+
value (int or str or datetime): The date or timestamp to convert.
142146
143147
Returns:
144148
str: The humanized date string.
@@ -154,7 +158,7 @@ def natural_date(value: int | str) -> str:
154158
return naturaltime(dt)
155159

156160

157-
def natural_number(value: int, unit=None) -> str:
161+
def natural_number(value: int, unit: str | None = None) -> str:
158162
"""Convert a number into a human-readable format.
159163
160164
Args:
@@ -173,8 +177,8 @@ def round_down(value: int, base: int) -> int:
173177
"""Round down the stats to the nearest base.
174178
175179
Args:
176-
value: The value to round down.
177-
base: The base to round down to.
180+
value (int): The value to round down.
181+
base (int): The base to round down to.
178182
179183
Returns:
180184
int: The rounded down value.
@@ -211,11 +215,11 @@ def truncate(text: str, limit: int, truncate: str = "...") -> str:
211215
return Truncator(text).chars(limit, truncate=truncate)
212216

213217

214-
def validate_url(url: str) -> bool:
218+
def validate_url(url: str | None) -> bool:
215219
"""Validate that a URL has proper scheme and netloc.
216220
217221
Args:
218-
url (str): URL string to validate.
222+
url (str, optional): URL string to validate.
219223
220224
Returns:
221225
bool: True if URL is valid, False otherwise.

backend/apps/github/utils.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ def check_owasp_site_repository(key: str) -> bool:
3333
)
3434

3535

36-
def check_funding_policy_compliance(platform: str, target: str) -> bool:
36+
def check_funding_policy_compliance(platform: str, target: str | None) -> bool:
3737
"""Check OWASP funding policy compliance.
3838
3939
Args:
4040
platform (str): The funding platform (e.g., 'github', 'custom').
41-
target (str): The funding target.
41+
target (str, optional): The funding target.
4242
4343
Returns:
4444
bool: True if the funding policy is compliant, False otherwise.
@@ -57,19 +57,15 @@ def check_funding_policy_compliance(platform: str, target: str) -> bool:
5757
return False
5858

5959

60-
def get_repository_file_content(
61-
url: str,
62-
*,
63-
timeout: float | None = 30,
64-
) -> str:
60+
def get_repository_file_content(url: str, *, timeout: float | None = 30) -> str:
6561
"""Get the content of a file from a repository.
6662
6763
Args:
6864
url (str): The URL of the file.
69-
timeout (int, optional): The request timeout in seconds.
65+
timeout (float, optional): The request timeout in seconds.
7066
7167
Returns:
72-
str: The content of the file, or None if the request fails.
68+
str: The content of the file, or empty string if the request fails.
7369
7470
"""
7571
try:
@@ -86,7 +82,8 @@ def get_repository_path(url: str) -> str | None:
8682
url (str): The repository URL.
8783
8884
Returns:
89-
str: The repository path in the format 'owner/repository_name', or None if parsing fails.
85+
str or None: The repository path in the format 'owner/repository_name',
86+
or None if parsing fails.
9087
9188
"""
9289
match = GITHUB_REPOSITORY_RE.search(url.split("#")[0])
@@ -101,7 +98,7 @@ def normalize_url(url: str, *, check_path: bool = False) -> str | None:
10198
check_path (bool, optional): Whether to check if the URL has a path.
10299
103100
Returns:
104-
str: The normalized URL, or None if the URL is invalid.
101+
str | None: The normalized URL, or None if the URL is invalid.
105102
106103
"""
107104
parsed_url = urlparse(url)

backend/apps/slack/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
logger: logging.Logger = logging.getLogger(__name__)
2323

2424

25-
def escape(content) -> str:
25+
def escape(content: str) -> str:
2626
"""Escape HTML content.
2727
2828
Args:
@@ -83,7 +83,7 @@ def get_news_data(limit: int = 10, timeout: float | None = 30) -> list[dict[str,
8383
8484
Args:
8585
limit (int, optional): The maximum number of news items to fetch.
86-
timeout (int, optional): The request timeout in seconds.
86+
timeout (float, optional): The request timeout in seconds.
8787
8888
Returns:
8989
list: A list of dictionaries containing news data (author, title, and URL).
@@ -118,7 +118,7 @@ def get_staff_data(timeout: float | None = 30) -> list | None:
118118
"""Get staff data.
119119
120120
Args:
121-
timeout (int, optional): The request timeout in seconds.
121+
timeout (float, optional): The request timeout in seconds.
122122
123123
Returns:
124124
list or None: A sorted list of staff data dictionaries, or None if an error occurs.

0 commit comments

Comments
 (0)