Skip to content

Commit

Permalink
Pyproject (#19)
Browse files Browse the repository at this point in the history
* moved configuration of tools to pyproject.toml
* remove flake8 and unused configurations
* strict type checking
* python version must be at least 3.9

---------

Co-authored-by: Roland Kaminski <[email protected]>
  • Loading branch information
MaxOstrowski and rkaminsk authored Feb 20, 2024
1 parent 0532576 commit d2b6042
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 83 deletions.
8 changes: 0 additions & 8 deletions .coveragerc

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/ci-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ jobs:
- name: "checkout repository"
uses: actions/checkout@v3

- name: "setup python 3.7"
- name: "setup python 3.9"
uses: actions/setup-python@v4
with:
python-version: 3.7
python-version: 3.9

- name: "setup python 3.11"
uses: actions/setup-python@v4
Expand Down
2 changes: 0 additions & 2 deletions .isort.cfg

This file was deleted.

1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ repos:
rev: 5.11.5
hooks:
- id: isort
args: ["--profile", "black"]
exclude: ^.github/

- repo: https://github.com/psf/black
Expand Down
31 changes: 0 additions & 31 deletions .pylintrc

This file was deleted.

8 changes: 4 additions & 4 deletions init.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ def replace(filepath):

dirs = [os.path.join("src", "fillname"), "tests", "doc"]
files = [
"setup.cfg",
".pre-commit-config.yaml",
"noxfile.py",
"pyproject.toml",
"setup.cfg",
"CONTRIBUTING.md",
"README.md",
"DEVELOPMENT.md",
"LICENSE",
".pre-commit-config.yaml",
".coveragerc",
"README.md",
]

for rootpath in dirs:
Expand Down
15 changes: 3 additions & 12 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import nox

nox.options.sessions = "lint_flake8", "lint_pylint", "typecheck", "test"
nox.options.sessions = "lint_pylint", "typecheck", "test"

EDITABLE_TESTS = True
PYTHON_VERSIONS = None
if "GITHUB_ACTIONS" in os.environ:
PYTHON_VERSIONS = ["3.7", "3.11"]
PYTHON_VERSIONS = ["3.9", "3.11"]
EDITABLE_TESTS = False


Expand Down Expand Up @@ -90,15 +90,6 @@ def dev(session):
session.install("-e", ".[dev]")


@nox.session
def lint_flake8(session):
"""
Run flake8 linter.
"""
session.install("-e", ".[lint_flake8]")
session.run("flake8", "src", "tests")


@nox.session
def lint_pylint(session):
"""
Expand All @@ -114,7 +105,7 @@ def typecheck(session):
Typecheck the code using mypy.
"""
session.install("-e", ".[typecheck]")
session.run("mypy", "-p", "fillname", "-p", "tests")
session.run("mypy", "--strict", "-p", "fillname", "-p", "tests")


@nox.session(python=PYTHON_VERSIONS)
Expand Down
40 changes: 40 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,43 @@ requires = [
"setuptools-scm",
]
build-backend = "setuptools.build_meta"

[tool.isort]
profile = "black"
line_length = 120

[tool.black]
line-length = 120

[tool.pylint.format]
max-line-length = 120

[tool.pylint.design]
max-args = 10
max-attributes = 7
max-bool-expr = 5
max-branches = 12
max-locals = 30
max-parents = 7
max-public-methods = 20
max-returns = 10
max-statements = 50
min-public-methods = 1

[tool.pylint.similarities]
ignore-comments = true
ignore-docstrings = true
ignore-imports = true
ignore-signatures = true

[tool.pylint.basic]
argument-rgx = "^[a-z][a-z0-9]*((_[a-z0-9]+)*_?)?$"
variable-rgx = "^[a-z][a-z0-9]*((_[a-z0-9]+)*_?)?$"
good-names = ["_"]

[tool.coverage.run]
source = ["fillname", "tests"]
omit = ["*/fillname/__main__.py"]

[tool.coverage.report]
exclude_lines = ["assert", "nocoverage"]
11 changes: 3 additions & 8 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ url = https://potassco.org/
packages = find:
package_dir =
=src
python_requires = >=3.9
include_package_data = True
install_requires =
importlib_metadata;python_version<'3.8'

[options.packages.find]
where = src
Expand All @@ -25,25 +24,21 @@ format =
black
isort
autoflake
lint_flake8 =
flake8
flake8-black
flake8-isort
lint_pylint =
pylint
typecheck =
types-setuptools
mypy
test =
coverage
coverage[toml]
doc =
sphinx
furo
nbsphinx
sphinx_copybutton
myst-parser
dev =
fillname[test,typecheck,lint_pylint,lint_flake8]
fillname[test,typecheck,lint_pylint]

[options.entry_points]
console_scripts =
Expand Down
2 changes: 1 addition & 1 deletion src/fillname/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .utils.parser import get_parser


def main():
def main() -> None:
"""
Run the main function.
"""
Expand Down
15 changes: 8 additions & 7 deletions src/fillname/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@ class SingleLevelFilter(logging.Filter):
Filter levels.
"""

def __init__(self, passlevel, reject):
passlevel: int
reject: bool

def __init__(self, passlevel: int, reject: bool):
# pylint: disable=super-init-not-called
self.passlevel = passlevel
self.reject = reject

def filter(self, record):
def filter(self, record: logging.LogRecord) -> bool:
if self.reject:
return record.levelno != self.passlevel # nocoverage

return record.levelno == self.passlevel


def setup_logger(name, level):
def setup_logger(name: str, level: int) -> logging.Logger:
"""
Setup logger.
"""
Expand All @@ -42,13 +45,11 @@ def setup_logger(name, level):
logger.setLevel(level)
log_message_str = "{}%(levelname)s:{} - %(message)s{}"

def set_handler(level, color):
def set_handler(level: int, color: str) -> None:
handler = logging.StreamHandler(sys.stderr)
handler.addFilter(SingleLevelFilter(level, False))
handler.setLevel(level)
formatter = logging.Formatter(
log_message_str.format(COLORS[color], COLORS["GREY"], COLORS["NORMAL"])
)
formatter = logging.Formatter(log_message_str.format(COLORS[color], COLORS["GREY"], COLORS["NORMAL"]))
handler.setFormatter(formatter)
logger.addHandler(handler)

Expand Down
8 changes: 3 additions & 5 deletions src/fillname/utils/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
from argparse import ArgumentParser
from textwrap import dedent
from typing import Any, cast
from typing import Any, Optional, cast

__all__ = ["get_parser"]

Expand Down Expand Up @@ -39,7 +39,7 @@ def get_parser() -> ArgumentParser:
("debug", logging.DEBUG),
]

def get(levels, name):
def get(levels: list[tuple[str, int]], name: str) -> Optional[int]:
for key, val in levels:
if key == name:
return val
Expand All @@ -54,7 +54,5 @@ def get(levels, name):
type=cast(Any, lambda name: get(levels, name)),
)

parser.add_argument(
"--version", "-v", action="version", version=f"%(prog)s {VERSION}"
)
parser.add_argument("--version", "-v", action="version", version=f"%(prog)s {VERSION}")
return parser
5 changes: 3 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ class TestMain(TestCase):
Test cases for main application functionality.
"""

def test_logger(self):
def test_logger(self) -> None:
"""
Test the logger.
"""
log = setup_logger("global", logging.INFO)
sio = StringIO()
for handler in log.handlers:
assert isinstance(handler, logging.StreamHandler)
handler.setStream(sio)
log.info("test123")
self.assertRegex(sio.getvalue(), "test123")

def test_parser(self):
def test_parser(self) -> None:
"""
Test the parser.
"""
Expand Down

0 comments on commit d2b6042

Please sign in to comment.