Skip to content

Show custom error message in prompt with hide_input=True#3256

Open
doiken wants to merge 3 commits intopallets:mainfrom
doiken:fix/hide-input-custom-error-message
Open

Show custom error message in prompt with hide_input=True#3256
doiken wants to merge 3 commits intopallets:mainfrom
doiken:fix/hide-input-custom-error-message

Conversation

@doiken
Copy link
Copy Markdown

@doiken doiken commented Mar 10, 2026

fixes #2809

prompt() with hide_input=True always showed a generic error message, even for custom types.

This change masks the input value using repr(value) matching
(consistent with built-in types' {value!r} pattern) and shows
custom messages that don't contain the input value as-is.

@Rowlando13 Rowlando13 added this to the 8.4.0 milestone Apr 29, 2026
@Rowlando13 Rowlando13 requested a review from kdeldycke April 29, 2026 06:11
@Rowlando13 Rowlando13 changed the base branch from stable to main April 29, 2026 06:52
Comment thread src/click/termui.py Outdated
@kdeldycke kdeldycke changed the title fix: show custom error message in prompt with hide_input=True fix: show custom error message in prompt with hide_input=True Apr 30, 2026
@kdeldycke kdeldycke added bug f:prompt feature: prompt for input labels Apr 30, 2026
@kdeldycke kdeldycke force-pushed the fix/hide-input-custom-error-message branch from 36a0bad to 3dac311 Compare April 30, 2026 08:12
@kdeldycke
Copy link
Copy Markdown
Collaborator

So exactly what I was thinking @AndreasBackx : this fix is way too naive. See the number of failing edge-cases I was able to add.

So now: either I try to fix it locally on top of that PR, or I can implement a better long-term solution. Maybe by modifying how UsageError manage the raw value. But I have not studied the consequences and impact yet.

@kdeldycke kdeldycke marked this pull request as draft April 30, 2026 08:25
@AndreasBackx
Copy link
Copy Markdown
Collaborator

I've got two thoughts as well. We could instead of a ValueError do a custom error like you say, but require the message to be a literal meaning you cannot interpolate the input value in it.

Or... we wrap the input value in a wrapper that has its own str and repr implementation that would obfuscate the real value. The real value would be some subfield perhaps. Though this would be a breaking change and I don't think it's very pretty.

@kdeldycke kdeldycke force-pushed the fix/hide-input-custom-error-message branch from 3dac311 to 3c2fc33 Compare May 8, 2026 14:41
@kdeldycke kdeldycke changed the title fix: show custom error message in prompt with hide_input=True Show custom error message in prompt with hide_input=True May 8, 2026
@kdeldycke kdeldycke marked this pull request as ready for review May 8, 2026 14:43
@kdeldycke kdeldycke force-pushed the fix/hide-input-custom-error-message branch from c5d2967 to 61bdc2a Compare May 8, 2026 14:47
@kdeldycke
Copy link
Copy Markdown
Collaborator

I've got two thoughts as well. We could instead of a ValueError do a custom error like you say, but require the message to be a literal meaning you cannot interpolate the input value in it.

Or... we wrap the input value in a wrapper that has its own str and repr implementation that would obfuscate the real value. The real value would be some subfield perhaps. Though this would be a breaking change and I don't think it's very pretty.

I think the subfield looks like the ultimate, realiable solution. But it would introduce too much changes in my opinion. And I am not sure if it will look clean or not. I propose to defer that exploration in another PR.

In the mean time, I implemented a solution based on regexp so the hidden heuristic is more robust and cover all cases. You can see the edge-cases I handle with this regexp in the associated unittests. Just pushed that on top of the PR.

So this PR is ready to be reviewed and merged unless someone has feedbacks.

@doiken
Copy link
Copy Markdown
Author

doiken commented May 8, 2026

Thanks so much for taking the time to push the regex-based solution and the extra test coverage!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug f:prompt feature: prompt for input

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@click.Option with 'hide_input' = True (used for password) and custom type parameter doesn't show custom error message

4 participants