I'm unsure if this question belongs on Code Review or Stack Overflow.
Another developer told me that I should consider adding type hints to my open source project WordHoard.
I have never used type hints before and the documentation doesn't seem intuitive. So I need to guidance on how to implement this functionality in my code.
I've started looking at adding it some low level code pieces first.
For example:
from typing import AnyStr, Dict
def colorized_text(r: int, g: int, b: int, text: str) -> AnyStr:
"""
This function provides error messages color.
For example:
rgb(255, 0, 0) is displayed as the color red
rgb(0, 255, 0) is displayed as the color green
:param r: red color value
:param g: green color value
:param b: below color value
:param text: text to colorized
:return: string of colorized text
"""
return f"\033[38;2;{r};{g};{b}m{text}\033[0m"
Are type hints implemented correctly in the function colorized_text?
Here is another code example.
from typing import Optional
temporary_dict_antonyms = {}
def cache_antonyms(word: str) -> [bool, str]:
item_to_check = word
if item_to_check in temporary_dict_antonyms.keys():
values = temporary_dict_antonyms.get(item_to_check)
return True, list(sorted(set(values)))
else:
return False, None
def insert_word_cache_antonyms(word: str, values: list[str]) -> None:
if word in temporary_dict_antonyms:
deduplicated_values = set(values) - set(temporary_dict_antonyms.get(word))
temporary_dict_antonyms[word].extend(deduplicated_values)
else:
values = [value.strip() for value in values]
temporary_dict_antonyms[word] = values
How do I implement type hints correctly in the second example?
Thanks in advance for any guidance!
.append(values). It seems more likely you intended.extend(values), leading to a single flat list of antonyms, rather than a nested list structure. \$\endgroup\$