Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve bug with sorting of table columns with mixed value types #1278

Merged
merged 1 commit into from
May 30, 2024

Conversation

marc-vdm
Copy link
Member

resolve #1215

Checklist

  • Keep pull requests small so they can be easily reviewed.
  • Update the documentation, please follow the numpy style guide.
  • Update tests.
  • Categorize the PR by setting a good title and adding one of the labels:
    bug, feature, ui, change, documentation, breaking, ci
    as they show up in the changelog.
  • Link this PR to related issues by using closing keywords.
  • Add a milestone to the PR (and related issues, if any) for the intended release.
  • Request a review from another developer.

@marc-vdm marc-vdm added the bug Issues/PRs related to bugs label Apr 20, 2024
@marc-vdm marc-vdm added this to the 2.9.8 milestone Apr 20, 2024
@coveralls
Copy link

Coverage Status

coverage: 50.63% (-0.02%) from 50.652%
when pulling e84b94f on marc-vdm:sort_real_values
into 94ddba5 on LCA-ActivityBrowser:master.

@mrvisscher mrvisscher merged commit cb64dca into LCA-ActivityBrowser:main May 30, 2024
15 checks passed
if type(left_data) is type(right_data):
return left_data < right_data
elif type(left_data) in (int, float) and type(right_data) is str and right_data == "":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to use isinstance() instead of type(). I believe type() is faster, but isinstance looks cleaner to me.

This could be rewritten as:
elif type(left_data) in (int, float) and isinstance(right_data, str) and not right_data

If right_data is a string, not right_data would be falsy. We can do the same on line 435.

This would just make the code look a bit cleaner in my opinion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To go a step further, I think it would be cleaner to try left_data < right_data directly. They don't need to have the same type to compare the two objects.

And isinstance is exactly the right answer here.


If `left` and `right` are not the same type, we check if numerical and empty string are compared, if that is the
case, we assume empty string == 0.
Added this case for: https://github.com/LCA-ActivityBrowser/activity-browser/issues/1215"""
left_data = self.sourceModel().data(left, 'sorting')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We seem to have an inconsistent usage of single quotes versus double quotes. This is not a technical issue but consistency across the codebase will allow new people to easily come in and understand what's going on.

class ABSortProxyModel(QSortFilterProxyModel):
"""Reimplementation to allow for sorting on the actual data in cells instead of the visible data.

See this for context: https://github.com/LCA-ActivityBrowser/activity-browser/pull/1151
"""
def lessThan(self, left, right):
def lessThan(self, left, right) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about importing QModelIndex for the left and right arguments so that we can have better type hinting support?

Try this:

from PySide2.QtCore import QModelIndex```


Then update the function signature to this:

def lessThan(self, left: QModelIndex, right: QModelIndex) -> bool:```

If we try to be consistent with type hinting, then we will be able to detect errors by using linters like mypy.

@marc-vdm
Copy link
Member Author

marc-vdm commented Jun 3, 2024

Thanks for your reviews both, that's is always welcome 👍! though more useful on PRs that are open ;)

I'm writing one reply for this PR to all your comments, that's a bit easier than writing a bunch of comments imo

@ughstudios
I like to use isinstance() instead of type()

Feel free to open a PR, I'm happy to merge it!

@cmutel
To go a step further, I think it would be cleaner to try left_data < right_data directly

The reason this PR exists is because left_data < right_data didn't work, see linked issue. This is also documented in code (L426-L429)

@ughstudios
We seem to have an inconsistent usage of single quotes versus double quotes

You are correct, our code isn't very consistent at the moment. This is something we're working on, see #1048 for general cleanup plans

@ughstudios
What do you think about importing QModelIndex

Feel free to open a PR, I'm happy to merge it!

@ughstudios
Copy link
Contributor

Thanks for your reviews both, that's is always welcome 👍! though more useful on PRs that are open ;)

I'm writing one reply for this PR to all your comments, that's a bit easier than writing a bunch of comments imo

@ughstudios
I like to use isinstance() instead of type()

Feel free to open a PR, I'm happy to merge it!

@cmutel
To go a step further, I think it would be cleaner to try left_data < right_data directly

The reason this PR exists is because left_data < right_data didn't work, see linked issue. This is also documented in code (L426-L429)

@ughstudios
We seem to have an inconsistent usage of single quotes versus double quotes

You are correct, our code isn't very consistent at the moment. This is something we're working on, see #1048 for general cleanup plans

@ughstudios
What do you think about importing QModelIndex

Feel free to open a PR, I'm happy to merge it!

It looks like I don't have the ability to make new branches? Can you give me access? I can make them locally of course, but in order to open a PR it needs to be on the github repo.

@marc-vdm
Copy link
Member Author

marc-vdm commented Jun 4, 2024

Happy to see you managed to figure it out. Feel free to have a read through CONTRIBUTING.md for such info like forking/branching in the future :)

@marc-vdm marc-vdm modified the milestones: 2.9.8, 2.10 Jun 25, 2024
@marc-vdm marc-vdm deleted the sort_real_values branch June 27, 2024 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues/PRs related to bugs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sorting of Elementary Flow Contributions table does not work properly
5 participants