Skip to content

Commit

Permalink
Introduce Poetry in generated project
Browse files Browse the repository at this point in the history
  • Loading branch information
matyaskuti committed Oct 18, 2023
1 parent 513b134 commit 1c95841
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 128 deletions.
3 changes: 1 addition & 2 deletions cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"package_name": "pymx",
"short_description": "Python project",
"author_name": "Mendix Cloud Value Added Services Team",
"author_email": "dis_valueaddedservices@mendix.com",
"use_pylint": "y"
"author_email": "dis_valueaddedservices@mendix.com"
}
8 changes: 6 additions & 2 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import os
import subprocess

PROJECT_DIRECTORY = os.path.realpath(os.path.curdir)


if __name__ == "__main__":
if "{{ cookiecutter.use_pylint }}" != "y":
os.remove(os.path.join(PROJECT_DIRECTORY, "pylintrc"))
# if " cookiecutter.use_sometool }}" != "y":
# os.remove(os.path.join(PROJECT_DIRECTORY, "sometoolrc"))
# The above couple lines are left as an example for the future.

subprocess.call(["poetry", "lock"], stderr=subprocess.STDOUT)
45 changes: 9 additions & 36 deletions tests/test_project_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@
EXPECTED_PROJECT_FILES = (
DEFAULT_PROJECT_NAME,
".gitignore",
"pyproject.toml",
"README.md",
"setup.py",
"tests",
"pylintrc",
".isort.cfg",
"mypy.ini",
)


Expand Down Expand Up @@ -64,23 +61,6 @@ def assert_expected_files_do_not_exist(
assert cleaned_up not in filename


EXPECTED_PROJECT_FILES_NO_PYLINT = tuple(
file_name
for file_name in EXPECTED_PROJECT_FILES
if file_name != "pylintrc"
)
NO_PLINT = {"use_pylint": "n"}


def test_project_creation_without_pylint(cookies: Cookies) -> None:
with generate_temporary_project(cookies, extra_context=NO_PLINT) as result:
assert_successful_creation(result)
assert_expected_files_exist(
result, files=EXPECTED_PROJECT_FILES_NO_PYLINT
)
assert_expected_files_do_not_exist(result, files=("pylintrc",))


def assert_expected_lines_are_in_output(
expected_lines: Sequence[str],
output: str,
Expand All @@ -89,14 +69,14 @@ def assert_expected_lines_are_in_output(
assert line in output


FILES_TO_CHECK_FORMAT = f"{DEFAULT_PROJECT_NAME} tests setup.py"
BLACK_OUTPUT = f"black --line-length=79 --check --diff {FILES_TO_CHECK_FORMAT}"
FILES_TO_CHECK_FORMAT = f"{DEFAULT_PROJECT_NAME} tests"
BLACK_OUTPUT = f"black --check --diff {FILES_TO_CHECK_FORMAT}"
PYLINT_OUTPUT_1 = f"pylint {DEFAULT_PROJECT_NAME} tests"
PYLINT_OUTPUT_2 = "Your code has been rated at 10.00/10"
ISORT_OUTPUT = f"isort --check-only {DEFAULT_PROJECT_NAME} tests setup.py"
MYPY_OUTPUT = f"mypy --ignore-missing-imports {DEFAULT_PROJECT_NAME} tests"
ISORT_OUTPUT = f"isort --check-only {FILES_TO_CHECK_FORMAT}"
MYPY_OUTPUT = f"mypy --ignore-missing-imports {FILES_TO_CHECK_FORMAT}"
EXPECTED_LINT_OUTPUT = (
"pip3 install -e .[lint]",
"poetry install --with lint",
f"flake8 {DEFAULT_PROJECT_NAME} tests",
"files would be left unchanged",
BLACK_OUTPUT,
Expand All @@ -113,15 +93,8 @@ def test_linting(cookies: Cookies) -> None:
assert_expected_lines_are_in_output(EXPECTED_LINT_OUTPUT, output)


def test_linting_without_pylint(cookies: Cookies) -> None:
with generate_temporary_project(cookies, extra_context=NO_PLINT) as result:
output = check_output_in_result_dir("make lint", result)
assert PYLINT_OUTPUT_1 not in output
assert PYLINT_OUTPUT_2 not in output


EXPECTED_TEST_OUTPUT = (
"pip3 install -e .[test]",
"poetry install --with test",
"test session starts",
"files skipped due to complete coverage.",
)
Expand Down Expand Up @@ -167,9 +140,9 @@ def test_clean_can_be_executed_in_empty_project_dir(cookies: Cookies) -> None:


EXPECTED_FORMAT_OUTPUT = (
f"black --line-length=79 {DEFAULT_PROJECT_NAME} tests setup.py",
f"black {FILES_TO_CHECK_FORMAT}",
"files left unchanged",
f"isort {DEFAULT_PROJECT_NAME} tests setup.py",
f"isort {FILES_TO_CHECK_FORMAT}",
)


Expand Down
6 changes: 0 additions & 6 deletions {{cookiecutter.package_name}}/.isort.cfg

This file was deleted.

22 changes: 8 additions & 14 deletions {{cookiecutter.package_name}}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ PACKAGE_NAME := {{cookiecutter.package_name}}

.PHONY: install_lint_requirements
install_lint_requirements:
pip3 install -e .[lint]
poetry install --with lint

.PHONY: lint
lint: install_lint_requirements
flake8 $(PACKAGE_NAME) tests
black --line-length=79 --check --diff $(PACKAGE_NAME) tests setup.py
{%- if cookiecutter.use_pylint == "y" %}
black --check --diff $(PACKAGE_NAME) tests
pylint $(PACKAGE_NAME) tests
{%- endif%}
isort --check-only $(PACKAGE_NAME) tests setup.py
isort --check-only $(PACKAGE_NAME) tests
mypy --ignore-missing-imports $(PACKAGE_NAME) tests

.PHONY: install_test_requirements
install_test_requirements:
pip3 install -e .[test]
poetry install --with test

.PHONY: test
test: install_test_requirements
Expand All @@ -38,15 +36,11 @@ clean:
find . -regex ".*__pycache__.*" -delete
find . -regex "*.py[co]" -delete

.PHONY: install_build_requirements
install_build_requirements:
pip3 install -e .[build]

.PHONY: build
build: install_build_requirements
python3 setup.py sdist bdist_wheel
build:
poetry build

.PHONY: format
format:
black --line-length=79 $(PACKAGE_NAME) tests setup.py
isort $(PACKAGE_NAME) tests setup.py
black $(PACKAGE_NAME) tests
isort $(PACKAGE_NAME) tests
7 changes: 0 additions & 7 deletions {{cookiecutter.package_name}}/mypy.ini

This file was deleted.

27 changes: 0 additions & 27 deletions {{cookiecutter.package_name}}/pylintrc

This file was deleted.

81 changes: 81 additions & 0 deletions {{cookiecutter.package_name}}/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
[tool.poetry]
name = "{{cookiecutter.package_name}}"
version = "0.1.0"
description = "{{cookiecutter.short_description}}"
readme = "README.md"
classifiers = [
"Programming Language :: Python :: 3",
"Framework :: Pytest",
"Typing :: Typed",
]
authors = ["{{cookiecutter.author_name}} <{{cookiecutter.author_email}}>"]
packages = [{ include = "{{cookiecutter.package_name}}" }]

[tool.poetry.dependencies]
python = "^3.10"

[tool.poetry.group.lint.dependencies]
flake8 = "^6"
black = "^23"
isort = "^5"
pylint = "^3"
mypy = "^1"

[tool.poetry.group.test.dependencies]
pytest = "^7"
pytest-cov = "^4"

[build-system]
requires = ["poetry-core<2"]
build-backend = "poetry.core.masonry.api"

[tool.black]
line-length = 79

[tool.isort]
ensure_newline_before_comments = true
include_trailing_comma = true
line_length = 79
multi_line_output = 3
split_on_trailing_comma = true

[tool.mypy]
strict = true
pretty = true

warn_redundant_casts = true
warn_unused_ignores = true
warn_no_return = true

[tool.pylint.BASIC]
module-rgx = "[a-z_][a-z0-9_]{2,32}$"
const-rgx = "[a-zA-Z_][a-zA-Z0-9_]{2,32}$"
class-rgx = "[A-Z_][a-zA-Z0-9]+$"
function-rgx = "([a-z_][a-z0-9_]{2,32}|(assert|test_|__)[a-z_][a-z0-9_]{2,80})$"
method-rgx = "([a-z_][a-z0-9_]{2,32}|(assert|test_|__)[a-z_][a-z0-9_]{2,80})$"
attr-rgx = "[a-z_][a-z0-9_]{2,32}$"
argument-rgx = "[a-z_][a-z0-9_]{2,32}$"
variable-rgx = "[a-z_][a-z0-9_]{2,32}$"
class-attribute-rgx = "([A-Za-z_][A-Za-z0-9_]{2,32}|(__.*__))$"
inlinevar-rgx = "[A-Za-z_][A-Za-z0-9_]*$"

[tool.pylint."MESSAGES CONTROL"]
disable = [
"missing-docstring",
"locally-disabled",
"too-few-public-methods",
"fixme",
]
msg-template = "{module}:{line} [{msg_id}: {symbol}] - {msg}"

[tool.pylint.REPORTS]
reports = "yes"

[tool.pylint.FORMAT]
max-line-length = 79

[tool.pylint.SIMILARITIES]
min-similarity-lines = 6
ignore-comments = "yes"
ignore-docstrings = "yes"
ignore-imports = "yes"
32 changes: 0 additions & 32 deletions {{cookiecutter.package_name}}/setup.py

This file was deleted.

This file was deleted.

0 comments on commit 1c95841

Please sign in to comment.