Skip to content

Commit caac49c

Browse files
committed
Add _default_map_has utility
1 parent bc42caf commit caac49c

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

src/click/core.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,20 @@ def lookup_default(self, name: str, call: bool = True) -> t.Any | None:
720720

721721
return None
722722

723+
def _default_map_has(self, name: str | None) -> bool:
724+
"""Check if :attr:`default_map` contains a real value for ``name``.
725+
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+
)
736+
723737
def fail(self, message: str) -> t.NoReturn:
724738
"""Aborts the execution of the program with a specific error
725739
message.
@@ -2286,12 +2300,7 @@ def get_default(
22862300
name = self.name
22872301
value = ctx.lookup_default(name, call=False) if name is not None else None
22882302

2289-
if value is None and not (
2290-
ctx.default_map is not None
2291-
and name is not None
2292-
and name in ctx.default_map
2293-
and ctx.default_map[name] is not UNSET
2294-
):
2303+
if value is None and not ctx._default_map_has(name):
22952304
value = self.default
22962305

22972306
if call and callable(value):
@@ -2333,11 +2342,7 @@ def consume_value(
23332342

23342343
if value is UNSET:
23352344
default_map_value = ctx.lookup_default(self.name) # type: ignore[arg-type]
2336-
if default_map_value is not None or (
2337-
ctx.default_map is not None
2338-
and self.name in ctx.default_map
2339-
and ctx.default_map[self.name] is not UNSET # type: ignore[index]
2340-
):
2345+
if default_map_value is not None or ctx._default_map_has(self.name):
23412346
value = default_map_value
23422347
source = ParameterSource.DEFAULT_MAP
23432348

0 commit comments

Comments
 (0)