Feedback wanted on pyorder, a Python definition-ordering linter

Implementing ones own solution is the best way imo to learn and become better, very true

I have never worked with any codebase where I would prefer automatic ordering to deliberate programmer choice. And I have worked with a lot of codebases. Maybe the original reasons can’t be articulated, but they are there, and they mattered. Don’t throw that away just because you don’t understand why someone did what they did.

I’m working in a code base where sometimes there is a reason for the ordering of functions, and sometimes there isn’t. Recently one class had all their methods ordered alphabetically, and that was a big improvement for that class.
But in the code base as a whole I wouldn’t want automatic ordering of classes and functions either.

Comparison with Black is cute to me, because Black also sometimes discards the intent of the author.
Sometimes there is a good reason to prefer

def f(...): return ...

over

def f(...):
  return ...

Sometimes Black makes it impossible to put the line breaks in the most logical place (in long expressions with brackets particularly).

Yet these downsides of reduced expressiveness are generally weighed less than the advantage of regularity, and of not having the formatting of an expression depend on which author last touched it.

Thats valid and then you should not consider a tool like pyorder but it definitely has possible usages. I think flat out saying it is has no applications is just incorrect.

Yeah the tool only makes sense to use if you want regularity and a constant way of ordering

It it not that the order has any meaning to Python, but to human readers, where the logical ordering of methods at the same call level may not be alphabetical, or where related methods are grouped with comments.

Another example where one size does not fit all. In idlelib modules (about 70), where only stdlib imports are allowed, I (and others at the time) settled on an order that makes sense for idlelib: ‘other’ stdlib modules alphabetically, tkinter and maybe tkinter submodules, other idlelib modules.

Additional notes to consider:

  • ‘caller-first’ is also called ‘top-down’; a legitimate alternative is ‘callee-first’ or ‘bottom-up’. If I adopted either, I would want to be able to check if my ordering was consistent with it without alphabetical checks.

  • Some module functions call class methods, others are called by class methods. The appropriate function-class order, according to caller-callee rule, would then depend on the type of function. Your function-class rule is not always consistent with your caller-callee rule.

  • Some code checkers are flexible, like linters that allow users to choose which rules to apply. Some are opinionated, like Black, limiting their user base to people who agree with the choices. Your program appears to be of the latter type, but your opening message says that you are flexible about which rules to (inflexibly?) apply. You need to decide and be clear about your end goal.

The alphabetical ordering is just a final tie breaker in the sorting key if all previous ranks are equal, but its valid to say the existing order should be preferred in a tie break, a plugin could solve this very easily.

In case it is not clear: The sorting is applied through a sorting key where each rule is a key of that larger sorting key, meaning a rule only ever decides a tiebreak after the previous rule: So callers-first is the first rule and if the order of two definitions is not decided by that then the functions-before-classes rule decides and so on, therefore rules cannot interfere with each other by design and the sorting is always consistent and reliable.

You are right, Pyorder is very inflexible if you consider the adjustablity through a config file or suppression comments which by design I did not allow. However it is very flexible via plugins, which offer perfect adjustability, it is possible to add, remove or adjust any rules in whatever way please the developer this way. The goal is to define the rules one wants their code ordered by and if they dont agree with the (admittedly strict) defaults. This way a project is able to have a constant ordering that pleases any developer who want it.

The distinction between modular grouping and the ordering within a single module seems useful here. I can see why an enforced ordering would matter more as a codebase grows and the original rationale for a layout gets lost, even if for many projects it stays a matter of taste. The plugin system sounds like it addresses the main objection, since anyone who disagrees with the default rules isn’t stuck with them. Chris Angelico’s point about implementing your own solution as a way to learn also stands out to me. Even a niche tool can teach its author a lot about how code gets organized.

Thanks, any thoughts on the rules I implemented or any chance you could try it out?