Skip to content

Commit 1df07fd

Browse files
committed
Make _default_map_has the single source of truth
1 parent 4c459c3 commit 1df07fd

1 file changed

Lines changed: 22 additions & 25 deletions

File tree

src/click/core.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,20 @@ def ensure_object(self, object_type: type[V]) -> V:
685685
self.obj = rv = object_type()
686686
return rv
687687

688+
def _default_map_has(self, name: str | None) -> bool:
689+
"""Check if :attr:`default_map` contains a real value for ``name``.
690+
691+
Returns ``False`` when the key is absent, the map is ``None``,
692+
``name`` is ``None``, or the stored value is the internal
693+
:data:`UNSET` sentinel.
694+
"""
695+
return (
696+
name is not None
697+
and self.default_map is not None
698+
and name in self.default_map
699+
and self.default_map[name] is not UNSET
700+
)
701+
688702
@t.overload
689703
def lookup_default(
690704
self, name: str, call: t.Literal[True] = True
@@ -705,34 +719,17 @@ def lookup_default(self, name: str, call: bool = True) -> t.Any | None:
705719
.. versionchanged:: 8.0
706720
Added the ``call`` parameter.
707721
"""
708-
if self.default_map is not None:
709-
value = self.default_map.get(name)
710-
711-
# Hide the UNSET sentinel from the caller, as it is an
712-
# implementation detail. Treat it the same as "key not found".
713-
if value is UNSET:
714-
return None
715-
716-
if call and callable(value):
717-
return value()
718-
719-
return value
722+
if not self._default_map_has(name):
723+
return None
720724

721-
return None
725+
# Assert to make the type checker happy.
726+
assert self.default_map is not None
727+
value = self.default_map[name]
722728

723-
def _default_map_has(self, name: str | None) -> bool:
724-
"""Check if :attr:`default_map` contains a real value for ``name``.
729+
if call and callable(value):
730+
return value()
725731

726-
Returns ``False`` when the key is absent, the map is ``None``,
727-
``name`` is ``None``, or the stored value is the internal
728-
:data:`UNSET` sentinel.
729-
"""
730-
return (
731-
name is not None
732-
and self.default_map is not None
733-
and name in self.default_map
734-
and self.default_map[name] is not UNSET
735-
)
732+
return value
736733

737734
def fail(self, message: str) -> t.NoReturn:
738735
"""Aborts the execution of the program with a specific error

0 commit comments

Comments
 (0)