Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/click/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ def close(self, delete: bool = False) -> None:
def __getattr__(self, name: str) -> t.Any:
return getattr(self._f, name)

def __enter__(self) -> _AtomicFile:
def __enter__(self) -> t.Self:
return self

def __exit__(
Expand Down
2 changes: 1 addition & 1 deletion src/click/_termui_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def __init__(
self._is_atty = isatty(self.file)
self._last_line: str | None = None

def __enter__(self) -> ProgressBar[V]:
def __enter__(self) -> t.Self:
self.entered = True
self.render_progress()
return self
Expand Down
6 changes: 0 additions & 6 deletions src/click/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ class Sentinel(enum.Enum):
def __repr__(self) -> str:
return f"{self.__class__.__name__}.{self.name}"

def __copy__(self) -> Sentinel:
return self

def __deepcopy__(self, memo: dict[int, t.Any]) -> Sentinel:
return self

def __reduce_ex__(self, protocol: object) -> tuple[t.Any, ...]:
return getattr, (self.__class__, self.name)

Expand Down
2 changes: 1 addition & 1 deletion src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ def get_help(self) -> str:
"""
return self.command.get_help(self)

def _make_sub_context(self, command: Command) -> Context:
def _make_sub_context(self, command: Command) -> t.Self:
"""Create a new context of the same type as this context, but
for a new command.

Expand Down
4 changes: 2 additions & 2 deletions src/click/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def close_intelligently(self) -> None:
if self.should_close:
self.close()

def __enter__(self) -> _LazyFile:
def __enter__(self) -> t.Self:
return self

def __exit__(
Expand Down Expand Up @@ -215,7 +215,7 @@ def __init__(self, file: t.IO[t.Any]) -> None:
def __getattr__(self, name: str) -> t.Any:
return getattr(self._file, name)

def __enter__(self) -> _KeepOpenFile:
def __enter__(self) -> t.Self:
return self

def __exit__(
Expand Down
4 changes: 1 addition & 3 deletions tests/test_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ def test_parameter_name_is_an_identifier_or_deprecated(count, expose_value):

for decls in itertools.product(NAME_SWEEP_DECLS, repeat=count):
for cls in (click.Option, click.Argument):
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")

with warnings.catch_warnings(record=True, action="always") as caught:
try:
param = cls(list(decls), expose_value=expose_value)
except (TypeError, ValueError):
Expand Down
6 changes: 2 additions & 4 deletions tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ def human_readable_name(self) -> str:
@pytest.mark.parametrize("base", [click.Parameter, click.Option, click.Argument])
def test_parameter_subclass_without_override_is_silent(base):
"""The warning names a subclass that overrides the property, and no other."""
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
with warnings.catch_warnings(action="error", category=DeprecationWarning):

class Custom(base): # type: ignore[misc, valid-type]
pass
Expand Down Expand Up @@ -250,6 +249,5 @@ def test_normalized_explicit_name_not_deprecated(recwarn, decls):
def test_unnormalized_name_on_a_refused_option_is_silent():
"""An option that cannot be built at all warns about nothing."""
with pytest.raises(TypeError, match="No options defined"):
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
with warnings.catch_warnings(action="error", category=DeprecationWarning):
click.Option(["Foo_Bar"])
3 changes: 1 addition & 2 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -1512,8 +1512,7 @@ def cmd(**kwargs):

assert [p.name for p in cmd.params] == ["foo_bar"] * 3

with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
with warnings.catch_warnings(record=True, action="always") as caught:
result = runner.invoke(cmd, ["--help"], catch_exceptions=False)

assert not [w for w in caught if issubclass(w.category, UserWarning)]
Expand Down
11 changes: 8 additions & 3 deletions tests/test_utils/test_sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ def test_unset_sentinel():
assert value not in real_values


@pytest.mark.parametrize("sentinel", tuple(Sentinel))
def test_sentinel_value_is_a_bare_object(sentinel):
"""A sentinel's ``value`` must never look like data."""
assert type(sentinel.value) is object


@pytest.mark.parametrize("sentinel", tuple(Sentinel))
@pytest.mark.parametrize(
"duplicate",
Expand All @@ -71,9 +77,8 @@ def test_unset_sentinel():
def test_sentinel_duplication_preserves_identity(sentinel, duplicate):
"""Sentinels are singletons: copying or pickling one returns the member.

The default ``Enum`` reduction is ``(cls, (member.value,))``, which cannot
round-trip a bare ``object()`` value: the copy or the unpickled value is a
new object and ``Sentinel()`` rejects it.
Pickling by value cannot rebuild a bare ``object()``: it unpickles as a new object,
which ``Sentinel()`` rejects. ``Sentinel`` pickles its members by name.
"""
assert duplicate(sentinel) is sentinel

Expand Down
Loading