Skip to content

Fix IntegerVectors membership for empty vectors - #42631

Open
vivek41-glitch wants to merge 2 commits into
sagemath:developfrom
vivek41-glitch:fix/integervectors-empty-max-42527-v2
Open

Fix IntegerVectors membership for empty vectors#42631
vivek41-glitch wants to merge 2 commits into
sagemath:developfrom
vivek41-glitch:fix/integervectors-empty-max-42527-v2

Conversation

@vivek41-glitch

Copy link
Copy Markdown
Contributor

Fixes #42527.

Summary

This PR fixes the membership check for empty integer vectors in IntegerVectorsConstraints.

The original issue reported that the following returned False:

[] in IntegerVectors(max_length=0)

even though the empty vector satisfies the given constraint.

Root cause

The implementation of IntegerVectorsConstraints.__contains__ directly returned the result of

check_integer_list_constraints(x, singleton=True, **self.constraints)

When singleton=True, check_integer_list_constraints returns:

  • the vector itself if it satisfies the constraints,
  • None otherwise.

For an empty vector, the returned value is []. Although this represents a valid result, an empty list is falsy in Python, causing __contains__ to incorrectly return False.

This PR changes the membership test to explicitly check whether the result is None, preserving valid empty vectors while still rejecting invalid ones.

Additional issue discovered

While investigating the reported bug, I found that check_integer_list_constraints also did not correctly handle empty lists for min_part and max_part.

The implementation previously called:

min(x)
max(x)

which raises a ValueError for empty lists.

This PR updates these checks to use appropriate defaults:

min(x, default=Infinity)
max(x, default=-Infinity)

This gives the expected mathematical behavior for empty vectors and allows the constraint checks to work correctly without raising exceptions.

Tests

Added doctests covering the empty-vector membership cases:

[] in IntegerVectors(max_part=0)
True

[] in IntegerVectors(max_length=0)
True

All affected doctests pass successfully.

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown

Documentation preview for this PR (built with commit 06c69b2; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

@cxzhong

cxzhong commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

please rebase to latest develop and fix the lint

@vivek41-glitch
vivek41-glitch force-pushed the fix/integervectors-empty-max-42527-v2 branch from 2c859eb to 06c69b2 Compare August 6, 2026 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IntegerVector of length 0 doesn't interact well with max constraints

2 participants