You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When running a script without type annotations, but a default boolean value, I sort-of expected the type to be inferred from the default. That's how autocommand works. If the default value is a boolean and no type annotation is given, the type is inferred to be bool. But in Typer, the type presumably defaults to str, so when running the above script with no parameters, do_bad_thing is set to 'False', which is True, and a NotImplementedError is raised.
Obviously, this unexpected behavior can be fixed by always supplying type annotations, but since the issue happens silently, it's a bit of a foot gun.
Better would be to either use the default value to infer that the type is a bool or warn or raise an Exception if the default value is a bool but no type annotation was given. It's unlikely if the user is supplying a default value of True or False that they want that cast to a string.
I note also that this behavior differs from how arg = None is handled, which apparently does not get cast to a string; it seems to be handled as str | None based on the default of None.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
First Check
Commit to Help
Example Code
Description
When running a script without type annotations, but a default boolean value, I sort-of expected the type to be inferred from the default. That's how autocommand works. If the default value is a boolean and no type annotation is given, the type is inferred to be
bool
. But in Typer, the type presumably defaults tostr
, so when running the above script with no parameters,do_bad_thing
is set to'False'
, which isTrue
, and aNotImplementedError
is raised.Obviously, this unexpected behavior can be fixed by always supplying type annotations, but since the issue happens silently, it's a bit of a foot gun.
Better would be to either use the default value to infer that the type is a bool or warn or raise an Exception if the default value is a bool but no type annotation was given. It's unlikely if the user is supplying a default value of
True
orFalse
that they want that cast to a string.I note also that this behavior differs from how
arg = None
is handled, which apparently does not get cast to a string; it seems to be handled asstr | None
based on the default ofNone
.This issue came up in jaraco.develop, where after migrating to Typer in jaraco/jaraco.develop#21, I ran the update-projects script, but it failed when
dry_run
was set to'False'
(True
).Operating System
macOS
Operating System Details
n/a
Typer Version
0.12.4
Python Version
3.13.0rc1
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions