Skip to content

Commit

Permalink
Increase Flake8 and reconcile Pylint and Pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
Xierumeng committed Feb 20, 2024
1 parent 204ddd2 commit 909d119
Show file tree
Hide file tree
Showing 16 changed files with 288 additions and 227 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

# Python
__pycache__/
.pytest_cache/
venv/

# Logging
logs/

# Pytorch and Ultralytics
*.pt
*.pth

# Logging
*log*
Empty file removed __init__.py
Empty file.
10 changes: 5 additions & 5 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# Global constants for main with cuda
# Global constants for main with CUDA

queue_max_size: 10

log_directory_path: "logs"

video_input:
camera_name: 0
worker_period: 1.0 # seconds
worker_period: 1.0 # seconds
save_prefix: "log_image"

detect_target:
worker_count: 1
device: 0
model_path: "tests/model_example/yolov8s_ultralytics_pretrained_default.pt" # TODO: update
model_path: "tests/model_example/yolov8s_ultralytics_pretrained_default.pt" # TODO: update
save_prefix: "log_comp"

flight_interface:
address: "tcp:127.0.0.1:14550"
timeout: 10.0 # seconds
worker_period: 0.1 # seconds
timeout: 10.0 # seconds
worker_period: 0.1 # seconds
8 changes: 4 additions & 4 deletions documentation/tests/unit/add_or_multiply.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ class AddOrMultiply:
Add or multiply depending on state.
"""

def __init__(self, switch: MathOperation):
def __init__(self, switch: MathOperation) -> None:
self.__operator = switch

def add_or_multiply(self, num1: float, num2: float) -> float:
def add_or_multiply(self, num_1: float, num_2: float) -> float:
"""
Adds or multiplies numbers based on internal state.
"""
if self.__operator == MathOperation.ADD:
return num1 + num2
return num_1 + num_2

if self.__operator == MathOperation.MULTIPLY:
return num1 * num2
return num_1 * num_2

raise NotImplementedError

Expand Down
60 changes: 30 additions & 30 deletions documentation/tests/unit/test_add_or_multiply.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@
# Pytest fixtures are reusable setup components
# Makes it easier when setup is complicated
@pytest.fixture()
def adder():
def adder() -> add_or_multiply.AddOrMultiply: # type: ignore
"""
Creates AddOrMultiply in addition state.
"""
add = add_or_multiply.AddOrMultiply(add_or_multiply.MathOperation.ADD)
yield add
yield add # type: ignore


@pytest.fixture()
def multiplier():
def multiplier() -> add_or_multiply.AddOrMultiply: # type: ignore
"""
Creates AddOrMultiply in multiplication state.
"""
multiply = add_or_multiply.AddOrMultiply(add_or_multiply.MathOperation.MULTIPLY)
yield multiply
yield multiply # type: ignore


class TestAddition:
Expand All @@ -44,84 +44,84 @@ class TestAddition:
The function or method name contains test somewhere for Pytest to run it.
"""

def test_add_positive(self, adder: add_or_multiply.AddOrMultiply):
def test_add_positive(self, adder: add_or_multiply.AddOrMultiply) -> None:
"""
Add 2 positive numbers.
The parameter names must match the fixture function name to be used.
"""
# Setup
# Hardcode values on the right side if possible
input1 = 1.2
input2 = 3.4
input_1 = 1.2
input_2 = 3.4
# expected is the variable name to compare against
expected = 4.6

# Run
# actual is the variable name to compare
actual = adder.add_or_multiply(input1, input2)
actual = adder.add_or_multiply(input_1, input_2)

# Test
# Pytest unit tests pass if there are no unexpected exceptions
# Actual before expected
# Use math.isclose() for floating point comparison
assert math.isclose(actual, expected)

def test_add_large_positive(self, adder: add_or_multiply.AddOrMultiply):
def test_add_large_positive(self, adder: add_or_multiply.AddOrMultiply) -> None:
"""
Add 2 positive numbers.
"""
# Setup
input1 = 400_000_000.0
input2 = 0.1
input_1 = 400_000_000.0
input_2 = 0.1
expected = 400_000_000.1

# Run
actual = adder.add_or_multiply(input1, input2)
actual = adder.add_or_multiply(input_1, input_2)

# Test
assert math.isclose(actual, expected)

def test_add_positive_negative(self, adder: add_or_multiply.AddOrMultiply):
def test_add_positive_negative(self, adder: add_or_multiply.AddOrMultiply) -> None:
"""
Add positive and negative number.
"""
# Setup
input1 = 0.1
input2 = -1.0
input_1 = 0.1
input_2 = -1.0
expected = -0.9

# Run
actual = adder.add_or_multiply(input1, input2)
actual = adder.add_or_multiply(input_1, input_2)

# Test
assert math.isclose(actual, expected)

def test_add_positive_and_same_negative(self, adder: add_or_multiply.AddOrMultiply):
def test_add_positive_and_same_negative(self, adder: add_or_multiply.AddOrMultiply) -> None:
"""
Add positive and negative number.
"""
# Setup
input1 = 1.5
input2 = -1.5
input_1 = 1.5
input_2 = -1.5
expected = 0.0

# Run
actual = adder.add_or_multiply(input1, input2)
actual = adder.add_or_multiply(input_1, input_2)

# Test
assert math.isclose(actual, expected)

def test_add_negative(self, adder: add_or_multiply.AddOrMultiply):
def test_add_negative(self, adder: add_or_multiply.AddOrMultiply) -> None:
"""
Add positive and negative number.
"""
# Setup
input1 = -0.5
input2 = -1.0
input_1 = -0.5
input_2 = -1.0
expected = -1.5

# Run
actual = adder.add_or_multiply(input1, input2)
actual = adder.add_or_multiply(input_1, input_2)

# Test
assert math.isclose(actual, expected)
Expand All @@ -138,18 +138,18 @@ class TestMultiply:
Many multiplication cases need to be covered as well.
"""

def test_multiply_positive(self, multiplier: add_or_multiply.AddOrMultiply):
def test_multiply_positive(self, multiplier: add_or_multiply.AddOrMultiply) -> None:
"""
Multiply 2 positive numbers.
Different fixture so different parameter name.
"""
# Setup
input1 = 1.2
input2 = 3.4
input_1 = 1.2
input_2 = 3.4
expected = 4.08

# Run
actual = multiplier.add_or_multiply(input1, input2)
actual = multiplier.add_or_multiply(input_1, input_2)

# Test
assert math.isclose(actual, expected)
Expand All @@ -160,7 +160,7 @@ class TestSwap:
Test a different method.
"""

def test_swap_add_to_multiply(self, adder: add_or_multiply.AddOrMultiply):
def test_swap_add_to_multiply(self, adder: add_or_multiply.AddOrMultiply) -> None:
"""
Add and then multiply.
"""
Expand All @@ -177,7 +177,7 @@ def test_swap_add_to_multiply(self, adder: add_or_multiply.AddOrMultiply):

assert actual == expected

def test_swap_multiply_to_add(self, multiplier: add_or_multiply.AddOrMultiply):
def test_swap_multiply_to_add(self, multiplier: add_or_multiply.AddOrMultiply) -> None:
"""
Multiply and then add.
"""
Expand Down
6 changes: 3 additions & 3 deletions documentation/tests/unit/test_pytest_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest


def test_trivial_pass():
def test_trivial_pass() -> None:
"""
Unit test will pass by default.
"""
Expand All @@ -18,7 +18,7 @@ def test_trivial_pass():
pass


def test_1_plus_1_equals_2():
def test_1_plus_1_equals_2() -> None:
"""
Easy unit test.
"""
Expand All @@ -28,7 +28,7 @@ def test_1_plus_1_equals_2():
assert actual == expected


def test_expect_exception():
def test_expect_exception() -> None:
"""
If an exception is expected.
"""
Expand Down
2 changes: 1 addition & 1 deletion modules/detections_and_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def create(
if bounds.shape != (4,) or not np.greater_equal(bounds, 0.0).all():
return False, None

# n1 <= n2
# n_1 <= n_2
if bounds[0] > bounds[2] or bounds[1] > bounds[3]:
return False, None

Expand Down
27 changes: 27 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
[tool.pylint.main]
# Add files or directories matching the regular expressions patterns to the
# ignore-list. The regex matches against paths and can be in Posix or Windows
# format. Because '\\' represents the directory delimiter on Windows systems, it
# can't be used as an escape character.
ignore-paths = [
# From .gitignore
# IDEs
".idea/",
".vscode/",

# Python
"__pycache__/",
"venv/",

# Logging
"logs/",
]

# Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the
# number of processors available to use, and will cap the count on Windows to
# avoid hangs.
jobs = 0

# Minimum Python version to use for version dependent checks. Will default to the
# version used to run pylint.
py-version = "3.8"

# Discover python modules and packages in the file system subtree.
recursive = true

# [tool.pylint.basic]
# Good variable names which should always be accepted, separated by a comma.
good-names = [
Expand Down Expand Up @@ -46,6 +72,7 @@ disable = [
"no-member", # Pylint cannot handle 3rd party imports
"too-few-public-methods", # Some classes are simple
"too-many-arguments", # Function signatures
"too-many-branches", # Don't care
"too-many-lines", # Line count in file
"too-many-locals", # Don't care
"too-many-statements", # Don't care
Expand Down
9 changes: 5 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Packages listed in alphabetical order
# Linters and formatters are explicitly versioned
black==24.2.0
flake8-annotations==3.0.1
numpy
opencv-python
pylint==3.0.3
pymap3d
pytest
pyyaml
scikit-learn
# pytorch special case, see requirements-pytorch.txt

# Linters and formatters are explicitly versioned
black==24.2.0
flake8-annotations==3.0.1
pylint==3.0.3
3 changes: 0 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ extend-exclude=
.vscode/,
# Python
__pycache__/,
.pytest_cache/,
venv/,
# Logging
logs/,
# Outside of .gitignore
# Submodules
modules/common/,
# Tests
*/unit/
Loading

0 comments on commit 909d119

Please sign in to comment.