Skip to content

Conversation

josephw
Copy link
Contributor

@josephw josephw commented Nov 25, 2021

The discussion in #649 suggests that there's no specific reason for sorting directly after parsing. Looking at the code, it appears to be a relatively surface-level decision to sort the results. This PR adds a small test for ordering, and removes the sorting from get_entries.

The original discussion says:

A deep code inspection would be required through.

While this change doesn't cause any tests to fail, I'm unclear on whether there are other subtleties here that I've missed.

resolves #649

@josephw josephw force-pushed the parse_file-retain-original-file-order branch from ebc52e6 to 0e3ee3e Compare November 25, 2021 01:28
Keep the original order for the parsed entries, so round-tripped modifications
don't restructure an out-of-order file. Take a defensive copy, to preserve
the behaviour from 'sorted' of not giving out a mutable reference.
@josephw josephw force-pushed the parse_file-retain-original-file-order branch from 0e3ee3e to 1e2525f Compare November 25, 2021 01:31
@dnicolodi
Copy link
Collaborator

This would be an API change. If it is desired to have the entries sorted as in the ledger file, is easy to sort them by the line number saved in the metadata. Thus I don't think it is worth the API breakage in v2. In v3 the parser is being anyhow rewritten in C++, thus the change to the Python code is not particularly meaningful. Also, almost all tests are on ledger snippets where the entries are sorted by date, thus the fact that the tests still pass is not a good indication that there aren't palces whete the entries are expected to be sorted and that would break if this change is introduced.

@josephw
Copy link
Contributor Author

josephw commented Nov 26, 2021

Thanks for the context.

thus the change to the Python code is not particularly meaningful.

You're right; and the grammar.py change here is not significant. My intention here was to turn that issue into a part of v3's unit test suite, with the assumption that the C++ rewrite would be tested against the existing unit tests.

If this is an acceptable API change for v3, updating the existing Python version seems like a reasonable way to record that. If it's not, #649 could probably be closed now as WONTFIX.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Is there a reason why parse.parse_file() sorts entries by date?

2 participants