-
Notifications
You must be signed in to change notification settings - Fork 78
[HZ-5510] Introduced number types and removed the default_int_type config #837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import asyncio | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A few small things in this example:
|
||
|
|
||
| from hazelcast.asyncio import HazelcastClient | ||
| from hazelcast import Int32 | ||
|
|
||
|
|
||
| async def amain(): | ||
| client = await HazelcastClient.create_and_start() | ||
| map = await client.get_map("number_test") | ||
| await map.set("i8", Int32(10)) | ||
| value_i8 = await map.get("i8") | ||
| assert type(value_i8) == int | ||
|
|
||
|
|
||
| asyncio.run(amain()) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| from typing import Self | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| from hazelcast.serialization import MIN_SHORT, MAX_SHORT, MIN_INT, MAX_INT, MIN_LONG, MAX_LONG | ||
| from hazelcast.serialization.bits import MIN_BYTE, MAX_BYTE | ||
|
|
||
| __all__ = "Int8", "Int16", "Int32", "Int64", "Float32", "Float64", "BigInt" | ||
|
|
||
|
|
||
| class Int8: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These types have no Int32(1) == Int32(1) # False
Int32(1) == 1 # FalseThis also affects A simpler fix for the whole file: let Dispatch keeps working, because |
||
| """Int8 represents an 8-bit signed integer | ||
|
|
||
| Corresponds to Java ``byte`` | ||
| """ | ||
|
|
||
| MIN_VALUE = MIN_BYTE | ||
| MAX_VALUE = MAX_BYTE | ||
|
|
||
| def __init__(self, value: int): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The range is checked but the type is not, so a wrong value is only caught much later: Int8(1.5) # accepted here, fails in to_data with "__int__ returned non-int (type float)"
Int8("3") # raises TypeError from the comparison, not the intended ValueErrorAn |
||
| if not (self.MIN_VALUE <= value <= self.MAX_VALUE): | ||
| raise ValueError( | ||
| "{} value must be between {} and {}".format( | ||
| self.__class__.__name__, | ||
| self.MIN_VALUE, | ||
| self.MAX_VALUE, | ||
| ) | ||
| ) | ||
| self.value = value | ||
|
|
||
| def __int__(self): | ||
| return self.value | ||
|
|
||
| def __repr__(self) -> str: | ||
| return str(self.value) | ||
|
|
||
|
|
||
| class Int16: | ||
| """Int16 represents a 16-bit signed integer | ||
|
|
||
| Corresponds to Java ``short``. | ||
| """ | ||
|
|
||
| MIN_VALUE = MIN_SHORT | ||
| MAX_VALUE = MAX_SHORT | ||
|
|
||
| def __init__(self, value: int): | ||
| if not (self.MIN_VALUE <= value <= self.MAX_VALUE): | ||
| raise ValueError( | ||
| "{} value must be between {} and {}".format( | ||
| self.__class__.__name__, | ||
| self.MIN_VALUE, | ||
| self.MAX_VALUE, | ||
| ) | ||
| ) | ||
| self.value = value | ||
|
|
||
| def __int__(self): | ||
| return self.value | ||
|
|
||
| def __repr__(self) -> str: | ||
| return str(self.value) | ||
|
|
||
|
|
||
| class Int32: | ||
| """Int32 represents a 32-bit signed integer | ||
|
|
||
| Corresponds to Java ``int``. | ||
| """ | ||
|
|
||
| MIN_VALUE = MIN_INT | ||
| MAX_VALUE = MAX_INT | ||
|
|
||
| def __init__(self, value: int): | ||
| if not (self.MIN_VALUE <= value <= self.MAX_VALUE): | ||
| raise ValueError( | ||
| "{} value must be between {} and {}".format( | ||
| self.__class__.__name__, | ||
| self.MIN_VALUE, | ||
| self.MAX_VALUE, | ||
| ) | ||
| ) | ||
| self.value = value | ||
|
|
||
| def __int__(self): | ||
| return self.value | ||
|
|
||
| def __repr__(self) -> str: | ||
| return str(self.value) | ||
|
|
||
|
|
||
| class Int64: | ||
| """Int64 represents a 64-bit signed integer | ||
|
|
||
| Corresponds to Java ``long``. | ||
| """ | ||
|
|
||
| MIN_VALUE = MIN_LONG | ||
| MAX_VALUE = MAX_LONG | ||
|
|
||
| def __init__(self, value: int): | ||
| if not (self.MIN_VALUE <= value <= self.MAX_VALUE): | ||
| raise ValueError( | ||
| "{} value must be between {} and {}".format( | ||
| self.__class__.__name__, | ||
| self.MIN_VALUE, | ||
| self.MAX_VALUE, | ||
| ) | ||
| ) | ||
| self.value = value | ||
|
|
||
| def __int__(self): | ||
| return self.value | ||
|
|
||
| def __repr__(self) -> str: | ||
| return str(self.value) | ||
|
|
||
|
|
||
| class BigInt: | ||
| """BigInt represents a big integer | ||
|
|
||
| Corresponds to Java ``java.math.BigInteger``. | ||
| """ | ||
|
|
||
| def __init__(self, value: int): | ||
| self.value = value | ||
|
|
||
| def __int__(self): | ||
| return self.value | ||
|
|
||
| def __repr__(self) -> str: | ||
| return str(self.value) | ||
|
|
||
|
|
||
| class Float32: | ||
| """Float32 represents a 32-bit floating point number | ||
|
|
||
| Corresponds to Java ``float``. | ||
| """ | ||
|
|
||
| def __init__(self, value: float | int): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No range check here, unlike the A range check would be good. It may also be worth a docstring note that precision is lost: |
||
| self.value = float(value) | ||
|
|
||
| def __float__(self): | ||
| return self.value | ||
|
|
||
| def __repr__(self) -> str: | ||
| return str(self.value) | ||
|
|
||
|
|
||
| class Float64: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should it be named to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| """Float32 represents a 64-bit floating point number | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: should be |
||
|
|
||
| Corresponds to Java ``double``. | ||
| """ | ||
|
|
||
| def __init__(self, value: float | int): | ||
| self.value = float(value) | ||
|
|
||
| def __float__(self): | ||
| return self.value | ||
|
|
||
| def __repr__(self) -> str: | ||
| return str(self.value) | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -42,7 +42,7 @@ def read(self, inp): | |||||
| return inp.read_byte() | ||||||
|
|
||||||
| def write(self, out, obj): | ||||||
| out.write_byte(obj) | ||||||
| out.write_byte(int(obj)) | ||||||
|
|
||||||
| def get_type_id(self): | ||||||
| return CONSTANT_TYPE_BYTE | ||||||
|
|
@@ -63,7 +63,7 @@ def read(self, inp): | |||||
| return inp.read_short() | ||||||
|
|
||||||
| def write(self, out, obj): | ||||||
| out.write_short(obj) | ||||||
| out.write_short(int(obj)) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
and similar for the other int changes below.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would be incorrect. We must convert |
||||||
|
|
||||||
| def get_type_id(self): | ||||||
| return CONSTANT_TYPE_SHORT | ||||||
|
|
@@ -74,7 +74,7 @@ def read(self, inp): | |||||
| return inp.read_int() | ||||||
|
|
||||||
| def write(self, out, obj): | ||||||
| out.write_int(obj) | ||||||
| out.write_int(int(obj)) | ||||||
|
|
||||||
| def get_type_id(self): | ||||||
| return CONSTANT_TYPE_INTEGER | ||||||
|
|
@@ -85,7 +85,7 @@ def read(self, inp): | |||||
| return inp.read_long() | ||||||
|
|
||||||
| def write(self, out, obj): | ||||||
| out.write_long(obj) | ||||||
| out.write_long(int(obj)) | ||||||
|
|
||||||
| def get_type_id(self): | ||||||
| return CONSTANT_TYPE_LONG | ||||||
|
|
@@ -95,7 +95,8 @@ class FloatSerializer(BaseSerializer): | |||||
| def read(self, inp): | ||||||
| return inp.read_float() | ||||||
|
|
||||||
| # "write(self, out, obj)" is never called so not implemented here | ||||||
| def write(self, out, obj): | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Removing |
||||||
| out.write_float(float(obj)) | ||||||
|
|
||||||
| def get_type_id(self): | ||||||
| return CONSTANT_TYPE_FLOAT | ||||||
|
|
@@ -106,7 +107,7 @@ def read(self, inp): | |||||
| return inp.read_double() | ||||||
|
|
||||||
| def write(self, out, obj): | ||||||
| out.write_double(obj) | ||||||
| out.write_double(float(obj)) | ||||||
|
|
||||||
| def get_type_id(self): | ||||||
| return CONSTANT_TYPE_DOUBLE | ||||||
|
|
@@ -247,7 +248,7 @@ def read(self, inp): | |||||
| return IOUtil.read_big_integer(inp) | ||||||
|
|
||||||
| def write(self, out, obj): | ||||||
| IOUtil.write_big_integer(out, obj) | ||||||
| IOUtil.write_big_integer(out, int(obj)) | ||||||
|
|
||||||
| def get_type_id(self): | ||||||
| return JAVA_DEFAULT_TYPE_BIG_INTEGER | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IntTypeis removed here, but nothing is added for the new types, anddocs/serialization.rststill describes the old behaviour: line 23 mapsintto Byte/Short/Integer/Long/BigInteger, and line 37 tells users to configure this withdefault_int_type. That argument is removed by this PR, so following the docs now raisesInvalidConfigurationError: Unrecognized config option: default_int_type.Could
Int8..BigIntget an entry here and a short section inserialization.rst?