Skip to content

Conversation

Liam-DeVoe
Copy link
Contributor

@Liam-DeVoe Liam-DeVoe commented Aug 26, 2025

Issue number: #7252

The implementation of slice_dictionary has a bug: it will keep repeating the first chunk, because the stop index chunk_size is not incremented as it iterates.

import itertools

def slice_dictionary(data: dict, chunk_size: int):
    for _ in range(0, len(data), chunk_size):
        yield {dict_key: data[dict_key] for dict_key in itertools.islice(data, chunk_size)}

data = {"k0": 0, "k1": 1, "k2": 2}
# actual   [{'k0': 0, 'k1': 1}, {'k0': 0, 'k1': 1}]
# expected [{'k0': 0, 'k1': 1}, {'k2': 2}]
print(list(slice_dictionary(data, 2)))

fwiw: I found this bug during a research project I'm working on which uses LLMs to write property-based tests in Hypothesis. A property which found this bug was proposed and written "autonomously" by the agent. I wrote this PR myself and take full responsibility for it.


(By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.)

@Liam-DeVoe Liam-DeVoe requested a review from a team as a code owner August 26, 2025 00:32
@Liam-DeVoe Liam-DeVoe requested a review from hjgraca August 26, 2025 00:32
@pull-request-size pull-request-size bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Aug 26, 2025
Copy link

boring-cyborg bot commented Aug 26, 2025

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@anafalcao
Copy link
Contributor

Hey @tybug ! Thank you so much for opening this PR, we’ll review it and get back to you.

@github-actions github-actions bot added the bug Something isn't working label Aug 26, 2025
Copy link

codecov bot commented Aug 26, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.35%. Comparing base (3aada65) to head (ca8c2b7).
⚠️ Report is 4 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #7246   +/-   ##
========================================
  Coverage    96.35%   96.35%           
========================================
  Files          275      275           
  Lines        12980    12980           
  Branches       965      965           
========================================
  Hits         12507    12507           
  Misses         366      366           
  Partials       107      107           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@leandrodamascena
Copy link
Contributor

fwiw: I found this bug during a research project I'm working on which uses LLMs to write property-based tests in Hypothesis. A property which found this bug was proposed and written "autonomously" by the agent. I wrote this PR myself and take full responsibility for it.

(By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.)

Hey @tybug this code looks good to me. But I'm curious about the hypothesis you created. I have plans to introduce hypothesis framework in this project to exactly cover cases like this. Do you have a minimal repository where I can those tests?

Copy link

@leandrodamascena leandrodamascena requested review from leandrodamascena and removed request for hjgraca August 28, 2025 15:52
Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

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

Thanks a lot for working on this @Liam-DeVoe! APPROVED

@leandrodamascena leandrodamascena merged commit aface09 into aws-powertools:develop Aug 28, 2025
12 checks passed
Copy link

boring-cyborg bot commented Aug 28, 2025

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

@Liam-DeVoe
Copy link
Contributor Author

Liam-DeVoe commented Aug 28, 2025

I have plans to introduce hypothesis framework in this project to exactly cover cases like this. Do you have a minimal repository where I can those tests?

As a Hypothesis maintainer, that makes me very happy to hear :)

Here's the full bug report written autonomously by the agent, which has the property-based test it wrote inside it:
bug_report_slice_dictionary_2025-08-18_23-20_k8f3.md.

This test is...fine, but it's not particularly idiomatic. The way I might write it instead is:

from hypothesis import given, strategies as st
from aws_lambda_powertools.shared.functions import slice_dictionary

@given(
    st.dictionaries(st.text(), st.integers()),
    st.integers(min_value=1)
)
def test(data, chunk_size):
    if not data:
        chunks = list(slice_dictionary(data, chunk_size))
        assert chunks == []
        return

    chunks = list(slice_dictionary(data, chunk_size))

    reconstructed = {}
    for chunk in chunks:
        reconstructed.update(chunk)

    assert reconstructed == data

and possibly the empty-list case could be removed/folded into the standard property as well (I would have to think about it a bit more).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working commons size/S Denotes a PR that changes 10-29 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: slice_dictionary yields duplicate chunks instead of slicing dictionary correctly
3 participants