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

* fix(mypy): type annotations for conversions algorithms

* refactor(CI): include conversions algorithms for mypy tests
This commit is contained in:
Dhruv Manilawala 2021-04-04 18:55:49 +05:30 committed by GitHub
parent 536fb4bca4
commit 20c7518028
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 19 additions and 16 deletions

View File

@ -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() }}

View File

@ -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:

View File

@ -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:
@ -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

View File

@ -21,7 +21,7 @@ values = {
}
def decimal_to_hexadecimal(decimal):
def decimal_to_hexadecimal(decimal: float) -> str:
"""
take integer decimal value, return hexadecimal representation as str beginning
with 0x
@ -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:

View File

@ -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

View File

@ -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
@ -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

View File

@ -29,7 +29,7 @@ REFERENCES :
-> 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),
@ -42,7 +42,7 @@ KILOGRAM_CHART = {
"atomic-mass-unit": 6.022136652e26,
}
WEIGHT_TYPE_CHART = {
WEIGHT_TYPE_CHART: dict[str, float] = {
"kilogram": 1,
"gram": pow(10, -3),
"milligram": pow(10, -6),