Skip to content

Commit 20c7518

Browse files
authored
fix(mypy): type annotations for conversions algorithms (TheAlgorithms#4314)
* fix(mypy): type annotations for conversions algorithms * refactor(CI): include conversions algorithms for mypy tests
1 parent 536fb4b commit 20c7518

7 files changed

+19
-16
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
python -m pip install mypy pytest-cov -r requirements.txt
2424
# FIXME: #4052 fix mypy errors in the exclude directories and remove them below
2525
- run: mypy --ignore-missing-imports
26-
--exclude '(conversions|data_structures|digital_image_processing|dynamic_programming|graphs|linear_algebra|maths|matrix|other|project_euler|scripts|searches|strings*)/$' .
26+
--exclude '(data_structures|digital_image_processing|dynamic_programming|graphs|linear_algebra|maths|matrix|other|project_euler|scripts|searches|strings*)/$' .
2727
- name: Run tests
2828
run: pytest --doctest-modules --ignore=project_euler/ --ignore=scripts/ --cov-report=term-missing:skip-covered --cov=. .
2929
- if: ${{ success() }}

conversions/binary_to_octal.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def bin_to_octal(bin_string: str) -> str:
2828
bin_string = "0" + bin_string
2929
bin_string_in_3_list = [
3030
bin_string[index : index + 3]
31-
for index, value in enumerate(bin_string)
31+
for index in range(len(bin_string))
3232
if index % 3 == 0
3333
]
3434
for bin_group in bin_string_in_3_list:

conversions/decimal_to_binary.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ def decimal_to_binary(num: int) -> str:
2828
TypeError: 'str' object cannot be interpreted as an integer
2929
"""
3030

31-
if type(num) == float:
31+
if isinstance(num, float):
3232
raise TypeError("'float' object cannot be interpreted as an integer")
33-
if type(num) == str:
33+
if isinstance(num, str):
3434
raise TypeError("'str' object cannot be interpreted as an integer")
3535

3636
if num == 0:
@@ -42,7 +42,7 @@ def decimal_to_binary(num: int) -> str:
4242
negative = True
4343
num = -num
4444

45-
binary = []
45+
binary: list[int] = []
4646
while num > 0:
4747
binary.insert(0, num % 2)
4848
num >>= 1

conversions/decimal_to_hexadecimal.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
}
2222

2323

24-
def decimal_to_hexadecimal(decimal):
24+
def decimal_to_hexadecimal(decimal: float) -> str:
2525
"""
2626
take integer decimal value, return hexadecimal representation as str beginning
2727
with 0x
@@ -58,6 +58,7 @@ def decimal_to_hexadecimal(decimal):
5858
True
5959
"""
6060
assert type(decimal) in (int, float) and decimal == int(decimal)
61+
decimal = int(decimal)
6162
hexadecimal = ""
6263
negative = False
6364
if decimal < 0:

conversions/decimal_to_octal.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ def decimal_to_octal(num: int) -> str:
1717
counter = 0
1818
while num > 0:
1919
remainder = num % 8
20-
octal = octal + (remainder * math.pow(10, counter))
20+
octal = octal + (remainder * math.floor(math.pow(10, counter)))
2121
counter += 1
2222
num = math.floor(num / 8) # basically /= 8 without remainder if any
2323
# This formatting removes trailing '.0' from `octal`.
2424
return f"0o{int(octal)}"
2525

2626

27-
def main():
27+
def main() -> None:
2828
"""Print octal equivalents of decimal numbers."""
2929
print("\n2 in octal is:")
3030
print(decimal_to_octal(2)) # = 2

conversions/prefix_conversions.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ def convert_si_prefix(
5959
1000
6060
"""
6161
if isinstance(known_prefix, str):
62-
known_prefix: SI_Unit = SI_Unit[known_prefix.lower()]
62+
known_prefix = SI_Unit[known_prefix.lower()]
6363
if isinstance(unknown_prefix, str):
64-
unknown_prefix: SI_Unit = SI_Unit[unknown_prefix.lower()]
65-
unknown_amount = known_amount * (10 ** (known_prefix.value - unknown_prefix.value))
64+
unknown_prefix = SI_Unit[unknown_prefix.lower()]
65+
unknown_amount: float = known_amount * (
66+
10 ** (known_prefix.value - unknown_prefix.value)
67+
)
6668
return unknown_amount
6769

6870

@@ -85,10 +87,10 @@ def convert_binary_prefix(
8587
1024
8688
"""
8789
if isinstance(known_prefix, str):
88-
known_prefix: Binary_Unit = Binary_Unit[known_prefix.lower()]
90+
known_prefix = Binary_Unit[known_prefix.lower()]
8991
if isinstance(unknown_prefix, str):
90-
unknown_prefix: Binary_Unit = Binary_Unit[unknown_prefix.lower()]
91-
unknown_amount = known_amount * (
92+
unknown_prefix = Binary_Unit[unknown_prefix.lower()]
93+
unknown_amount: float = known_amount * (
9294
2 ** ((known_prefix.value - unknown_prefix.value) * 10)
9395
)
9496
return unknown_amount

conversions/weight_conversion.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
-> Wikipedia reference: https://en.wikipedia.org/wiki/Dalton_(unit)
3030
"""
3131

32-
KILOGRAM_CHART = {
32+
KILOGRAM_CHART: dict[str, float] = {
3333
"kilogram": 1,
3434
"gram": pow(10, 3),
3535
"milligram": pow(10, 6),
@@ -42,7 +42,7 @@
4242
"atomic-mass-unit": 6.022136652e26,
4343
}
4444

45-
WEIGHT_TYPE_CHART = {
45+
WEIGHT_TYPE_CHART: dict[str, float] = {
4646
"kilogram": 1,
4747
"gram": pow(10, -3),
4848
"milligram": pow(10, -6),

0 commit comments

Comments
 (0)