diff --git a/CHANGES.md b/CHANGES.md index c2c5aa0b0..3efb850ad 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,7 @@ Unreleased -- Drop support for Python 3.10. {pr}`3920` +- Drop support for Python 3.10. {pr}`3920` {pr}`3921` - A parameter name that is not a valid Python identifier, or that is a Python keyword, is deprecated and raises `TypeError` in Click 9.0. {pr}`3866` - An {class}`Option` name written as a Python identifier is deprecated when it diff --git a/src/click/_compat.py b/src/click/_compat.py index 8507a503a..0e5e7a56d 100644 --- a/src/click/_compat.py +++ b/src/click/_compat.py @@ -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__( diff --git a/src/click/_termui_impl.py b/src/click/_termui_impl.py index 99ba7f9d2..086a14dca 100644 --- a/src/click/_termui_impl.py +++ b/src/click/_termui_impl.py @@ -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 diff --git a/src/click/_utils.py b/src/click/_utils.py index 2d4e8f242..3e5a5ef28 100644 --- a/src/click/_utils.py +++ b/src/click/_utils.py @@ -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) diff --git a/src/click/core.py b/src/click/core.py index e9c2de66e..c3efc7437 100644 --- a/src/click/core.py +++ b/src/click/core.py @@ -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. diff --git a/src/click/utils.py b/src/click/utils.py index 417c89a0e..6021c96c9 100644 --- a/src/click/utils.py +++ b/src/click/utils.py @@ -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__( @@ -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__( diff --git a/tests/test_arguments.py b/tests/test_arguments.py index 2368f12f7..f22a52579 100644 --- a/tests/test_arguments.py +++ b/tests/test_arguments.py @@ -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): diff --git a/tests/test_deprecations.py b/tests/test_deprecations.py index 231dd6103..45fd6692b 100644 --- a/tests/test_deprecations.py +++ b/tests/test_deprecations.py @@ -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 @@ -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"]) diff --git a/tests/test_options.py b/tests/test_options.py index bc3cea3a4..8dd2b5382 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -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)] diff --git a/tests/test_utils/test_sentinel.py b/tests/test_utils/test_sentinel.py index 4ced0d5fa..a296bc96c 100644 --- a/tests/test_utils/test_sentinel.py +++ b/tests/test_utils/test_sentinel.py @@ -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", @@ -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