From 0cfe5db6740835b35ef2afff77f5921c69777344 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20Jedlicska?= Date: Sat, 24 Sep 2022 16:22:04 +0200 Subject: [PATCH] reformat stuff, fix backwards compatible typing --- example/stream_objects.py | 5 ++- example/units_none.py | 17 ++++----- poetry.lock | 36 +++++++------------ pyproject.toml | 2 +- specklepy/api/client.py | 2 +- specklepy/api/resources/stream.py | 10 +++--- specklepy/logging/exceptions.py | 6 ++-- specklepy/objects/base.py | 12 +++---- specklepy/objects/geometry.py | 1 - specklepy/objects/structural/axis.py | 2 +- specklepy/objects/units.py | 31 ++++++++-------- .../serialization/base_object_serializer.py | 10 ++++-- tests/test_base.py | 1 + tests/test_client_and_ops.py | 2 +- tests/test_serialization.py | 4 +-- tests/test_server.py | 2 +- tests/test_transforms.py | 2 +- 17 files changed, 72 insertions(+), 73 deletions(-) diff --git a/example/stream_objects.py b/example/stream_objects.py index 4013448..6fd0375 100644 --- a/example/stream_objects.py +++ b/example/stream_objects.py @@ -5,13 +5,16 @@ from specklepy.api import operations import string import random + class Sub(Base): bar: List[str] + def random_string(): letters = string.ascii_lowercase return "".join(random.choice(letters) for _ in range(10)) + def create_object(child_count: int) -> Base: foo = Base() for i in range(child_count): @@ -33,4 +36,4 @@ if __name__ == "__main__": hash = operations.send(base=foo, transports=[transport], use_default_cache=False) rec = operations.receive(hash, transport) - print(rec) \ No newline at end of file + print(rec) diff --git a/example/units_none.py b/example/units_none.py index 26b8cdc..0e94bc7 100644 --- a/example/units_none.py +++ b/example/units_none.py @@ -1,12 +1,13 @@ from specklepy.api import operations from specklepy.objects.geometry import Base from specklepy.objects.units import Units + dct = { - 'id': '1234abcd', - 'units': None, - 'speckle_type': 'Base', - 'applicationId': None, - 'totalChildrenCount': 0, + "id": "1234abcd", + "units": None, + "speckle_type": "Base", + "applicationId": None, + "totalChildrenCount": 0, } base = Base() for prop, value in dct.items(): @@ -17,13 +18,13 @@ from devtools import debug debug(base) debug(base.units) -base.units = 'm' +base.units = "m" debug(base.units) base.units = None debug(base.units) -foo = operations.serialize(base ) +foo = operations.serialize(base) base.units = 10 @@ -31,4 +32,4 @@ debug(base.units) debug(foo) base.units = Units.mm -debug(base.units) \ No newline at end of file +debug(base.units) diff --git a/poetry.lock b/poetry.lock index 98c519a..8b7771d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -66,25 +66,26 @@ python-versions = ">=3.7,<4.0" [[package]] name = "black" -version = "20.8b1" +version = "22.8.0" description = "The uncompromising code formatter." category = "dev" optional = false -python-versions = ">=3.6" +python-versions = ">=3.6.2" [package.dependencies] -appdirs = "*" -click = ">=7.1.2" +click = ">=8.0.0" mypy-extensions = ">=0.4.3" -pathspec = ">=0.6,<1" -regex = ">=2020.1.8" -toml = ">=0.10.1" -typed-ast = ">=1.4.0" -typing-extensions = ">=3.7.4" +pathspec = ">=0.9.0" +platformdirs = ">=2" +tomli = {version = ">=1.1.0", markers = "python_full_version < \"3.11.0a7\""} +typed-ast = {version = ">=1.4.2", markers = "python_version < \"3.8\" and implementation_name == \"cpython\""} +typing-extensions = {version = ">=3.10.0.0", markers = "python_version < \"3.10\""} [package.extras] colorama = ["colorama (>=0.4.3)"] -d = ["aiohttp (>=3.3.2)", "aiohttp-cors"] +d = ["aiohttp (>=3.7.4)"] +jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"] +uvloop = ["uvloop (>=0.15.2)"] [[package]] name = "certifi" @@ -471,14 +472,6 @@ python-versions = "*" [package.dependencies] pytest = "*" -[[package]] -name = "regex" -version = "2022.6.2" -description = "Alternative regular expression module, to replace re." -category = "dev" -optional = false -python-versions = ">=3.6" - [[package]] name = "requests" version = "2.28.0" @@ -621,7 +614,7 @@ testing = ["pytest (>=6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytest- [metadata] lock-version = "1.1" python-versions = ">=3.7.2, <4.0" -content-hash = "97910d837e3b3119dc16a34160869d3ce7ac3ac26fdf4062b92d3842f0385cb3" +content-hash = "46dae0dc34c24a27efb649d4837360551f355d0a531f3f998fd53e1980be8c9e" [metadata.files] appdirs = [ @@ -642,9 +635,7 @@ attrs = [ {file = "attrs-21.4.0.tar.gz", hash = "sha256:626ba8234211db98e869df76230a137c4c40a12d72445c45d5f5b716f076e2fd"}, ] backoff = [] -black = [ - {file = "black-20.8b1.tar.gz", hash = "sha256:1c02557aa099101b9d21496f8a914e9ed2222ef70336404eeeac8edba836fbea"}, -] +black = [] certifi = [] charset-normalizer = [ {file = "charset-normalizer-2.0.12.tar.gz", hash = "sha256:2857e29ff0d34db842cd7ca3230549d1a697f96ee6d3fb071cfa6c7393832597"}, @@ -835,7 +826,6 @@ pytest-ordering = [ {file = "pytest_ordering-0.6-py2-none-any.whl", hash = "sha256:27fba3fc265f5d0f8597e7557885662c1bdc1969497cd58aff6ed21c3b617de2"}, {file = "pytest_ordering-0.6-py3-none-any.whl", hash = "sha256:3f314a178dbeb6777509548727dc69edf22d6d9a2867bf2d310ab85c403380b6"}, ] -regex = [] requests = [] requests-toolbelt = [ {file = "requests-toolbelt-0.9.1.tar.gz", hash = "sha256:968089d4584ad4ad7c171454f0a5c6dac23971e9472521ea3b6d49d610aa6fc0"}, diff --git a/pyproject.toml b/pyproject.toml index 4bceff6..4f70506 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,7 +19,7 @@ ujson = "^5.3.0" Deprecated = "^1.2.13" [tool.poetry.dev-dependencies] -black = "^20.8b1" +black = "^22.8.0" isort = "^5.7.0" pytest = "^6.2.2" pytest-ordering = "^0.6" diff --git a/specklepy/api/client.py b/specklepy/api/client.py index e29ffd9..98652b2 100644 --- a/specklepy/api/client.py +++ b/specklepy/api/client.py @@ -137,7 +137,7 @@ class SpeckleClient: "Authorization": f"Bearer {self.account.token}", "Content-Type": "application/json", "apollographql-client-name": metrics.HOST_APP, - "apollographql-client-version": metrics.HOST_APP_VERSION + "apollographql-client-version": metrics.HOST_APP_VERSION, } httptransport = RequestsHTTPTransport( url=self.graphql, headers=headers, verify=True, retries=3 diff --git a/specklepy/api/resources/stream.py b/specklepy/api/resources/stream.py index 5dc1b95..921a6ec 100644 --- a/specklepy/api/resources/stream.py +++ b/specklepy/api/resources/stream.py @@ -366,9 +366,8 @@ class Resource(ResourceBase): # we're checking for the actual version info, and if the version is 'dev' we treat it # as an up to date instance if self.server_version and ( - self.server_version == ('dev',) or - self.server_version >= (2, 6, 4) - ) : + self.server_version == ("dev",) or self.server_version >= (2, 6, 4) + ): raise UnsupportedException( ( "Server mutation `grant_permission` is no longer supported as of Speckle Server v2.6.4. " @@ -647,9 +646,8 @@ class Resource(ResourceBase): metrics.PERMISSION, self.account, {"name": "update", "role": role} ) if self.server_version and ( - self.server_version != ('dev',) and - self.server_version < (2, 6, 4) - ) : + self.server_version != ("dev",) and self.server_version < (2, 6, 4) + ): raise UnsupportedException( ( "Server mutation `update_permission` is only supported as of Speckle Server v2.6.4. " diff --git a/specklepy/logging/exceptions.py b/specklepy/logging/exceptions.py index a82e4bd..04eb452 100644 --- a/specklepy/logging/exceptions.py +++ b/specklepy/logging/exceptions.py @@ -15,9 +15,9 @@ class SpeckleInvalidUnitException(SpeckleException): def __init__(self, invalid_unit: Any) -> None: super().__init__( - message = f"Invalid units: expected type str but received {type(invalid_unit)} ({invalid_unit}).", - exception = None - ) + message=f"Invalid units: expected type str but received {type(invalid_unit)} ({invalid_unit}).", + exception=None, + ) class SerializationException(SpeckleException): diff --git a/specklepy/objects/base.py b/specklepy/objects/base.py index 5d84295..0507d0f 100644 --- a/specklepy/objects/base.py +++ b/specklepy/objects/base.py @@ -146,10 +146,10 @@ class _RegisteringBase: class Base(_RegisteringBase): - id: Optional[str] = None - totalChildrenCount: Optional[int] = None - applicationId: Optional[str] = None - _units: Units | None = None + id: Union[str, None] = None + totalChildrenCount: Union[int, None] = None + applicationId: Union[str, None] = None + _units: Union[Units, None] = None def __init__(self, **kwargs) -> None: super().__init__() @@ -313,13 +313,13 @@ class Base(_RegisteringBase): self._detachable = self._detachable.union(names) @property - def units(self) -> str | None: + def units(self) -> Union[str, None]: if self._units: return self._units.value return None @units.setter - def units(self, value: str | Units | None): + def units(self, value: Union[str, Units, None]): if value == None: units = value elif isinstance(value, Units): diff --git a/specklepy/objects/geometry.py b/specklepy/objects/geometry.py index ed5a480..664ad17 100644 --- a/specklepy/objects/geometry.py +++ b/specklepy/objects/geometry.py @@ -590,7 +590,6 @@ class BrepEdge(Base, speckle_type=GEOMETRY + "BrepEdge"): ] - class BrepLoopType(int, Enum): Unknown = 0 Outer = 1 diff --git a/specklepy/objects/structural/axis.py b/specklepy/objects/structural/axis.py index 57266bd..574fe9c 100644 --- a/specklepy/objects/structural/axis.py +++ b/specklepy/objects/structural/axis.py @@ -5,4 +5,4 @@ from ..geometry import Plane class Axis(Base, speckle_type="Objects.Structural.Geometry.Axis"): name: str = None axisType: str = None - plane: Plane = None \ No newline at end of file + plane: Plane = None diff --git a/specklepy/objects/units.py b/specklepy/objects/units.py index ad8d34b..372e7b2 100644 --- a/specklepy/objects/units.py +++ b/specklepy/objects/units.py @@ -1,22 +1,23 @@ -from typing import Literal -from warnings import warn -from specklepy.logging.exceptions import SpeckleException, SpeckleWarning, SpeckleInvalidUnitException +from typing import Literal, Union +from specklepy.logging.exceptions import SpeckleException, SpeckleInvalidUnitException from enum import Enum + class Units(Enum): mm = "mm" - cm="cm" - m="m" - km="km" - inches="in" - feet="ft" - yards="yd" - miles="mi" - none="none" + cm = "cm" + m = "m" + km = "km" + inches = "in" + feet = "ft" + yards = "yd" + miles = "mi" + none = "none" + # UNITS = ["mm", "cm", "m", "in", "ft", "yd", "mi"] -UNIT_TYPES=Literal["mm", "cm", "m", "in", "ft", "yd", "mi", "none"] +UNIT_TYPES = Literal["mm", "cm", "m", "in", "ft", "yd", "mi", "none"] UNITS_STRINGS = { Units.mm: ["mm", "mil", "millimeters", "millimetres"], @@ -64,8 +65,10 @@ def get_units_from_encoding(unit: int): ) -def get_encoding_from_units(unit: Units | None): +def get_encoding_from_units(unit: Union[Units, None]): try: return UNITS_ENCODINGS[unit] except KeyError as e: - raise SpeckleException(message=f"No encoding exists for unit {unit}. Please enter a valid unit to encode (eg {UNITS_ENCODINGS}).") from e + raise SpeckleException( + message=f"No encoding exists for unit {unit}. Please enter a valid unit to encode (eg {UNITS_ENCODINGS})." + ) from e diff --git a/specklepy/serialization/base_object_serializer.py b/specklepy/serialization/base_object_serializer.py index d38c352..d11faba 100644 --- a/specklepy/serialization/base_object_serializer.py +++ b/specklepy/serialization/base_object_serializer.py @@ -44,9 +44,13 @@ class BaseObjectSerializer: lineage: List[str] # keeps track of hash chain through the object tree family_tree: Dict[str, Dict[str, int]] closure_table: Dict[str, Dict[str, int]] - deserialized: Dict[str, Base] # holds deserialized objects so objects with same id return the same instance + deserialized: Dict[ + str, Base + ] # holds deserialized objects so objects with same id return the same instance - def __init__(self, write_transports: List[AbstractTransport] = None, read_transport=None) -> None: + def __init__( + self, write_transports: List[AbstractTransport] = None, read_transport=None + ) -> None: self.write_transports = write_transports or [] self.read_transport = read_transport self.detach_lineage = [] @@ -294,7 +298,7 @@ class BaseObjectSerializer: """ if not obj_string: return None - + self.deserialized = {} obj = safe_json_loads(obj_string) return self.recompose_base(obj=obj) diff --git a/tests/test_base.py b/tests/test_base.py index 3feb372..c6dfb10 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -100,6 +100,7 @@ def test_setting_units(): b.units = Units.cm assert b.units == Units.cm.value + def test_base_of_custom_speckle_type() -> None: b1 = Base.of_type("BirdHouse", name="Tweety's Crib") assert b1.speckle_type == "BirdHouse" diff --git a/tests/test_client_and_ops.py b/tests/test_client_and_ops.py index 1f85c46..ed47a6f 100644 --- a/tests/test_client_and_ops.py +++ b/tests/test_client_and_ops.py @@ -45,4 +45,4 @@ def test_account_from_token_and_url(): acct = get_account_from_token(token, url) assert acct.token == token - assert acct.serverInfo.url == url \ No newline at end of file + assert acct.serverInfo.url == url diff --git a/tests/test_serialization.py b/tests/test_serialization.py index 3a47bc6..8a2ba3e 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -91,7 +91,7 @@ class TestSerialization: assert deserialised == {"foo": "bar"} def test_big_int(self): - big_int = '{"big": ' + str(2 ** 64) + "}" + big_int = '{"big": ' + str(2**64) + "}" deserialised = operations.deserialize(big_int) - assert deserialised == {"big": 2 ** 64} + assert deserialised == {"big": 2**64} diff --git a/tests/test_server.py b/tests/test_server.py index 797bcd4..5e0707c 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -23,7 +23,7 @@ class TestServer: assert isinstance(version, tuple) if len(version) == 1: - assert version[0] == 'dev' + assert version[0] == "dev" else: assert isinstance(version[0], int) assert len(version) >= 3 diff --git a/tests/test_transforms.py b/tests/test_transforms.py index d85288a..e276224 100644 --- a/tests/test_transforms.py +++ b/tests/test_transforms.py @@ -129,4 +129,4 @@ def test_transform_serialisation(transform: Transform): serialized = operations.serialize(transform) deserialized = operations.deserialize(serialized) - assert transform.get_id() == deserialized.get_id() \ No newline at end of file + assert transform.get_id() == deserialized.get_id()