Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f7ae62ade2 | |||
| 38ffbc27b7 | |||
| 8cebccf250 | |||
| 17aac0b552 | |||
| c281a329a4 | |||
| ca472716db |
@@ -252,6 +252,9 @@ class Base(_RegisteringBase):
|
|||||||
if t is None:
|
if t is None:
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
|
|
||||||
if t.__module__ == "typing":
|
if t.__module__ == "typing":
|
||||||
origin = getattr(t, "__origin__")
|
origin = getattr(t, "__origin__")
|
||||||
t = (
|
t = (
|
||||||
@@ -310,7 +313,9 @@ class Base(_RegisteringBase):
|
|||||||
|
|
||||||
@units.setter
|
@units.setter
|
||||||
def units(self, value: str):
|
def units(self, value: str):
|
||||||
self._units = get_units_from_string(value)
|
units = get_units_from_string(value)
|
||||||
|
if units:
|
||||||
|
self._units = units
|
||||||
|
|
||||||
def get_member_names(self) -> List[str]:
|
def get_member_names(self) -> List[str]:
|
||||||
"""Get all of the property names on this object, dynamic or not"""
|
"""Get all of the property names on this object, dynamic or not"""
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from specklepy.logging.exceptions import SpeckleException
|
from warnings import warn
|
||||||
|
from specklepy.logging.exceptions import SpeckleException, SpeckleWarning
|
||||||
|
|
||||||
UNITS = ["mm", "cm", "m", "in", "ft", "yd", "mi"]
|
UNITS = ["mm", "cm", "m", "in", "ft", "yd", "mi"]
|
||||||
|
|
||||||
@@ -28,6 +29,12 @@ UNITS_ENCODINGS = {
|
|||||||
|
|
||||||
|
|
||||||
def get_units_from_string(unit: str):
|
def get_units_from_string(unit: str):
|
||||||
|
if not isinstance(unit, str):
|
||||||
|
warn(
|
||||||
|
f"Invalid units: expected type str but received {type(unit)} ({unit}). Skipping - no units will be set.",
|
||||||
|
SpeckleWarning,
|
||||||
|
)
|
||||||
|
return
|
||||||
unit = str.lower(unit)
|
unit = str.lower(unit)
|
||||||
for name, alternates in UNITS_STRINGS.items():
|
for name, alternates in UNITS_STRINGS.items():
|
||||||
if unit in alternates:
|
if unit in alternates:
|
||||||
|
|||||||
@@ -60,14 +60,19 @@ class BaseObjectSerializer:
|
|||||||
chunkable = False
|
chunkable = False
|
||||||
detach = False
|
detach = False
|
||||||
|
|
||||||
# skip nulls or props marked to be ignored with "__" or "_"
|
# skip props marked to be ignored with "__" or "_"
|
||||||
if value is None or prop.startswith(("__", "_")):
|
if prop.startswith(("__", "_")):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# don't prepopulate id as this will mess up hashing
|
# don't prepopulate id as this will mess up hashing
|
||||||
if prop == "id":
|
if prop == "id":
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# allow serialisation of nulls
|
||||||
|
if value is None:
|
||||||
|
object_builder[prop] = value
|
||||||
|
continue
|
||||||
|
|
||||||
# only bother with chunking and detaching if there is a write transport
|
# only bother with chunking and detaching if there is a write transport
|
||||||
if self.write_transports:
|
if self.write_transports:
|
||||||
dynamic_chunk_match = prop.startswith("@") and re.match(
|
dynamic_chunk_match = prop.startswith("@") and re.match(
|
||||||
|
|||||||
@@ -77,6 +77,18 @@ def test_speckle_type_cannot_be_set(base: Base) -> None:
|
|||||||
assert base.speckle_type == "Base"
|
assert base.speckle_type == "Base"
|
||||||
|
|
||||||
|
|
||||||
|
def test_setting_units():
|
||||||
|
b = Base(units="foot")
|
||||||
|
assert b.units == "ft"
|
||||||
|
|
||||||
|
with pytest.raises(SpeckleException):
|
||||||
|
b.units = "big"
|
||||||
|
|
||||||
|
b.units = None # invalid args are skipped
|
||||||
|
b.units = 7
|
||||||
|
assert b.units == "ft"
|
||||||
|
|
||||||
|
|
||||||
def test_base_of_custom_speckle_type() -> None:
|
def test_base_of_custom_speckle_type() -> None:
|
||||||
b1 = Base.of_type("BirdHouse", name="Tweety's Crib")
|
b1 = Base.of_type("BirdHouse", name="Tweety's Crib")
|
||||||
assert b1.speckle_type == "BirdHouse"
|
assert b1.speckle_type == "BirdHouse"
|
||||||
|
|||||||
Reference in New Issue
Block a user