Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ tests-random = [
]
typing = [
"mypy",
"pyrefly>=1.2.0",
"pyright",
"pytest",
]
Expand Down Expand Up @@ -196,6 +197,7 @@ dependency_groups = ["typing"]
commands = [
["mypy"],
["pyright", "--ignoreexternal", "--verifytypes", "click"],
["pyrefly", "check"],
]

[tool.tox.env.docs]
Expand Down Expand Up @@ -229,3 +231,17 @@ dependency_groups = []
no_default_groups = true
skip_install = true
commands = [["uv", "lock", {replace = "posargs", default = ["-U"], extend = true}]]

[tool.pyrefly]
project-includes = [
"src",
"tests/typing",
]
# Windows-only module (guarded by `assert sys.platform == "win32"`); mypy and
# pyright both skip it as unreachable on other platforms, but pyrefly does
# not, so it is excluded here to avoid noise from missing Windows stubs.
project-excludes = [
"src/click/_winconsole.py",
]
python-version = "3.10.0"
preset = "legacy"
2 changes: 1 addition & 1 deletion src/click/termui.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ def unstyle(text: str) -> str:

def secho(
message: t.Any | None = None,
file: t.IO[t.AnyStr] | None = None,
file: t.IO[t.Any] | None = None,

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

AnyStr is a typevar, so it's not meant to be used in only a single place in a function

I've updated t.IO[t.AnyStr] to t.IO[t.Any] then

nl: bool = True,
err: bool = False,
color: bool | None = None,
Expand Down
13 changes: 11 additions & 2 deletions src/click/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@

from .core import Command

if sys.version_info >= (3, 13):
from typing import TypeIs
else:
from typing_extensions import TypeIs

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

this typing_extensions import only happens within a if TYPE_CHECKING block, so no runtime dependency is introduced


if sys.platform == "win32":
CaptureMode: t.TypeAlias = t.Literal["sys"] # pyright: ignore[reportRedeclaration]
else:
Expand Down Expand Up @@ -208,12 +213,16 @@ def mode(self) -> str:
return self._mode


def _has_read(x: object) -> TypeIs[t.IO[t.Any]]:
return hasattr(x, "read")


def make_input_stream(
input: str | bytes | t.IO[t.Any] | None, charset: str
) -> t.BinaryIO:
# Is already an input stream.
if hasattr(input, "read"):
rv = _find_binary_reader(t.cast("t.IO[t.Any]", input))
if _has_read(input):
rv = _find_binary_reader(input)
Comment on lines -215 to +225

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

using TypeIs instead of just hasattr (which doesn't give type-checkers any info) we avoid the need for casts


if rv is not None:
return rv
Expand Down
2 changes: 2 additions & 0 deletions src/click/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ def __repr__(self) -> str:
return "INT"


# pyrefly: ignore [invalid-inheritance] # https://github.com/facebook/pyrefly/issues/4842
class IntRange(_NumberRangeBase[int, int], IntParamType):
Comment on lines +733 to 734

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

it's under discussion whether pyrefly should flag this (it probably should) facebook/pyrefly#4842

for now I've just suppressed it

"""Restrict an :data:`click.INT` value to a range of accepted
values. See :ref:`ranges`.
Expand Down Expand Up @@ -762,6 +763,7 @@ def __repr__(self) -> str:
return "FLOAT"


# pyrefly: ignore [invalid-inheritance] # https://github.com/facebook/pyrefly/issues/4842
class FloatRange(_NumberRangeBase[float, float], FloatParamType):
"""Restrict a :data:`click.FLOAT` value to a range of accepted
values. See :ref:`ranges`.
Expand Down
4 changes: 2 additions & 2 deletions src/click/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def __exit__(
) -> None:
self.close_intelligently()

def __iter__(self) -> cabc.Iterator[t.AnyStr]:
def __iter__(self) -> cabc.Iterator[t.Any]:
self.open()
return iter(self._f) # type: ignore

Expand Down Expand Up @@ -245,7 +245,7 @@ def __exit__(
def __repr__(self) -> str:
return repr(self._file)

def __iter__(self) -> cabc.Iterator[t.AnyStr]:
def __iter__(self) -> cabc.Iterator[t.Any]:
return iter(self._file)


Expand Down
21 changes: 21 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading