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
8 changes: 4 additions & 4 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def test_parameter_nargs_gt_1() -> None:


def test_parameter_constructor() -> None:
# no param_decl and expose_value is False: sets name to None
# no param_decl and expose_value is False: sets name to ""
arg = TyperArgument(param_decls=[], expose_value=False)
assert arg.name is None
assert arg.name == ""
assert arg.opts == []
assert arg.secondary_opts == []

Expand All @@ -77,9 +77,9 @@ def test_parameter_constructor() -> None:
with pytest.raises(ValueError, match="cannot use the same flag for true/false"):
TyperOption(param_decls=["flag", "--flag/--flag"], required=False, is_flag=True)

# inferred name is not a valid identifier: sets name to None
# inferred name is not a valid identifier: sets name to ""
unnamed_option = TyperOption(param_decls=["--123"], required=False)
assert unnamed_option.name is None
assert unnamed_option.name == ""

# no param_decl and prompt=True: raises
with pytest.raises(TypeError, match="'name' is required with 'prompt=True'."):
Expand Down
17 changes: 8 additions & 9 deletions typer/_click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ def shell_complete(self, ctx: Context, incomplete: str) -> list["CompletionItem"
or param.hidden
or (
not param.multiple
and ctx.get_parameter_source(param.name) # type: ignore
and ctx.get_parameter_source(param.name)
is ParameterSource.COMMANDLINE
)
):
Expand Down Expand Up @@ -834,7 +834,7 @@ def __init__(
]
| None = None,
) -> None:
self.name: str | None
self.name: str
self.opts: list[str]
self.secondary_opts: list[str]
self.name, self.opts, self.secondary_opts = self._parse_decls(
Expand Down Expand Up @@ -867,15 +867,14 @@ def __repr__(self) -> str:
@abstractmethod
def _parse_decls(
self, decls: Sequence[str], expose_value: bool
) -> tuple[str | None, list[str], list[str]]:
) -> tuple[str, list[str], list[str]]:
pass # pragma: no cover

@property
def human_readable_name(self) -> str:
"""Returns the human readable name of this parameter. This is the
same as the name for options, but the metavar for arguments.
"""
assert self.name is not None, "self.name should be set"
return self.name

def make_metavar(self, ctx: Context) -> str:
Expand Down Expand Up @@ -904,7 +903,7 @@ def get_default(
self, ctx: Context, call: bool = True
) -> Any | Callable[[], Any] | None:
"""Get the default for the parameter"""
value = ctx.lookup_default(self.name, call=False) # type: ignore
value = ctx.lookup_default(self.name, call=False)

if value is None:
value = self.default
Expand All @@ -921,15 +920,15 @@ def add_to_parser(self, parser: _OptionParser, ctx: Context) -> None:
def consume_value(
self, ctx: Context, opts: Mapping[str, Any]
) -> tuple[Any, ParameterSource]:
value = opts.get(self.name) # type: ignore
value = opts.get(self.name)
source = ParameterSource.COMMANDLINE

if value is None:
value = self.value_from_envvar(ctx)
source = ParameterSource.ENVIRONMENT

if value is None:
value = ctx.lookup_default(self.name) # type: ignore
value = ctx.lookup_default(self.name)
source = ParameterSource.DEFAULT_MAP

if value is None:
Expand Down Expand Up @@ -1064,7 +1063,7 @@ def handle_parse_result(
with augment_usage_errors(ctx, param=self):
value, source = self.consume_value(ctx, opts)

ctx.set_parameter_source(self.name, source) # type: ignore
ctx.set_parameter_source(self.name, source)

# Process the value through the parameter's type.
try:
Expand All @@ -1075,7 +1074,7 @@ def handle_parse_result(
value = None

if self.expose_value:
ctx.params[self.name] = value # type: ignore
ctx.params[self.name] = value

return value, args

Expand Down
2 changes: 0 additions & 2 deletions typer/_click/shell_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ def _is_incomplete_argument(ctx: Context, param: Parameter) -> bool:
if not isinstance(param, TyperArgument):
return False

assert param.name is not None
# Will be None if expose_value is False.
value = ctx.params.get(param.name)
return (
param.nargs == -1
Expand Down
19 changes: 7 additions & 12 deletions typer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ def __init__(
def human_readable_name(self) -> str:
if self.metavar is not None:
return self.metavar
assert self.name is not None, "self.name or self.metavar should be set"
return self.name.upper()

def _get_default_string(
Expand Down Expand Up @@ -403,10 +402,10 @@ def value_is_missing(self, value: Any) -> bool:

def _parse_decls(
self, decls: Sequence[str], expose_value: bool
) -> tuple[str | None, list[str], list[str]]:
) -> tuple[str, list[str], list[str]]:
if not decls:
if not expose_value:
return None, [], []
return "", [], []
raise TypeError("Argument is marked as exposed, but does not have a name.")
if len(decls) == 1:
name = arg = decls[0]
Expand Down Expand Up @@ -491,7 +490,7 @@ def __init__(
)

if prompt is True:
if self.name is None:
if not self.name:
raise TypeError("'name' is required with 'prompt=True'.")

prompt_text: str | None = self.name.replace("_", " ").capitalize()
Expand Down Expand Up @@ -542,7 +541,7 @@ def get_error_hint(self, ctx: _click.Context) -> str:

def _parse_decls(
self, decls: Sequence[str], expose_value: bool
) -> tuple[str | None, list[str], list[str]]:
) -> tuple[str, list[str], list[str]]:
opts = []
secondary_opts = []
name = None
Expand Down Expand Up @@ -579,7 +578,7 @@ def _parse_decls(
if not name.isidentifier():
name = None

return name, opts, secondary_opts
return name or "", opts, secondary_opts

def add_to_parser(self, parser: _OptionParser, ctx: _click.Context) -> None:
if self.multiple:
Expand Down Expand Up @@ -685,11 +684,7 @@ def resolve_envvar_value(self, ctx: _click.Context) -> str | None:
if rv is not None:
return rv

if (
self.allow_from_autoenv
and ctx.auto_envvar_prefix is not None
and self.name is not None
):
if self.allow_from_autoenv and ctx.auto_envvar_prefix is not None and self.name:
envvar = f"{ctx.auto_envvar_prefix}_{self.name.upper()}"
rv = os.environ.get(envvar)

Expand Down Expand Up @@ -782,7 +777,7 @@ def _write_opts(opts: Sequence[str]) -> str:
if (
self.allow_from_autoenv
and ctx.auto_envvar_prefix is not None
and self.name is not None
and self.name
):
envvar = f"{ctx.auto_envvar_prefix}_{self.name.upper()}"

Expand Down
2 changes: 1 addition & 1 deletion typer/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def _get_parameter_help(
if (
getattr(param, "allow_from_autoenv", None)
and getattr(ctx, "auto_envvar_prefix", None) is not None
and param.name is not None
and param.name
):
envvar = f"{ctx.auto_envvar_prefix}_{param.name.upper()}"
if envvar is not None:
Expand Down
Loading