added strict mypy checks
This commit is contained in:
@@ -1,6 +0,0 @@
|
|||||||
def main():
|
|
||||||
print("Hello from python-utils!")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
@@ -24,3 +24,20 @@ dev = [
|
|||||||
"ruff>=0.12.10",
|
"ruff>=0.12.10",
|
||||||
"safety>=3.6.0",
|
"safety>=3.6.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[tool.mypy]
|
||||||
|
python_version = "3.10"
|
||||||
|
warn_return_any = true # nudge to use stricter types
|
||||||
|
warn_unused_configs = true # nudge to remove unused configs
|
||||||
|
disallow_untyped_defs = true # disallow untyped function definitions
|
||||||
|
disallow_incomplete_defs = true # ensure all parts of a function has type annotation
|
||||||
|
check_untyped_defs = true # dont skip untyped functions
|
||||||
|
disallow_untyped_decorators = false # allow untyped decorators to keep code more readable
|
||||||
|
no_implicit_optional = true # disallow implicit optional types leading to None-mess
|
||||||
|
warn_redundant_casts = true # nudge to use explicit type casts
|
||||||
|
warn_unused_ignores = true # nudge to remove unused ignores
|
||||||
|
warn_no_return = true # catch missing return statements
|
||||||
|
warn_unreachable = true # catch unreachable code
|
||||||
|
show_error_codes = true # show error codes in output
|
||||||
|
explicit_package_bases = true # reduce risk of import confusion
|
||||||
|
namespace_packages = true # enable namespace packages
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
from .check_env import check_env as check_env
|
||||||
|
from .readable_unit import readable_unit as readable_unit
|
||||||
|
|
||||||
|
__all__ = ["check_env", "readable_unit"]
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import os
|
|||||||
|
|
||||||
|
|
||||||
def check_env(
|
def check_env(
|
||||||
var_list: str | list[str],
|
var_list: str | set[str] | list[str] | tuple[str, ...],
|
||||||
missing_ok: bool = False,
|
missing_ok: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Check if environment variables are set.
|
"""Check if environment variables are set.
|
||||||
|
|||||||
@@ -3,12 +3,15 @@
|
|||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from pytohn_utils import check_env
|
from python_utils import check_env
|
||||||
|
|
||||||
|
|
||||||
class TestCheckEnv(unittest.TestCase):
|
class TestCheckEnv(unittest.TestCase):
|
||||||
|
set_vars: list[str]
|
||||||
|
unset_var: str
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls) -> None:
|
||||||
"""Set up test environment variables."""
|
"""Set up test environment variables."""
|
||||||
cls.set_vars = ["A", "B", "C"]
|
cls.set_vars = ["A", "B", "C"]
|
||||||
for var in cls.set_vars:
|
for var in cls.set_vars:
|
||||||
@@ -20,41 +23,41 @@ class TestCheckEnv(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def tearDownClass(cls):
|
def tearDownClass(cls) -> None:
|
||||||
for var in cls.set_vars:
|
for var in cls.set_vars:
|
||||||
if var in os.environ:
|
if var in os.environ:
|
||||||
del os.environ[var]
|
del os.environ[var]
|
||||||
os.environ.pop(var, None)
|
os.environ.pop(var, None)
|
||||||
|
|
||||||
def test_check_env_accepts_single_string(self):
|
def test_check_env_accepts_single_string(self) -> None:
|
||||||
"""Test check_env function with a valid single string."""
|
"""Test check_env function with a valid single string."""
|
||||||
self.assertIsNone(check_env(self.set_vars[0]))
|
check_env(self.set_vars[0])
|
||||||
|
|
||||||
def test_check_env_accepts_list_of_strings(self):
|
def test_check_env_accepts_list_of_strings(self) -> None:
|
||||||
"""Test check_env function with a valid list of strings."""
|
"""Test check_env function with a valid list of strings."""
|
||||||
self.assertIsNone(check_env(self.set_vars))
|
check_env(self.set_vars)
|
||||||
|
|
||||||
def test_check_env_accepts_set_of_strings(self):
|
def test_check_env_accepts_set_of_strings(self) -> None:
|
||||||
"""Test check_env function with a valid set of strings."""
|
"""Test check_env function with a valid set of strings."""
|
||||||
self.assertIsNone(check_env(set(self.set_vars)))
|
check_env(set(self.set_vars))
|
||||||
|
|
||||||
def test_check_env_accepts_tuple_of_strings(self):
|
def test_check_env_accepts_tuple_of_strings(self) -> None:
|
||||||
"""Test check_env function with a valid tuple of strings."""
|
"""Test check_env function with a valid tuple of strings."""
|
||||||
self.assertIsNone(check_env(tuple(self.set_vars)))
|
check_env(tuple(self.set_vars))
|
||||||
|
|
||||||
def test_check_env_rejects_empty_list(self):
|
def test_check_env_rejects_empty_list(self) -> None:
|
||||||
"""Test check_env function with an empty list."""
|
"""Test check_env function with an empty list."""
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
check_env([])
|
check_env([])
|
||||||
|
|
||||||
def test_check_env_raises_error_on_missing_vars(self):
|
def test_check_env_raises_error_on_missing_vars(self) -> None:
|
||||||
"""Test check_env function with missing environment variables."""
|
"""Test check_env function with missing environment variables."""
|
||||||
with self.assertRaises(OSError):
|
with self.assertRaises(OSError):
|
||||||
check_env(self.unset_var)
|
check_env(self.unset_var)
|
||||||
|
|
||||||
def test_check_env_does_not_raise_error_when_missing_ok_flag_set(self):
|
def test_check_env_does_not_raise_error_when_missing_ok_flag_set(self) -> None:
|
||||||
"""Test that check_env does not raise an error when the environment variable is missing and missing_ok=True.
|
"""Test that check_env does not raise an error when the environment variable is missing and missing_ok=True.
|
||||||
|
|
||||||
The 'missing_ok' flag allows the function to skip raising an error if the specified environment variable(s) are not set.
|
The 'missing_ok' flag allows the function to skip raising an error if the specified environment variable(s) are not set.
|
||||||
"""
|
"""
|
||||||
self.assertIsNone(check_env(self.unset_var, missing_ok=True))
|
check_env(self.unset_var, missing_ok=True)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
"""Unittests for the readable function."""
|
"""Unittests for the readable function."""
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from python_utils import readable_unit
|
from python_utils import readable_unit
|
||||||
|
|
||||||
@@ -31,7 +32,11 @@ TEST_CASES = [
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("value, unit, expected", TEST_CASES)
|
@pytest.mark.parametrize("value, unit, expected", TEST_CASES)
|
||||||
def test_readable_unit(value, unit, expected):
|
def test_readable_unit(
|
||||||
|
value: Any, # Allow invalid types for testing
|
||||||
|
unit: Any, # Allow invalid types for testing
|
||||||
|
expected: Any, # Allow invalid types for testing
|
||||||
|
) -> None:
|
||||||
"""Test readable_unit function with valid inputs."""
|
"""Test readable_unit function with valid inputs."""
|
||||||
if isinstance(expected, type) and issubclass(expected, Exception):
|
if isinstance(expected, type) and issubclass(expected, Exception):
|
||||||
with pytest.raises(expected):
|
with pytest.raises(expected):
|
||||||
|
|||||||
Reference in New Issue
Block a user