Skip to content

Require a SweepSource in a sweep's source position - #41

Merged
fedonman merged 4 commits into
mainfrom
require-sweep-source
Sep 7, 2026
Merged

Require a SweepSource in a sweep's source position#41
fedonman merged 4 commits into
mainfrom
require-sweep-source

Conversation

@fedonman

@fedonman fedonman commented Sep 7, 2026

Copy link
Copy Markdown
Member

QProgram.sweep() and the Sweep block now take a SweepSource and nothing else. A sequence of points in the source position raises a ValidationError naming the two spellings that pick explicit values, sweep(variable, qp.Values([...])) and sweep(variable).from_values([...]). The combinators are unaffected, and so is the .qp bracket literal, which still reads back as Values.

Sweep now takes a source and nothing else. A sequence of points raises a
ValidationError naming Values() and .from_values(), so the block always
holds something that can answer its own length and kind and write itself
back out to .qp.
@pullpo-for-slack

Copy link
Copy Markdown

🔗Pullpo.io Slack PR-Channel

@pullpo-for-slack

pullpo-for-slack Bot commented Sep 7, 2026

Copy link
Copy Markdown

AI Analysis

Purpose of the changes:

Refactor the handling of sweep sources in the Sweep class to enforce stricter validation, replacing the acceptance of bare sequences with explicit requirements for SweepSource objects.

Improvements and suggestions

(dropdown):
  • In file src/qprogram/blocks/sweep.py / _require_source

Issue:
The function _require_source does not handle the case where a callable is passed as a source, which could cause runtime errors if not properly validated.

Suggested solution:
Ensure to check for callable types in the _require_source function and raise a ValidationError with a clear message if a callable is provided.

Detailed file changes

(dropdown):
  • In file src/qprogram/blocks/sweep.py:

    • Renamed the _coerce_source function to _require_source to better reflect its purpose of enforcing strict source validation.
    • Updated the error message in _require_source to clarify that only SweepSource objects are accepted, removing the previous allowance for bare sequences.
    • Modified the Sweep class constructor to utilize _require_source instead of _coerce_source, ensuring stricter validation on initialization.
  • In file tests/test_blocks.py:

    • Updated the test case to reflect the new validation requirements, ensuring that a sequence of points raises a ValidationError instead of being accepted.
    • Removed the test case that checked for acceptance of a bare sequence, replacing it with a test that asserts a ValidationError is raised for invalid sources.
  • In file src/qprogram/qprogram.py:

    • Updated the docstring for the sweep method to clarify that only SweepSource objects are accepted, removing references to bare sequences.
  • In file docs/guide/control-flow.md:

    • Modified documentation to reflect the new validation logic for sweep sources, emphasizing the need for explicit lists of points as Values.
  • In file docs/reference/errors.md:

    • Updated the error documentation to specify that a SweepSource is required, removing references to 1-D sequences.

@pullpo-for-slack

Copy link
Copy Markdown

AI Analysis

of the newly committed changes

Purpose of the changes:

The changes improve the documentation and error messages related to the Sweep class and its usage, enhancing clarity for users regarding the expected input types.

Detailed file changes

(dropdown):
  • In file src/qprogram/blocks/sweep.py:

    • Removed redundant explanation in the docstring about explicit list of points for source parameter.
    • Simplified the ValidationError message in the _require_source function to focus on the type of the source.
  • In file src/qprogram/qprogram.py:

    • Updated the docstring for the sweep method to remove redundancy regarding the explicit list of points.
  • In file tests/test_blocks.py:

    • Modified the test docstring to clarify that the block binds a source, not the points a source would produce.

@pullpo-for-slack

Copy link
Copy Markdown

AI Analysis

of the newly committed changes

Purpose of the changes:

Refactor test case to improve readability by reusing variable for source.

Detailed file changes

(dropdown):
  • In file tests/test_blocks.py:
    • Introduced a variable 'v' to hold the instance of Variable('x') for better readability.
    • Updated the test to use the variable 'v' instead of creating a new Variable instance directly in the Sweep function call.

@pullpo-for-slack

Copy link
Copy Markdown

AI Analysis

of the newly committed changes

Purpose of the changes:

Clarifying the usage of sweep sources and builders in the control flow documentation.

Detailed file changes

(dropdown):
  • In file docs/guide/control-flow.md:
    • Removed redundant explanation regarding the handling of omitted sources, streamlining the text for better clarity.
    • Clarified that a builder is not a context manager due to the absence of values, emphasizing error handling.

@sonarqubecloud

sonarqubecloud Bot commented Sep 7, 2026

Copy link
Copy Markdown

@fedonman
fedonman merged commit 3df9736 into main Sep 7, 2026
9 checks passed
@fedonman
fedonman deleted the require-sweep-source branch September 7, 2026 16:04
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.

1 participant