From 5070422f8a97df5ef97aac643542653991000fd6 Mon Sep 17 00:00:00 2001 From: AllyWang Date: Wed, 19 Jul 2023 11:26:00 +0800 Subject: [PATCH 1/8] fix default value set for bool type --- knack/commands.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/knack/commands.py b/knack/commands.py index 2843f69..c55fb21 100644 --- a/knack/commands.py +++ b/knack/commands.py @@ -124,6 +124,8 @@ def update_argument(self, param_name, argtype): # that coincides with the default if isinstance(arg_default, str): arg_default = DefaultStr(arg_default) + elif isinstance(arg_default, bool): + pass elif isinstance(arg_default, int): arg_default = DefaultInt(arg_default) # update the default From a7b04c84059c7073c6c2efe05876e89a0eb8dd20 Mon Sep 17 00:00:00 2001 From: AllyWang Date: Wed, 19 Jul 2023 14:57:17 +0800 Subject: [PATCH 2/8] add default bool with is_default set --- knack/commands.py | 4 ++-- knack/validators.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/knack/commands.py b/knack/commands.py index c55fb21..222791b 100644 --- a/knack/commands.py +++ b/knack/commands.py @@ -17,7 +17,7 @@ from .events import (EVENT_CMDLOADER_LOAD_COMMAND_TABLE, EVENT_CMDLOADER_LOAD_ARGUMENTS, EVENT_COMMAND_CANCELLED) from .log import get_logger -from .validators import DefaultInt, DefaultStr +from .validators import DefaultInt, DefaultStr, DefaultBool logger = get_logger(__name__) @@ -125,7 +125,7 @@ def update_argument(self, param_name, argtype): if isinstance(arg_default, str): arg_default = DefaultStr(arg_default) elif isinstance(arg_default, bool): - pass + arg_default = DefaultBool(arg_default) elif isinstance(arg_default, int): arg_default = DefaultInt(arg_default) # update the default diff --git a/knack/validators.py b/knack/validators.py index 72fe118..8ae46cd 100644 --- a/knack/validators.py +++ b/knack/validators.py @@ -18,3 +18,14 @@ def __new__(cls, *args, **kwargs): instance = int.__new__(cls, *args, **kwargs) instance.is_default = True return instance + + +class DefaultBool: + + def __new__(cls, default_value): + instance = bool(default_value) + return instance + + def __getattr__(self, key, value): + if key == "is_default": + return True From 80ae0531bacf576db8f1a722478a8253c4e5a9d3 Mon Sep 17 00:00:00 2001 From: AllyWang Date: Wed, 19 Jul 2023 14:58:36 +0800 Subject: [PATCH 3/8] set all default values --- knack/commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/knack/commands.py b/knack/commands.py index 222791b..76db13c 100644 --- a/knack/commands.py +++ b/knack/commands.py @@ -129,7 +129,7 @@ def update_argument(self, param_name, argtype): elif isinstance(arg_default, int): arg_default = DefaultInt(arg_default) # update the default - if arg_default: + if arg_default is not None: arg.type.settings['default'] = arg_default def execute(self, **kwargs): From 089974af634d4d0b3128c7603e5011ddc6e56d2f Mon Sep 17 00:00:00 2001 From: AllyWang Date: Wed, 19 Jul 2023 17:18:04 +0800 Subject: [PATCH 4/8] skip bool default instance --- knack/commands.py | 13 ++++++++----- knack/validators.py | 11 ----------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/knack/commands.py b/knack/commands.py index 76db13c..36f086a 100644 --- a/knack/commands.py +++ b/knack/commands.py @@ -17,7 +17,7 @@ from .events import (EVENT_CMDLOADER_LOAD_COMMAND_TABLE, EVENT_CMDLOADER_LOAD_ARGUMENTS, EVENT_COMMAND_CANCELLED) from .log import get_logger -from .validators import DefaultInt, DefaultStr, DefaultBool +from .validators import DefaultInt, DefaultStr logger = get_logger(__name__) @@ -124,12 +124,15 @@ def update_argument(self, param_name, argtype): # that coincides with the default if isinstance(arg_default, str): arg_default = DefaultStr(arg_default) - elif isinstance(arg_default, bool): - arg_default = DefaultBool(arg_default) - elif isinstance(arg_default, int): + elif type(arg_default) is int: + # use type here is because: + # 1) bool is subclass of int so isinstance(True, int) cannot distinguish between int and bool + # 2) bool is not extendable according to + # https://stackoverflow.com/questions/2172189/why-i-cant-extend-bool-in-python, + # so bool's is_default is ignored for now arg_default = DefaultInt(arg_default) # update the default - if arg_default is not None: + if arg_default: arg.type.settings['default'] = arg_default def execute(self, **kwargs): diff --git a/knack/validators.py b/knack/validators.py index 8ae46cd..72fe118 100644 --- a/knack/validators.py +++ b/knack/validators.py @@ -18,14 +18,3 @@ def __new__(cls, *args, **kwargs): instance = int.__new__(cls, *args, **kwargs) instance.is_default = True return instance - - -class DefaultBool: - - def __new__(cls, default_value): - instance = bool(default_value) - return instance - - def __getattr__(self, key, value): - if key == "is_default": - return True From 376407231de71cb117c5a66da753f6e0179104fc Mon Sep 17 00:00:00 2001 From: AllyWang Date: Thu, 20 Jul 2023 09:22:32 +0800 Subject: [PATCH 5/8] add style skip --- knack/commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/knack/commands.py b/knack/commands.py index 36f086a..788c55b 100644 --- a/knack/commands.py +++ b/knack/commands.py @@ -124,7 +124,7 @@ def update_argument(self, param_name, argtype): # that coincides with the default if isinstance(arg_default, str): arg_default = DefaultStr(arg_default) - elif type(arg_default) is int: + elif type(arg_default) is int: # pylint: disable=unidiomatic-typecheck # use type here is because: # 1) bool is subclass of int so isinstance(True, int) cannot distinguish between int and bool # 2) bool is not extendable according to From 857f32b3695903059a3b5a33dcea18894a496a80 Mon Sep 17 00:00:00 2001 From: AllyWang Date: Thu, 20 Jul 2023 10:01:43 +0800 Subject: [PATCH 6/8] change funca --- knack/commands.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/knack/commands.py b/knack/commands.py index 788c55b..1d080b8 100644 --- a/knack/commands.py +++ b/knack/commands.py @@ -124,8 +124,8 @@ def update_argument(self, param_name, argtype): # that coincides with the default if isinstance(arg_default, str): arg_default = DefaultStr(arg_default) - elif type(arg_default) is int: # pylint: disable=unidiomatic-typecheck - # use type here is because: + elif isinstance(arg_default, int) and not isinstance(arg_default, bool): + # adjust here is because: # 1) bool is subclass of int so isinstance(True, int) cannot distinguish between int and bool # 2) bool is not extendable according to # https://stackoverflow.com/questions/2172189/why-i-cant-extend-bool-in-python, From b560be9046c2b81ba5f13f09a63e355cb9640748 Mon Sep 17 00:00:00 2001 From: AllyWang Date: Thu, 20 Jul 2023 10:57:30 +0800 Subject: [PATCH 7/8] use short link --- knack/commands.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/knack/commands.py b/knack/commands.py index 1d080b8..eec9e0c 100644 --- a/knack/commands.py +++ b/knack/commands.py @@ -127,9 +127,8 @@ def update_argument(self, param_name, argtype): elif isinstance(arg_default, int) and not isinstance(arg_default, bool): # adjust here is because: # 1) bool is subclass of int so isinstance(True, int) cannot distinguish between int and bool - # 2) bool is not extendable according to - # https://stackoverflow.com/questions/2172189/why-i-cant-extend-bool-in-python, - # so bool's is_default is ignored for now + # 2) bool is not extendable according to https://stackoverflow.com/a/2172204/6140424, + # so bool's is_default is ignored for now arg_default = DefaultInt(arg_default) # update the default if arg_default: From ccfe92b29e3afdfe56ac92c5b10e99c2d4fd750c Mon Sep 17 00:00:00 2001 From: AllyWang Date: Thu, 20 Jul 2023 11:03:03 +0800 Subject: [PATCH 8/8] adjust comment --- knack/commands.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/knack/commands.py b/knack/commands.py index eec9e0c..c268b69 100644 --- a/knack/commands.py +++ b/knack/commands.py @@ -125,10 +125,9 @@ def update_argument(self, param_name, argtype): if isinstance(arg_default, str): arg_default = DefaultStr(arg_default) elif isinstance(arg_default, int) and not isinstance(arg_default, bool): - # adjust here is because: - # 1) bool is subclass of int so isinstance(True, int) cannot distinguish between int and bool - # 2) bool is not extendable according to https://stackoverflow.com/a/2172204/6140424, - # so bool's is_default is ignored for now + # bool's is_default is ignored for now, because + # 1. bool is a subclass of int, so isinstance(True, int) cannot distinguish between int and bool + # 2. bool is not extendable according to https://stackoverflow.com/a/2172204/2199657 arg_default = DefaultInt(arg_default) # update the default if arg_default: