Skip to content

fix(mypy): type annotations for conversions algorithms #4314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
python -m pip install mypy pytest-cov -r requirements.txt
# FIXME: #4052 fix mypy errors in the exclude directories and remove them below
- run: mypy --ignore-missing-imports
--exclude '(conversions|data_structures|digital_image_processing|dynamic_programming|graphs|linear_algebra|maths|matrix|other|project_euler|scripts|searches|strings*)/$' .
--exclude '(data_structures|digital_image_processing|dynamic_programming|graphs|linear_algebra|maths|matrix|other|project_euler|scripts|searches|strings*)/$' .
- name: Run tests
run: pytest --doctest-modules --ignore=project_euler/ --ignore=scripts/ --cov-report=term-missing:skip-covered --cov=. .
- if: ${{ success() }}
Expand Down
2 changes: 1 addition & 1 deletion conversions/binary_to_octal.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def bin_to_octal(bin_string: str) -> str:
bin_string = "0" + bin_string
bin_string_in_3_list = [
bin_string[index : index + 3]
for index, value in enumerate(bin_string)
for index in range(len(bin_string))
if index % 3 == 0
]
for bin_group in bin_string_in_3_list:
Expand Down
6 changes: 3 additions & 3 deletions conversions/decimal_to_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def decimal_to_binary(num: int) -> str:
TypeError: 'str' object cannot be interpreted as an integer
"""

if type(num) == float:
if isinstance(num, float):
raise TypeError("'float' object cannot be interpreted as an integer")
if type(num) == str:
if isinstance(num, str):
raise TypeError("'str' object cannot be interpreted as an integer")

if num == 0:
Expand All @@ -42,7 +42,7 @@ def decimal_to_binary(num: int) -> str:
negative = True
num = -num

binary = []
binary: list[int] = []
while num > 0:
binary.insert(0, num % 2)
num >>= 1
Expand Down
3 changes: 2 additions & 1 deletion conversions/decimal_to_hexadecimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}


def decimal_to_hexadecimal(decimal):
def decimal_to_hexadecimal(decimal: float) -> str:
"""
take integer decimal value, return hexadecimal representation as str beginning
with 0x
Expand Down Expand Up @@ -58,6 +58,7 @@ def decimal_to_hexadecimal(decimal):
True
"""
assert type(decimal) in (int, float) and decimal == int(decimal)
decimal = int(decimal)
hexadecimal = ""
negative = False
if decimal < 0:
Expand Down
4 changes: 2 additions & 2 deletions conversions/decimal_to_octal.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ def decimal_to_octal(num: int) -> str:
counter = 0
while num > 0:
remainder = num % 8
octal = octal + (remainder * math.pow(10, counter))
octal = octal + (remainder * math.floor(math.pow(10, counter)))
counter += 1
num = math.floor(num / 8) # basically /= 8 without remainder if any
# This formatting removes trailing '.0' from `octal`.
return f"0o{int(octal)}"


def main():
def main() -> None:
"""Print octal equivalents of decimal numbers."""
print("\n2 in octal is:")
print(decimal_to_octal(2)) # = 2
Expand Down
14 changes: 8 additions & 6 deletions conversions/prefix_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ def convert_si_prefix(
1000
"""
if isinstance(known_prefix, str):
known_prefix: SI_Unit = SI_Unit[known_prefix.lower()]
known_prefix = SI_Unit[known_prefix.lower()]
if isinstance(unknown_prefix, str):
unknown_prefix: SI_Unit = SI_Unit[unknown_prefix.lower()]
unknown_amount = known_amount * (10 ** (known_prefix.value - unknown_prefix.value))
unknown_prefix = SI_Unit[unknown_prefix.lower()]
unknown_amount: float = known_amount * (
10 ** (known_prefix.value - unknown_prefix.value)
)
return unknown_amount


Expand All @@ -85,10 +87,10 @@ def convert_binary_prefix(
1024
"""
if isinstance(known_prefix, str):
known_prefix: Binary_Unit = Binary_Unit[known_prefix.lower()]
known_prefix = Binary_Unit[known_prefix.lower()]
if isinstance(unknown_prefix, str):
unknown_prefix: Binary_Unit = Binary_Unit[unknown_prefix.lower()]
unknown_amount = known_amount * (
unknown_prefix = Binary_Unit[unknown_prefix.lower()]
unknown_amount: float = known_amount * (
2 ** ((known_prefix.value - unknown_prefix.value) * 10)
)
return unknown_amount
Expand Down
4 changes: 2 additions & 2 deletions conversions/weight_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
-> Wikipedia reference: https://en.wikipedia.org/wiki/Dalton_(unit)
"""

KILOGRAM_CHART = {
KILOGRAM_CHART: dict[str, float] = {
"kilogram": 1,
"gram": pow(10, 3),
"milligram": pow(10, 6),
Expand All @@ -42,7 +42,7 @@
"atomic-mass-unit": 6.022136652e26,
}

WEIGHT_TYPE_CHART = {
WEIGHT_TYPE_CHART: dict[str, float] = {
"kilogram": 1,
"gram": pow(10, -3),
"milligram": pow(10, -6),
Expand Down