Skip to content

Commit a6abfc8

Browse files
committed
Update Flake8 Config, Update Code Style, Tweak Dependencies
- Update flake8 config with `.flake8` file and changes to `python-package.yml` - Update code style to comply with flake8 config - Tweak dependencies in `requirements.txt` and `pyproject.toml`
1 parent f7b2d49 commit a6abfc8

File tree

7 files changed

+76
-62
lines changed

7 files changed

+76
-62
lines changed

.flake8

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[flake8]
2+
ignore = E261
3+
max-line-length = 200
4+
exclude = .git,.github,__pycache__,.pytest_cache,.venv,.venv_test,.vscode
5+
max-complexity = 10

.github/workflows/python-package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
- name: Lint with flake8
3333
run: |
3434
# stop the build if there are Python syntax errors or undefined names
35-
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
35+
flake8 . --count --select=E9,F63,F7,F82 --ignore=E261 --show-source --statistics
3636
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
3737
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
3838
- name: Test with pytest

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
python-dotenv==1.1.1
21
pytest==8.4.1
32
pytest-cov==6.2.1
4-
pdoc==15.0.4
3+
pdoc==15.0.4
4+
flake8==7.3.0

stat_log_db/pyproject.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ description = ""
99
readme = "README.md"
1010
requires-python = "==3.12.10"
1111
dependencies = [
12-
"python-dotenv==1.1.1"
1312
]
1413

1514
[project.optional-dependencies]
1615
dev = [
1716
"pytest==8.4.1",
18-
"pytest-cov==6.2.1",
19-
"pdoc==15.0.4"
17+
"pytest-cov==6.2.1"
2018
]
2119

2220
[project.scripts]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from . import exceptions
2-
from . import cli
2+
from . import cli

stat_log_db/src/stat_log_db/cli.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import os
22
import sys
33
import argparse
4-
from dotenv import load_dotenv
54

65
from .exceptions import raise_type_error_with_signature
76

8-
load_dotenv()
97

10-
11-
def create_parser(parser_args: dict, version: str | int="0.0.1") -> argparse.ArgumentParser:
8+
def create_parser(parser_args: dict, version: str | int = "0.0.1") -> argparse.ArgumentParser:
129
"""Create the main argument parser."""
1310
# Validate parser_args
1411
if not isinstance(parser_args, dict):
@@ -41,7 +38,7 @@ def main():
4138
parser = create_parser({
4239
"prog": "sldb",
4340
"description": "My CLI tool",
44-
}, "0.0.1")
41+
}, "0.0.1") # TODO: Read version from pyproject.toml?
4542

4643
args = parser.parse_args()
4744

tests/test_tools.py

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,32 @@
66

77
import pytest
88

9-
#region Global Variables
9+
10+
# region Global Variables
1011

1112
ROOT = Path(__file__).resolve().parent.parent
12-
SCRIPT = ROOT / 'tools.sh'
13-
VENV_TEST = ROOT / '.venv_test'
14-
PACKAGE_NAME = 'stat-log-db'
13+
SCRIPT = ROOT / "tools.sh"
14+
VENV_TEST = ROOT / ".venv_test"
15+
PACKAGE_NAME = "stat-log-db"
1516

1617
GITHUB_ACTIONS = os.getenv("GITHUB_ACTIONS") == "true"
1718

18-
#endregion
19+
# endregion
20+
1921

22+
# region testing tools
2023

21-
#region testing tools
2224

2325
def _ensure_test_venv():
2426
"""Ensure the test virtual environment is created."""
2527
if not VENV_TEST.exists():
26-
subprocess.run([sys.executable, '-m', 'venv', str(VENV_TEST)], check=True)
28+
subprocess.run([sys.executable, "-m", "venv", str(VENV_TEST)], check=True)
29+
2730

2831
def _venv_python():
2932
"""Return path to the virtual environment's python interpreter."""
30-
return VENV_TEST / ('Scripts' if os.name == 'nt' else 'bin') / ('python.exe' if os.name == 'nt' else 'python')
33+
return VENV_TEST / ("Scripts" if os.name == "nt" else "bin") / ("python.exe" if os.name == "nt" else "python")
34+
3135

3236
def is_installed(package: str) -> bool:
3337
"""
@@ -36,26 +40,28 @@ def is_installed(package: str) -> bool:
3640
"""
3741
_ensure_test_venv()
3842
python_executable = _venv_python()
39-
result = subprocess.run([str(python_executable), '-m', 'pip', 'show', package], capture_output=True, text=True)
43+
result = subprocess.run([str(python_executable), "-m", "pip", "show", package], capture_output=True, text=True)
4044
return result.returncode == 0
4145

46+
4247
def run_tools(args, use_test_venv=False):
4348
"""Run tools.sh returning (code, stdout+stderr)."""
4449
env = os.environ.copy()
4550
if use_test_venv:
4651
_ensure_test_venv()
47-
scripts_dir = VENV_TEST / ('Scripts' if os.name == 'nt' else 'bin')
48-
env['PATH'] = str(scripts_dir) + os.pathsep + env.get('PATH', '')
49-
env['VIRTUAL_ENV'] = str(VENV_TEST)
50-
env['PYTHONHOME'] = '' # ensure venv python resolution
51-
bash = r'C:\Program Files\Git\bin\bash.exe' if os.name == 'nt' else 'bash' # TODO: indicate to the user that they need git bash
52+
scripts_dir = VENV_TEST / ("Scripts" if os.name == "nt" else "bin")
53+
env["PATH"] = str(scripts_dir) + os.pathsep + env.get("PATH", "")
54+
env["VIRTUAL_ENV"] = str(VENV_TEST)
55+
env["PYTHONHOME"] = "" # ensure venv python resolution
56+
bash = r"C:\Program Files\Git\bin\bash.exe" if os.name == "nt" else "bash" # TODO: indicate to the user that they need git bash
5257
proc = subprocess.run([bash, str(SCRIPT), *args], capture_output=True, text=True, cwd=ROOT, env=env)
5358
return proc.returncode, proc.stdout + proc.stderr
5459

55-
#endregion
60+
61+
# endregion
5662

5763

58-
@pytest.fixture() # scope="module"
64+
@pytest.fixture() # scope="module"
5965
def test_venv():
6066
"""
6167
Provision an isolated virtual environment used for install/uninstall tests.
@@ -69,13 +75,13 @@ def test_venv():
6975

7076

7177
def test_help():
72-
code, out = run_tools(['-h'])
78+
code, out = run_tools(["-h"])
7379
assert code == 0
7480
# Read README.md
75-
readme_path = ROOT / 'README.md'
81+
readme_path = ROOT / "README.md"
7682
assert readme_path.exists(), f"README not found at {readme_path}"
7783
readme_content = None
78-
with open(readme_path, 'r', encoding='utf-8') as f:
84+
with open(readme_path, "r", encoding="utf-8") as f:
7985
readme_content = f.read().strip()
8086
assert not (readme_content is None), "Unable to read README"
8187
# Compare README content with help output
@@ -84,66 +90,74 @@ def test_help():
8490
except AssertionError:
8591
assert out.strip() == readme_content.strip(), "Help output does not match README content (leading & trailing whitespace stripped)"
8692

93+
8794
@pytest.mark.skipif(GITHUB_ACTIONS, reason="Skipping test on GitHub Actions")
8895
def test_install_dev(test_venv):
89-
code, out = run_tools(['-id'], use_test_venv=True)
96+
code, out = run_tools(["-id"], use_test_venv=True)
9097
assert code == 0
91-
assert 'Installing' in out
92-
assert 'dev' in out
93-
assert is_installed(PACKAGE_NAME), 'Package should be installed after dev install'
98+
assert "Installing" in out
99+
assert "dev" in out
100+
assert is_installed(PACKAGE_NAME), "Package should be installed after dev install"
101+
94102

95103
@pytest.mark.skipif(GITHUB_ACTIONS, reason="Skipping test on GitHub Actions")
96104
def test_install_normal(test_venv):
97-
code, out = run_tools(['-in'], use_test_venv=True)
105+
code, out = run_tools(["-in"], use_test_venv=True)
98106
assert code == 0
99-
assert 'Installing' in out
100-
assert 'dev' not in out
101-
assert is_installed(PACKAGE_NAME), 'Package should be installed after normal install'
107+
assert "Installing" in out
108+
assert "dev" not in out
109+
assert is_installed(PACKAGE_NAME), "Package should be installed after normal install"
110+
102111

103112
def test_install_invalid_arg(test_venv):
104-
code, out = run_tools(['-ix'], use_test_venv=True)
113+
code, out = run_tools(["-ix"], use_test_venv=True)
105114
assert code == 1
106-
assert ('Unsupported argument' in out) or ('Invalid install mode' in out)
107-
assert not is_installed(PACKAGE_NAME), 'Package should not be installed after invalid install argument'
115+
assert ("Unsupported argument" in out) or ("Invalid install mode" in out)
116+
assert not is_installed(PACKAGE_NAME), "Package should not be installed after invalid install argument"
117+
108118

109119
@pytest.mark.skipif(GITHUB_ACTIONS, reason="Skipping test on GitHub Actions")
110120
def test_uninstall(test_venv):
111121
# Ensure something installed first (dev mode)
112-
icode, iout = run_tools(['-id'], use_test_venv=True)
122+
icode, iout = run_tools(["-id"], use_test_venv=True)
113123
assert icode == 0
114-
assert is_installed(PACKAGE_NAME), 'Package should be installed (before uninstall)'
115-
ucode, uout = run_tools(['-u'], use_test_venv=True)
124+
assert is_installed(PACKAGE_NAME), "Package should be installed (before uninstall)"
125+
ucode, uout = run_tools(["-u"], use_test_venv=True)
116126
assert ucode == 0
117-
assert 'Uninstalling' in uout
118-
assert 'Uninstall complete' in uout
119-
assert not is_installed(PACKAGE_NAME), 'Package should not be installed after uninstall'
127+
assert "Uninstalling" in uout
128+
assert "Uninstall complete" in uout
129+
assert not is_installed(PACKAGE_NAME), "Package should not be installed after uninstall"
130+
120131

121132
@pytest.mark.skipif(GITHUB_ACTIONS, reason="Skipping test on GitHub Actions")
122133
def test_install_and_clean_multi_flag(test_venv):
123-
code, out = run_tools(['-id', '-c'], use_test_venv=True)
134+
code, out = run_tools(["-id", "-c"], use_test_venv=True)
124135
assert code == 0
125-
assert is_installed(PACKAGE_NAME), 'Package should be installed'
126-
assert 'Installing' in out
127-
assert 'Cleaning up workspace' in out
128-
assert 'Cleanup complete' in out
129-
assert is_installed(PACKAGE_NAME), 'Cleanup should not remove installed package'
136+
assert is_installed(PACKAGE_NAME), "Package should be installed"
137+
assert "Installing" in out
138+
assert "Cleaning up workspace" in out
139+
assert "Cleanup complete" in out
140+
assert is_installed(PACKAGE_NAME), "Cleanup should not remove installed package"
141+
130142

131143
def test_test_no_arg():
132-
code, out = run_tools(['-t'])
144+
code, out = run_tools(["-t"])
133145
assert code == 1
134146
try:
135-
assert out == 'Option -t requires an argument'
147+
assert out == "Option -t requires an argument"
136148
except AssertionError:
137-
assert out.strip() == 'Option -t requires an argument'
149+
assert out.strip() == "Option -t requires an argument"
150+
138151

139152
def test_test_invalid_arg():
140-
code, out = run_tools(['-tx'])
153+
code, out = run_tools(["-tx"])
141154
assert code == 1
142-
assert ('Unsupported argument' in out) or ('Invalid test mode' in out)
155+
assert ("Unsupported argument" in out) or ("Invalid test mode" in out)
156+
143157

144158
@pytest.mark.skipif(GITHUB_ACTIONS, reason="Skipping test on GitHub Actions")
145159
def test_clean():
146-
code, out = run_tools(['-c'])
160+
code, out = run_tools(["-c"])
147161
assert code == 0
148-
assert 'Cleaning up workspace' in out
149-
assert 'Cleanup complete' in out
162+
assert "Cleaning up workspace" in out
163+
assert "Cleanup complete" in out

0 commit comments

Comments
 (0)