Skip to content

Conversation

@Tschuppi81
Copy link
Contributor

SWI: Ignore empty policy area

TYPE: Bugfix
LINK: swi-63

Checklist

  • I have performed a self-review of my code
  • I considered adding a reviewer
  • I have tested my code thoroughly by hand
  • I have added tests for my changes/features

@Tschuppi81 Tschuppi81 requested a review from Daverball July 25, 2025 13:23
@Tschuppi81
Copy link
Contributor Author

Is there a way to influence the converters to handle this case?

@codecov
Copy link

codecov bot commented Jul 25, 2025

Codecov Report

❌ Patch coverage is 95.23810% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.41%. Comparing base (444995f) to head (ddc3461).
⚠️ Report is 164 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/onegov/core/filters.py 81.81% 2 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
src/onegov/swissvotes/collections/votes.py 98.92% <100.00%> (+<0.01%) ⬆️
src/onegov/swissvotes/converters.py 100.00% <100.00%> (ø)
src/onegov/swissvotes/path.py 97.82% <100.00%> (+0.04%) ⬆️
src/onegov/core/filters.py 86.95% <81.81%> (-3.96%) ⬇️

... and 2 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 444995f...ddc3461. Read the comment docs.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Daverball
Copy link
Member

Daverball commented Jul 29, 2025

Is there a way to influence the converters to handle this case?

Yes, you can write a custom converter for the policy area format, so any values that don't conform to the format get dropped.

Ideally we change the collection to accept list[PolicyArea] instead and write a converter for PolicyArea that verifies the format, split the string over . and make sure each component is numeric and starts with the previous component.

Copy link
Member

@Daverball Daverball left a comment

Choose a reason for hiding this comment

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

This is closer to what I suggested, but it's not quite there yet.

Copy link
Member

Choose a reason for hiding this comment

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

I think you should merge master, this looks like changes that happened a while ago on master, so they should not be included in the diff.

Comment on lines +74 to +77
# ignore empty policy areas swi-63
if policy_area and '' in policy_area:
policy_area = [i for i in policy_area if i]

Copy link
Member

Choose a reason for hiding this comment

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

This check should go away if the converter is doing its job

Copy link
Member

Choose a reason for hiding this comment

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

This seems a little bit too complex for a converter, I would choose two simple converter functions like we do for things like datetime_converter and use the base class. Converter decode functions are allowed to raise exceptions IIRC, but you could also add simple try catch that returns None if splitting the components and turning them into integers fails.

The other issue is that you're converting from str to str. My idea was to convert to PolicyArea and move some of the validation into the PolicyArea constructor, this makes the API less error-prone to use in general, since the collection will now expect a PolicyArea instance rather than str, which has already been validated, with str it's still possible to pass in bogus data from somewhere else.

Copy link
Member

Choose a reason for hiding this comment

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

I'm also not sure if this will actually work, since Converter.__init__ will replace your decode/encode methods with whatever you passed in when constructing the instance, which is str.

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.

3 participants