From 7d68a3dfc0cce8bbb815c6957216b0d1237f9d93 Mon Sep 17 00:00:00 2001 From: syncblaze Date: Sat, 17 Aug 2024 19:54:16 +0200 Subject: [PATCH 1/6] adding None checks --- hikari/internal/data_binding.py | 43 +++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/hikari/internal/data_binding.py b/hikari/internal/data_binding.py index 923514dcb..2ecf94607 100644 --- a/hikari/internal/data_binding.py +++ b/hikari/internal/data_binding.py @@ -296,7 +296,8 @@ def put( value The JSON type to put. This may be a non-JSON type if a conversion is also specified. This may alternatively be undefined. In the latter - case, nothing is performed. + case, nothing is performed. This may also be [`None`][], in which case + the value isn't cast and is stored as-is. conversion The optional conversion to apply. """ @@ -309,13 +310,13 @@ def put( self[key] = conversion(value) @typing.overload - def put_array(self, key: str, values: undefined.UndefinedOr[typing.Iterable[JSONish]], /) -> None: ... + def put_array(self, key: str, values: undefined.UndefinedNoneOr[typing.Iterable[JSONish]], /) -> None: ... @typing.overload def put_array( self, key: str, - values: undefined.UndefinedOr[typing.Iterable[T_co]], + values: undefined.UndefinedNoneOr[typing.Iterable[T_co]], /, *, conversion: typing.Callable[[T_co], JSONish], @@ -324,7 +325,7 @@ def put_array( def put_array( self, key: str, - values: undefined.UndefinedOr[typing.Iterable[typing.Any]], + values: undefined.UndefinedNoneOr[typing.Iterable[typing.Any]], /, *, conversion: typing.Optional[typing.Callable[[typing.Any], JSONish]] = None, @@ -342,15 +343,22 @@ def put_array( values The JSON types to put. This may be an iterable of non-JSON types if a conversion is also specified. This may alternatively be undefined. - In the latter case, nothing is performed. + In the latter case, nothing is performed. This may also be [`None`][], + in which case the value isn't cast and is stored as-is. conversion The optional conversion to apply. """ - if values is not undefined.UNDEFINED: + if values is undefined.UNDEFINED: + return + + if values is not None: if conversion is not None: self[key] = [conversion(value) for value in values] else: self[key] = list(values) + else: + self[key] = values + def put_snowflake( self, key: str, value: undefined.UndefinedNoneOr[snowflakes.SnowflakeishOr[snowflakes.Unique]], / @@ -366,15 +374,18 @@ def put_snowflake( value The JSON type to put. This may alternatively be undefined, in this case, nothing is performed. This may also be [`None`][], in this - case the value isn't cast. + case the value isn't cast and is stored as-is. """ - if value is not undefined.UNDEFINED and value is not None: + if value is undefined.UNDEFINED: + return + + if value is not None: self[key] = str(int(value)) - elif value is None: - self[key] = None + else: + self[key] = value def put_snowflake_array( - self, key: str, values: undefined.UndefinedOr[typing.Iterable[snowflakes.SnowflakeishOr[snowflakes.Unique]]], / + self, key: str, values: undefined.UndefinedNoneOr[typing.Iterable[snowflakes.SnowflakeishOr[snowflakes.Unique]]], / ) -> None: """Put an array of snowflakes with the given key into this builder. @@ -388,10 +399,16 @@ def put_snowflake_array( The key to give the element. values The JSON snowflakes to put. This may alternatively be undefined. - In the latter case, nothing is performed. + In the latter case, nothing is performed. This may also be [`None`][], + in which case the value isn't cast and is stored as-is. """ - if values is not undefined.UNDEFINED: + if values is undefined.UNDEFINED: + return + + if values is not None: self[key] = [str(int(value)) for value in values] + else: + self[key] = values def cast_variants_array(cast: typing.Callable[[T_co], T], raw_values: typing.Iterable[T_co], /) -> list[T]: From 8bb2c7870b72fb402c0178b8728b6068c5f58a99 Mon Sep 17 00:00:00 2001 From: syncblaze Date: Sat, 17 Aug 2024 20:01:25 +0200 Subject: [PATCH 2/6] forgot to run nox --- hikari/internal/data_binding.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/hikari/internal/data_binding.py b/hikari/internal/data_binding.py index 2ecf94607..d3795fa66 100644 --- a/hikari/internal/data_binding.py +++ b/hikari/internal/data_binding.py @@ -359,7 +359,6 @@ def put_array( else: self[key] = values - def put_snowflake( self, key: str, value: undefined.UndefinedNoneOr[snowflakes.SnowflakeishOr[snowflakes.Unique]], / ) -> None: @@ -385,7 +384,10 @@ def put_snowflake( self[key] = value def put_snowflake_array( - self, key: str, values: undefined.UndefinedNoneOr[typing.Iterable[snowflakes.SnowflakeishOr[snowflakes.Unique]]], / + self, + key: str, + values: undefined.UndefinedNoneOr[typing.Iterable[snowflakes.SnowflakeishOr[snowflakes.Unique]]], + /, ) -> None: """Put an array of snowflakes with the given key into this builder. From 8d5c26bfa5c0a5da9504ddc58af64973b421fe81 Mon Sep 17 00:00:00 2001 From: syncblaze Date: Sat, 17 Aug 2024 20:29:37 +0200 Subject: [PATCH 3/6] add changelog --- changes/2021.feature.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/2021.feature.md diff --git a/changes/2021.feature.md b/changes/2021.feature.md new file mode 100644 index 000000000..df4ef7b30 --- /dev/null +++ b/changes/2021.feature.md @@ -0,0 +1 @@ +Add `None` check for every put function in `JSONObjectBuilder` From 24997ef24bc1365a4d0415ed25281688d73a565a Mon Sep 17 00:00:00 2001 From: Blaze <88249929+syncblaze@users.noreply.github.com> Date: Wed, 28 Aug 2024 13:28:25 +0200 Subject: [PATCH 4/6] Update hikari/internal/data_binding.py Co-authored-by: beagold <86345081+beagold@users.noreply.github.com> Signed-off-by: Blaze <88249929+syncblaze@users.noreply.github.com> --- hikari/internal/data_binding.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hikari/internal/data_binding.py b/hikari/internal/data_binding.py index d3795fa66..cc76ae48d 100644 --- a/hikari/internal/data_binding.py +++ b/hikari/internal/data_binding.py @@ -357,7 +357,7 @@ def put_array( else: self[key] = list(values) else: - self[key] = values + self[key] = None def put_snowflake( self, key: str, value: undefined.UndefinedNoneOr[snowflakes.SnowflakeishOr[snowflakes.Unique]], / From 152b44e14c0e0a83063899a346bdace530cb42c8 Mon Sep 17 00:00:00 2001 From: Blaze <88249929+syncblaze@users.noreply.github.com> Date: Wed, 28 Aug 2024 13:28:30 +0200 Subject: [PATCH 5/6] Update hikari/internal/data_binding.py Co-authored-by: beagold <86345081+beagold@users.noreply.github.com> Signed-off-by: Blaze <88249929+syncblaze@users.noreply.github.com> --- hikari/internal/data_binding.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hikari/internal/data_binding.py b/hikari/internal/data_binding.py index cc76ae48d..7a920e944 100644 --- a/hikari/internal/data_binding.py +++ b/hikari/internal/data_binding.py @@ -381,7 +381,7 @@ def put_snowflake( if value is not None: self[key] = str(int(value)) else: - self[key] = value + self[key] = None def put_snowflake_array( self, From 69b1722726ab98e524dffd0f994315bee0dfc41c Mon Sep 17 00:00:00 2001 From: Blaze <88249929+syncblaze@users.noreply.github.com> Date: Wed, 28 Aug 2024 13:28:35 +0200 Subject: [PATCH 6/6] Update hikari/internal/data_binding.py Co-authored-by: beagold <86345081+beagold@users.noreply.github.com> Signed-off-by: Blaze <88249929+syncblaze@users.noreply.github.com> --- hikari/internal/data_binding.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hikari/internal/data_binding.py b/hikari/internal/data_binding.py index 7a920e944..f41d304ad 100644 --- a/hikari/internal/data_binding.py +++ b/hikari/internal/data_binding.py @@ -410,7 +410,7 @@ def put_snowflake_array( if values is not None: self[key] = [str(int(value)) for value in values] else: - self[key] = values + self[key] = None def cast_variants_array(cast: typing.Callable[[T_co], T], raw_values: typing.Iterable[T_co], /) -> list[T]: