Skip to content
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

feat(Base): rework speckle_type handling to better align with Core #245

Merged
merged 1 commit into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ appdirs = "^1.4.4"
gql = {extras = ["requests", "websockets"], version = "^3.3.0"}
ujson = "^5.3.0"
Deprecated = "^1.2.13"
stringcase = "^1.2.0"

[tool.poetry.group.dev.dependencies]
black = "^22.8.0"
Expand Down
34 changes: 29 additions & 5 deletions src/specklepy/objects/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
)
from warnings import warn

from stringcase import pascalcase

from specklepy.logging.exceptions import SpeckleException
from specklepy.objects.units import Units, get_units_from_string
from specklepy.transports.memory import MemoryTransport
Expand Down Expand Up @@ -92,6 +94,7 @@ class _RegisteringBase:

speckle_type: ClassVar[str]
_speckle_type_override: ClassVar[Optional[str]] = None
_speckle_namespace: ClassVar[Optional[str]] = None
_type_registry: ClassVar[Dict[str, Type["Base"]]] = {}
_attr_types: ClassVar[Dict[str, Type]] = {}
# dict of chunkable props and their max chunk size
Expand All @@ -103,7 +106,11 @@ class _RegisteringBase:
@classmethod
def get_registered_type(cls, speckle_type: str) -> Optional[Type["Base"]]:
"""Get the registered type from the protected mapping via the `speckle_type`"""
return cls._type_registry.get(speckle_type, None)
for full_name in reversed(speckle_type.split(":")):
maybe_type = cls._type_registry.get(full_name, None)
if maybe_type:
return maybe_type
return None

@classmethod
def _determine_speckle_type(cls) -> str:
Expand All @@ -122,12 +129,29 @@ def _determine_speckle_type(cls) -> str:
return base_name

bases = [
b._speckle_type_override if b._speckle_type_override else b.__name__
b._full_name()
for b in reversed(cls.mro())
if issubclass(b, Base) and b.__name__ != base_name
]
return ":".join(bases)

@classmethod
def _full_name(cls) -> str:
base_name = "Base"
if cls.__name__ == base_name:
return base_name

if cls._speckle_type_override:
return cls._speckle_type_override

# convert the module names to PascalCase to match c# namespace naming convention
# also drop specklepy from the beginning
namespace = ".".join(
pascalcase(m)
for m in filter(lambda name: name != "specklepy", cls.__module__.split("."))
)
return f"{namespace}.{cls.__name__}"

def __init_subclass__(
cls,
speckle_type: Optional[str] = None,
Expand All @@ -145,13 +169,13 @@ def __init_subclass__(
"""
cls._speckle_type_override = speckle_type
cls.speckle_type = cls._determine_speckle_type()
if cls.speckle_type in cls._type_registry:
if cls._full_name() in cls._type_registry:
raise ValueError(
f"The speckle_type: {speckle_type} is already registered for type: "
f"{cls._type_registry[cls.speckle_type].__name__}. "
f"{cls._type_registry[cls._full_name()].__name__}. "
"Please choose a different type name."
)
cls._type_registry[cls.speckle_type] = cls # type: ignore
cls._type_registry[cls._full_name()] = cls # type: ignore
try:
cls._attr_types = get_type_hints(cls)
except Exception:
Expand Down
2 changes: 1 addition & 1 deletion src/specklepy/objects/structural/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
LoadNode,
LoadType,
)
from specklepy.objects.structural.material import (
from specklepy.objects.structural.materials import (
Concrete,
MaterialType,
Steel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class StructuralMaterial(
materialSafetyFactor: float = 0.0


class Concrete(StructuralMaterial, speckle_type=STRUCTURAL_MATERIALS + ".Concrete"):
class Concrete(StructuralMaterial):
compressiveStrength: float = 0.0
tensileStrength: float = 0.0
flexuralStrength: float = 0.0
Expand Down
2 changes: 1 addition & 1 deletion src/specklepy/objects/structural/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from specklepy.objects.base import Base
from specklepy.objects.structural.axis import Axis
from specklepy.objects.structural.material import StructuralMaterial
from specklepy.objects.structural.materials import StructuralMaterial

STRUCTURAL_PROPERTY = "Objectives.Structural.Properties"

Expand Down
18 changes: 15 additions & 3 deletions tests/test_registering_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class Baz(Bar):
"klass, speckle_type",
[
(Base, "Base"),
(Foo, "Foo"),
(Bar, "Foo:Custom.Bar"),
(Baz, "Foo:Custom.Bar:Baz"),
(Foo, "Tests.TestRegisteringBase.Foo"),
(Bar, "Tests.TestRegisteringBase.Foo:Custom.Bar"),
(Baz, "Tests.TestRegisteringBase.Foo:Custom.Bar:Tests.TestRegisteringBase.Baz"),
(
Concrete,
"Objects.Structural.Materials.StructuralMaterial:Objects.Structural.Materials.Concrete",
Expand All @@ -33,3 +33,15 @@ class Baz(Bar):
)
def test_determine_speckle_type(klass: Type[Base], speckle_type: str):
assert klass._determine_speckle_type() == speckle_type


@pytest.mark.parametrize(
"klass, fully_qualified_name",
[
(Base, "Base"),
(Foo, "Tests.TestRegisteringBase.Foo"),
(Concrete, "Objects.Structural.Materials.Concrete"),
],
)
def test_full_name(klass: Type[Base], fully_qualified_name: str):
assert klass._full_name() == fully_qualified_name
2 changes: 1 addition & 1 deletion tests/test_structural.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Restraint,
)
from specklepy.objects.structural.loading import LoadGravity
from specklepy.objects.structural.material import StructuralMaterial
from specklepy.objects.structural.materials import StructuralMaterial
from specklepy.objects.structural.properties import (
MemberType,
Property1D,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_traverse_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_traverse_value():
object_id, object_dict = serializer.traverse_base(base)
assert object_dict == {
"id": object_id,
"speckle_type": "TestBase",
"speckle_type": "Tests.TestTraverseValue.TestBase",
"applicationId": None,
"foo": [None],
"units": None,
Expand Down