Skip to content

Commit b00d037

Browse files
[STYLE] Minor improvements from review (- WIP PR #373 -)
* This work is related to GHI #213 Changes in file docs/utils.py: * improved docstrings * related work
1 parent e193d62 commit b00d037

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

docs/utils.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,49 @@
3939
URL_ALLOWED_NETLOCS = {"github.com", "readthedocs.com", "docs.python.org"}
4040

4141

42+
# Error messages for URL validation
43+
INVALID_SCHEME_ERROR = "Invalid URL scheme. Only 'https' is allowed."
44+
"""Scheme error message for URL validation.
45+
46+
Unit-Testing:
47+
48+
First set up test fixtures by importing utils.
49+
50+
>>> import docs.utils as _utils
51+
>>>
52+
53+
>>> _utils.INVALID_SCHEME_ERROR is not None
54+
True
55+
>>> type(_utils.INVALID_SCHEME_ERROR) is type(str())
56+
True
57+
>>> len(_utils.INVALID_SCHEME_ERROR) > 0
58+
True
59+
>>>
60+
61+
"""
62+
63+
64+
INVALID_DOMAIN_ERROR = f"Invalid or untrusted domain. Only {URL_ALLOWED_NETLOCS} are allowed."
65+
"""Domain error message for URL validation.
66+
67+
Unit-Testing:
68+
69+
First set up test fixtures by importing utils.
70+
71+
>>> import docs.utils as _utils
72+
>>>
73+
74+
>>> _utils.INVALID_DOMAIN_ERROR is not None
75+
True
76+
>>> type(_utils.INVALID_DOMAIN_ERROR) is type(str())
77+
True
78+
>>> len(_utils.INVALID_DOMAIN_ERROR) > 0
79+
True
80+
>>>
81+
82+
"""
83+
84+
4285
def _validate_git_ref(ref: str) -> str:
4386
"""
4487
Validate if the provided string is a valid Git reference.
@@ -171,10 +214,10 @@ def sanitize_url(url: str) -> str:
171214
parsed_url = urlparse(url)
172215
# Validate scheme
173216
if parsed_url.scheme not in URL_ALLOWED_SCHEMES:
174-
raise ValueError("Invalid URL scheme. Only 'https' is allowed.")
217+
raise ValueError(INVALID_SCHEME_ERROR)
175218
# Validate netloc
176219
if parsed_url.netloc not in URL_ALLOWED_NETLOCS:
177-
raise ValueError(f"Invalid or untrusted domain. Only {URL_ALLOWED_NETLOCS} are allowed.")
220+
raise ValueError(INVALID_DOMAIN_ERROR)
178221
# Sanitize path and query
179222
sanitized_path = quote(parsed_url.path)
180223
sanitized_query = quote(parsed_url.query)

0 commit comments

Comments
 (0)