Skip to content

Commit

Permalink
⚑ 🐍 Change python default to pyproject.toml
Browse files Browse the repository at this point in the history
The days of setuptools are numbered! Switch default component to use
pyproject.toml, it is currently the least deprecated way of building
python packages.

Also sneak in some nifty features like adding a `run` command for
clients and services. And add a corresponding entry point to their
package.

Also made the generated code pass `ruff`, it uses the
`--unsafe-fixes` to add period at the end of docstrings if missing.
Since it only runs on the templated code we know that it won't do
anything else.
  • Loading branch information
simonrainerson committed Oct 30, 2024
1 parent d21f4f5 commit 9472b82
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 20 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Python now generates a `run` command and cli entry point for clients and services.

### Changed
- Python now uses `pyproject` instead of `setuptools` by default. Template for targetSetup
has been updated accordingly. It also conforms to `ruff`s checks and formatting by default.

### Fixed
- Python's config merger now handles mypy overrides targeting multiple packages.

Expand Down
4 changes: 2 additions & 2 deletions python/component-template/@mainPackage@/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
""" @desc@ """
"""@desc@"""


def main() -> None:
""" main function of @mainPackage@ """
"""Main function of @mainPackage@."""
raise NotImplementedError
18 changes: 18 additions & 0 deletions python/component-template/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[build-system]
requires = ["setuptools"]

[project]
name = "@pname@"
version = "@version@"
description = "@desc@"
authors = [{name = "@author@", email = "@email@"}]

[setuptools.packages.find]
exclude = ["tests*"]

[urls]
Homepage = "@url@"

[project.scripts]
@entryPoint@

13 changes: 0 additions & 13 deletions python/component-template/setup.py

This file was deleted.

5 changes: 3 additions & 2 deletions python/component-template/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Tests for @mainPackage@ in @pname@ """
"""Tests for @mainPackage@ in @pname@."""
import @mainPackage@.main


def test_main() -> None:
""" Tests for the main function """
"""Tests for the main function."""
@mainPackage@.main.main()
3 changes: 3 additions & 0 deletions python/main.py.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

if __name__ == "__main__":
main()
11 changes: 8 additions & 3 deletions python/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,26 @@ let
inherit version;
pname = name;
mainPackage = lib.toLower (builtins.replaceStrings [ "-" " " ] [ "_" "_" ] name);
entryPoint = if setuptoolsLibrary then "{}" else "{\\\"console_scripts\\\": [\\\"${name}=${mainPackage}.main:main\\\"]}";
entryPoint = if setuptoolsLibrary then "" else "${name}=\\\"${mainPackage}.main:main\\\"";
} // args.targetSetup.variables or { };
variableQueries = {
desc = "✍️ Write a short description for your component:";
author = "πŸ€“ Enter author name:";
email = "πŸ“§ Enter author email:";
url = "πŸ„ Enter author website url:";
} // args.targetSetup.variableQueries or { };
initCommands = "black .";
initCommands = ''
${if ! setuptoolsLibrary then "cat ${./main.py.in} >>$mainPackage/main.py" else ""}
ruff format .
ruff check --fix --unsafe-fixes .
'';
});

pythonPackageArgs = attrs // {
inherit version preBuild doStandardTests pythonVersion propagatedBuildInputs;
src = if lib.isStorePath src then src else filteredSrc;
pname = name;
format = attrs.format or "pyproject";

# Don't install dependencies with pip, let nix handle that
preInstall = ''
Expand Down Expand Up @@ -170,7 +175,7 @@ let
Show the config Nedryland has generated for a linter, one of:
black, coverage, flake8, isort, mypy, pylint, pytest'';
};
} // attrs.shellCommands or { });
} // (lib.optionalAttrs (! setuptoolsLibrary) { run = { script = ''python -m ${name}.main "$@"''; description = "Run the main module."; }; }) // attrs.shellCommands or { });
};
in
pythonPkgs.buildPythonPackage pythonPackageArgs

0 comments on commit 9472b82

Please sign in to comment.