Skip to content

platform-dependent testing.CaptureMode type alias#3452

Open
jorenham wants to merge 3 commits into
pallets:stablefrom
jorenham:typing/testing/platform-dependent-CaptureMode
Open

platform-dependent testing.CaptureMode type alias#3452
jorenham wants to merge 3 commits into
pallets:stablefrom
jorenham:typing/testing/platform-dependent-CaptureMode

Conversation

@jorenham
Copy link
Copy Markdown
Contributor

CliRunner.__init__ will raise a ValueError if capture="fd" on windows. By using the fact that type-checkers are able to understand basic sys.platform checks (https://typing.python.org/en/latest/spec/directives.html#version-and-platform-checking), we can use Python's type system to prevent this from happening, by having a definition for the CaptureMode type alias on windows, improving type-safety.

@kdeldycke kdeldycke changed the base branch from main to stable May 22, 2026 12:14
@kdeldycke kdeldycke added this to the 8.4.2 milestone May 22, 2026
Copy link
Copy Markdown
Collaborator

@AndreasBackx AndreasBackx left a comment

Choose a reason for hiding this comment

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

As other PRs: main instead of stable and no version change?

Comment thread src/click/testing.py
Comment on lines +26 to +29
if sys.platform == "win32":
CaptureMode: t.TypeAlias = t.Literal["sys"] # pyright: ignore[reportRedeclaration]
else:
CaptureMode: t.TypeAlias = t.Literal["sys", "fd"] # pyright: ignore[reportRedeclaration]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I wonder how you would make pyright happy here... 🤔️ Anyhow, this should still be fine I believe.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants