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` diff --git a/hikari/internal/data_binding.py b/hikari/internal/data_binding.py index 923514dcb..f41d304ad 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,21 @@ 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] = None def put_snowflake( self, key: str, value: undefined.UndefinedNoneOr[snowflakes.SnowflakeishOr[snowflakes.Unique]], / @@ -366,15 +373,21 @@ 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: + else: self[key] = None 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 +401,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] = None def cast_variants_array(cast: typing.Callable[[T_co], T], raw_values: typing.Iterable[T_co], /) -> list[T]: