GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-107862: Add property-based round-trip tests for base64 by encukou · Pull Request #119406 · python/cpython

Conversation

@encukou

@encukou encukou commented May 22, 2024 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Add round-trip tests for the various encodings in the base64 module.
These are rather stable, so I expect the tests will serve more as examples
of how to use the Hypothesis API.

Also, fix an issue in the @example decorator stub: it didn't work with multiple
positional arguments.

cc @Zac-HD

  • Issue: [Meta] Research: what can we test with Hypothesis? #107862

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
encukou added 2 commits May 22, 2024 10:58
@encukou
Add property-based tests to test_base64
351dc9e
@encukou
Allow multiple positional arguments in @hypothesis.example stub
bac685f
@bedevere-app bedevere-app Bot added the awaiting core review label May 22, 2024
@bedevere-app bedevere-app Bot mentioned this pull request May 22, 2024
[Meta] Research: what can we test with Hypothesis? #107862
Open
@encukou encukou added tests Tests in the Lib/test dir skip news labels May 22, 2024
Zac-HD
Zac-HD approved these changes May 22, 2024
View reviewed changes

@Zac-HD Zac-HD left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Looks good! We don't need the @composite decorator in this case, but otherwise lovely idiomatic use of Hypothesis.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@encukou encukou mentioned this pull request May 22, 2024
Clarify base64.a85encode docs: *wrapcols* doesn't count the newline #119409
Merged
Zac-HD
Zac-HD reviewed May 22, 2024
View reviewed changes
Comment thread Lib/test/test_base64.py Outdated
Comment on lines +25 to +26
return bytes(draw(hypothesis.strategies.lists(allowed_chars, min_size=2,
max_size=2, unique=True)))

@Zac-HD Zac-HD May 22, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Instead of using @composite here, we can simply return a strategy from our function:

Suggested change
return bytes(draw(hypothesis.strategies.lists(allowed_chars, min_size=2,
max_size=2, unique=True)))
return hypothesis.strategies.lists(allowed_chars, min_size=2, max_size=2, unique=True))).map(bytes)

In more complicated scenarios, this can be a decent performance improvement (by amortizing the construction and validation over multiple draws, instead of executing the body each time we draw a value) - particularly if we @functools.cache the function, or happen to draw from it multiple times (e.g. st.lists(altchars())). Mostly though it's just an idiom to remind readers that strategies are just python values, not magic.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
encukou added 2 commits May 22, 2024 11:45
@encukou
Simplify the altchars strategy
b28b80a
@encukou
Simplify the altchars strategy
2539d41
@rhettinger

rhettinger commented May 22, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

I thought there had been a decision not to add hypothesis tests to any module not under active development. During development, hypothesis is good at teasing out special cases that need to be fixed and have an explicit targeted test. Once in production, hypothesis doesn't do much for us and leaves it unclear whether the search space for a property has actually been covered.

AFAICT this PR doesn't add any value, it just eats clock cycles and adds another tool that a person has to learn before they can read the tests. If our current tests are inadequate in any way, we should add specifically targeted tests to fill the gaps.

We should differentiate a goal we care about, "thoroughly understand and test our code" from the more dubious goal, "Let's use this tool every we can." ISTM that this PR only does the latter and doesn't help base64 in any way.

Also note, that we've already had a number of people do the exercise of trying hypothesis on many standard library modules. We were able to do that without actually checking in the tests into main distribution. FWIW, the main result of the exercise was identifying bugs in the tester's understanding of the module (i.e. an expectation that colorsys conversions would round-trip without understanding gamut limits and published specifications that did not round-trip).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Zac-HD

Zac-HD commented May 22, 2024

Copy link
Copy Markdown
Contributor

I think these concerns are addressed in python/steering-council#65 and #107862; the stub implementation we added in #22863 has very low overhead.

We're starting with simpler cases rather than e.g. #83134, because it's easier to shake out config issues and get used to the workflow when we don't also need to work out how we'll handle e.g. the strategy to generate arbitrary Python source code.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@encukou

encukou commented May 22, 2024

Copy link
Copy Markdown
Member Author

I thought there had been a decision not to add hypothesis tests to any module not under active development.

I couldn't find that decision, do you have a link to it?
I'm happy to ask the SC for a decision if that's what we need.

I agree that the issues found by Hypothesis should be added to the tests that always run, though it might be enough to add them as @example.
Gaps in understanding can usually be used improve documentation -- see #119409 for two non-obvious things I found here.

Checking this into the repo means it stays in sync with stdlib.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rhettinger

rhettinger commented May 22, 2024

Copy link
Copy Markdown
Contributor

Focusing on this particular PR as an example of what is to come, is anyone claiming that it adds any value at all? Will users be any better-off? I don't think so.

AFAICT only the time and datetime module maintainer has said they wanted this (because it is part of their development process). No other module maintainer has asked for this. I expect that every such PR in the future will come from devs who have not previously shown any interest in the module being tested. Their goal isn't to improve the module. Their goal is just to use hypothesis. IMO that is a false goal. I don't think it should be done.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@encukou

encukou commented May 22, 2024

Copy link
Copy Markdown
Member Author

The goal is to use property-based testing in general -- i.e. using a tool to find edge cases that humans are bad at finding.

You're right that this is meant for tests in general: rather than base64 itself I'm interested in hypothesis/regrtest integration, the stubs (see the second commit here), and finding usability issues in running these tests. I'd love to get to the point where people who are actively developing a module have a bunch of examples and institutional knowledge ready to help them find new edge cases.

Zac-HD reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rhettinger

rhettinger commented May 22, 2024

Copy link
Copy Markdown
Contributor

FWIW, we already test properties (invariants) almost everywhere they are applicable (even in base64). You don't need hypothesis for that.

Hypothesis doesn't have a monopoly on properties or invariants (pretty much every existing assertion does that). The superpower of hypothesis is generating semi-random input patterns to find problems and then simplify to a minimal reproducer. That is helpful during development but not really something we want in the CI. Instead, a core-dev is expected to produce a comprehensive and deterministic sets of tests that reflect a full understanding of the code (both black-box and white-box).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@encukou

encukou commented May 23, 2024

Copy link
Copy Markdown
Member Author

I do not see how “not really something we want in the CI” follows from what was said before.

If this stuff is helpful during development, it should also be helpful to testing that the hypothesis/regrtest integration works as intended, and to have a bunch of worked examples around, so that getting it to run when you need it isn't an uphill battle.

Yes, a core dev is expected to produce good tests. Property-based testing is a tool to help with that. It also encodes the expected invariants for future generations, in a more useful format than only a set of examples.

pganssle reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

pganssle
pganssle approved these changes Sep 17, 2025
View reviewed changes

@pganssle pganssle left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I think we should merge this.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Sep 17, 2025
@pganssle

pganssle commented Sep 17, 2025

Copy link
Copy Markdown
Member

I don't think we need to re-litigate the question of using property-based testing in every thread that adds some tests. I don't see any indication of a decision to keep these tests to a minimum or to give them any special treatment at all. @encukou found actual concrete issues as part of making these tests, and @Zac-HD and I both agree that these are a useful addition.

Since the only objection to this particular PR is that it adds property-based (hypothesis) tests and none of us can point to any policy against doing such a thing or any kind of conditions attached when the testing framework was first added, I'm going to go ahead and merge this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pganssle
Merge branch 'main' into hypothesis-base64
3e64a4a
@pganssle
pganssle enabled auto-merge (squash) September 17, 2025 13:19
@pganssle
pganssle merged commit 5df6a56 into python:main Sep 17, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label Sep 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@pganssle pganssle pganssle approved these changes
+1 more reviewer
@Zac-HD Zac-HD Zac-HD approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news tests Tests in the Lib/test dir

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@encukou @rhettinger @Zac-HD @pganssle
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.