sst-core icon indicating copy to clipboard operation
sst-core copied to clipboard

Enforce Python code standards and formatting

Open berquist opened this issue 3 months ago • 2 comments

Even though this doesn't touch the C++ code, it does affect writing tests and modifying the test suite, so I'm marking this as a major feature.

A large chunk of what can be considered coding standards is handled automatically for the C++ code by using clang-format. The benefits of not having to think about this are missing from the Python code, specifically the test framework. I'm tired of worrying about formatting and style when modifying or adding to the test framework.

This would be automatically handled by the pre-commit tool, specifically ruff. It already runs in pre-commit, but just the linter, and only a subset of the default linter rules are currently used that didn't require making major changes to the code. The changes to the configuration would be:

  • turning on the formatter (add id: ruff-format to the list of hooks)[^1]
  • enable additional lint rules for enforcement Which rules and fixes are chosen are largely a matter of taste, much like clang-format, but many also prevent certain code features or patterns, similar to if we used clang-tidy.

If we don't want to enforce formatting for certain files, such as the SST inputs, they can be excluded. I would prefer the test runners are included, but the most important code to format is the test framework.

There is a particular style I see in the inputs that might be able to be enforced where key-value pairs in option dicts are always on separate lines, and the formatter prioritizes maximizing line length:

-StatBasic1.enableStatistics(["stat3_I32"], {
-    "type" : "sst.AccumulatorStatistic",
-    "rate" : "8 events",
-    "startat" : "25 ns",
-    "stopat" : "60 ns"})
+StatBasic1.enableStatistics(
+    ["stat3_I32"],
+    {"type": "sst.AccumulatorStatistic", "rate": "8 events", "startat": "25 ns", "stopat": "60 ns"},
+)

-StatBasic1.enableStatistics(["stat4_I64"], {
-    "type" : "sst.AccumulatorStatistic",
-    "startat" : "35 ns",
-    "stopat" : "70 ns",
-    "rate" : "14 events",
-    "resetOnOutput" : True})
+StatBasic1.enableStatistics(
+    ["stat4_I64"],
+    {
+        "type": "sst.AccumulatorStatistic",
+        "startat": "35 ns",
+        "stopat": "70 ns",
+        "rate": "14 events",
+        "resetOnOutput": True,
+    },
+)

If having the current appearance is important and this behavior can't be configured, then inputs will need to be excluded.

[^1]: There is another tool, pyupgrade, that adds features for a baseline Python version, such as converting % and format strings to f-strings when the minimum version is 3.6. Two problems are that 1. pyupgrade adds another Python-based hook, which slows down pre-commit, since ruff is fast, and 2. I think ruff can be configured to both lint and auto-apply pyupgrade. This will take some research, but configuring additional rules would anyway.

berquist avatar Oct 09 '25 18:10 berquist