Skip to content

Commit

Permalink
Merge pull request #303 from pcattori/dataclasses
Browse files Browse the repository at this point in the history
POC for dataclasses
  • Loading branch information
pcattori committed Nov 1, 2019
2 parents 93fe768 + 9f2ed39 commit 6976694
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 127 deletions.
44 changes: 0 additions & 44 deletions .github/PULL_REQUEST_TEMPLATE/BUG_FIX.md

This file was deleted.

41 changes: 0 additions & 41 deletions .github/PULL_REQUEST_TEMPLATE/NEW_FEATURE.md

This file was deleted.

3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## 0.10.0-dev
**BREAKING CHANGES**
- [#309](https://github.com/Datatamer/tamr-client/issues/309) Migrate `SubAttribute` to use `@dataclass(frozen=True)`. `SubAttribute.__init__` constructor replaced with the one generated by `@dataclass`. `SubAttribute`s should be constructed via the `SubAttribute.from_json` static method.

**BUG FIXES**
- [#293](https://github.com/Datatamer/tamr-client/issues/293) Better handling for HTTP 204 on already up-to-date operations

Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
extensions = [
"recommonmark",
"sphinx.ext.napoleon",
"sphinx_autodoc_typehints",
"sphinx.ext.intersphinx",
"sphinx.ext.viewcode",
]
Expand Down
3 changes: 2 additions & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# TODO(pcattori) Delete this file once RTD fully supports poetry
recommonmark==0.6.0
Sphinx==2.1.0
sphinx_rtd_theme==0.4.3
sphinx-autodoc-typehints==1.8.0
Sphinx==2.1.0
toml==0.10.0
.
23 changes: 22 additions & 1 deletion poetry.lock

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

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ classifiers = [
python = "^3.6"
requests = "^2.22"
simplejson = "^3.16"
dataclasses = "^0.6.0"

[tool.poetry.dev-dependencies]
Sphinx = "^2.1"
Expand All @@ -36,6 +37,7 @@ toml = "^0.10.0"
sphinx_rtd_theme = "^0.4.3"
pandas = "^0.25.0"
recommonmark = "^0.6.0"
sphinx-autodoc-typehints = "^1.8"

[build-system]
requires = ["poetry>=0.12"]
Expand Down
76 changes: 39 additions & 37 deletions tamr_unify_client/attribute/subattribute.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
from copy import deepcopy
from dataclasses import dataclass, field
from typing import Any, Dict, Optional

from tamr_unify_client.attribute.type import AttributeType

SubAttributeJson = Dict[str, Any]


@dataclass(frozen=True)
class SubAttribute:
"""
An attribute which is itself a property of another attribute.
"""An attribute which is itself a property of another attribute.
See https://docs.tamr.com/reference#attribute-types
:param data: JSON data representing this attribute
:type data: :py:class:`dict`
Args:
name: Name of sub-attribute
description: Description of sub-attribute
type: See https://docs.tamr.com/reference#attribute-types
is_nullable: If this sub-attribute can be null
"""

def __init__(self, data):
self._data = data

@property
def name(self):
""":type: str"""
return self._data.get("name")

@property
def description(self):
""":type: str"""
return self._data.get("description")

@property
def type(self):
""":type: :class:`~tamr_unify_client.attribute.type.AttributeType`"""
# import locally to avoid circular dependency
from tamr_unify_client.attribute.type import AttributeType

type_json = self._data.get("type")
return AttributeType(type_json)

@property
def is_nullable(self):
""":type: bool"""
return self._data.get("isNullable")

def __repr__(self):
return (
f"{self.__class__.__module__}."
f"{self.__class__.__qualname__}("
f"name={self.name!r})"
)
name: str
type: AttributeType
is_nullable: bool
_json: SubAttributeJson = field(repr=False)
description: Optional[str] = None

@staticmethod
def from_json(data: SubAttributeJson) -> "SubAttribute":
"""Create a SubAttribute from JSON data.
Args:
data: JSON data received from Tamr server.
"""
_json = deepcopy(data)

dc = deepcopy(data)
dc["is_nullable"] = dc.pop("isNullable")

type_json = dc.pop("type")
# TODO implement AttributeType.from_json and use that instead
type = AttributeType(type_json)

return SubAttribute(**dc, type=type, _json=_json)
6 changes: 3 additions & 3 deletions tamr_unify_client/attribute/type.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from copy import deepcopy

from tamr_unify_client.attribute.subattribute import SubAttribute


class AttributeType:
"""
Expand Down Expand Up @@ -32,8 +30,10 @@ def inner_type(self):
@property
def attributes(self):
""":type: list[:class:`~tamr_unify_client.attribute.subattribute.SubAttribute`]"""
from tamr_unify_client.attribute.subattribute import SubAttribute

collection_json = self._data.get("attributes")
return [SubAttribute(attr) for attr in collection_json]
return [SubAttribute.from_json(attr) for attr in collection_json]

def spec(self):
"""Returns a spec representation of this attribute type.
Expand Down

0 comments on commit 6976694

Please sign in to comment.