Skip to content

Commit 7ca346e

Browse files
authored
Add mypy to CI pipeline and improve code quality checks -> v1.1.2 (#6)
- Add `mypy` type checking to CI pipeline (`ci.yml`) - Rename `lint` job to `code-quality` - Rename `test` job to `code-tests` - Add and configure pytest options in `pyproject.toml` - Simplify pytest call in `ci.yml` - Specify version numbers for dependencies - Improve type annotations in `casregnum.py` to comply with mypy - Add `casregnum.pyi` type stub for static type checking - Add `py.typed` marker file to indicate typed package - Extend CI matrix to multiple OS and Python versions - Bump version to 1.1.2
1 parent 86fee26 commit 7ca346e

File tree

6 files changed

+384
-273
lines changed

6 files changed

+384
-273
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
branches: [ main ]
88

99
jobs:
10-
test:
10+
code-tests:
1111
runs-on: ${{ matrix.os }}
1212
strategy:
1313
matrix:
@@ -28,15 +28,15 @@ jobs:
2828
- name: Install dependencies
2929
run: uv sync --dev
3030

31-
- name: Run tests with coverage
32-
run: uv run pytest -v --cov=src --cov-branch --cov-report=term --cov-report=xml
31+
- name: Run tests with coverage (as defined in pyproject.toml)
32+
run: uv run pytest
3333

3434
- name: Upload coverage reports to Codecov
3535
uses: codecov/codecov-action@v5
3636
with:
3737
token: ${{ secrets.CODECOV_TOKEN }}
3838

39-
lint:
39+
code-quality:
4040
runs-on: ubuntu-latest
4141

4242
steps:
@@ -52,3 +52,6 @@ jobs:
5252

5353
- name: Run ruff linting
5454
run: uv run ruff check src test
55+
56+
- name: Run mypy type checking
57+
run: uv run mypy src test

pyproject.toml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "casregnum"
3-
version = "1.1.1"
3+
version = "1.1.2"
44
description = "Python class to manage, check and sort CAS Registry Numbers® (CAS RN®)"
55
readme = "README.md"
66
authors = [
@@ -33,11 +33,12 @@ build-backend = "hatchling.build"
3333

3434
[tool.uv]
3535
dev-dependencies = [
36-
"pytest",
37-
"pytest-sugar",
38-
"pytest-cov",
39-
"ruff",
40-
"twine",
36+
"mypy>=1.18.1",
37+
"pytest>=8.4.1",
38+
"pytest-cov>=7.0.0",
39+
"pytest-sugar>=1.1.1",
40+
"ruff>=0.13.0",
41+
"twine>=6.2.0",
4142
]
4243

4344
[tool.pytest.ini_options]
@@ -49,6 +50,10 @@ addopts = [
4950
"--strict-markers",
5051
"--strict-config",
5152
"--verbose",
53+
"--cov=src",
54+
"--cov-branch",
55+
"--cov-report=term",
56+
"--cov-report=xml",
5257
]
5358

5459
[tool.ruff]

src/casregnum/casregnum.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,23 @@ def __repr__(self) -> str:
5151
return f"CAS(cas_rn='{self.cas_string}')"
5252

5353
# defines a string format for CAS Registry Numbers
54-
def __format__(self, format_spec) -> str:
54+
def __format__(self, format_spec: str) -> str:
5555
return f"{self.cas_string:{format_spec}}"
5656

5757
# checks if two CAS Registry Numbers are equal
58-
def __eq__(self, other: CAS) -> bool:
58+
def __eq__(self, other: object) -> bool:
59+
"""
60+
Checks if two CAS Registry Numbers® are equal.
61+
"""
5962
if not isinstance(other, CAS):
6063
raise TypeError("Comparisons can only be made between CAS objects.")
6164
return self.cas_integer == other.cas_integer
6265

6366
# checks if self.cas_integer < other.cas_integer
64-
def __lt__(self, other: CAS) -> bool:
67+
def __lt__(self, other: object) -> bool:
68+
"""
69+
Checks if this CAS Registry Number® is less than another CAS Registry Number®.
70+
"""
6571
if not isinstance(other, CAS):
6672
raise TypeError("Comparisons can only be made between CAS objects.")
6773
return self.cas_integer < other.cas_integer
@@ -70,7 +76,7 @@ def __lt__(self, other: CAS) -> bool:
7076
@property
7177
def cas_string(self) -> str:
7278
"""
73-
Returns the CAS Registry Number as a formatted string (e.g. "58-08-2").
79+
Returns the CAS Registry Number® as a formatted string (e.g. "58-08-2").
7480
"""
7581
return self._cas_string
7682

@@ -95,7 +101,7 @@ def cas_string(self, cas_rn: str) -> None:
95101
@property
96102
def cas_integer(self) -> int:
97103
"""
98-
Returns the CAS Registry Number as an integer (e.g. 58082).
104+
Returns the CAS Registry Number® as an integer (e.g. 58082).
99105
"""
100106
return self._cas_integer
101107

@@ -114,7 +120,7 @@ def cas_integer(self, cas_rn: int) -> None:
114120
@property
115121
def check_digit(self) -> int:
116122
"""
117-
Returns the check digit of the CAS Registry Number (e.g. 2 for "58-08-2").
123+
Returns the check digit of the CAS Registry Number® as an integer (e.g. 2 for "58-08-2").
118124
"""
119125
return self._check_digit
120126

src/casregnum/casregnum.pyi

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class CAS:
2+
def __init__(self, cas_rn: int | str) -> None: ...
3+
def __format__(self, format_spec: str) -> str: ...
4+
def __eq__(self, other: object) -> bool: ...
5+
def __lt__(self, other: object) -> bool: ...
6+
@property
7+
def cas_string(self) -> str: ...
8+
@cas_string.setter
9+
def cas_string(self, cas_rn: str) -> None: ...
10+
@property
11+
def cas_integer(self) -> int: ...
12+
@cas_integer.setter
13+
def cas_integer(self, cas_rn: int) -> None: ...
14+
@property
15+
def check_digit(self) -> int: ...
16+
@check_digit.setter
17+
def check_digit(self, digit_to_test: int) -> None: ...

src/casregnum/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)