Skip to content

Commit cab859b

Browse files
committed
fix: Make name a kwarg for Entity
Signed-off-by: Achal Shah <[email protected]>
1 parent 32e925e commit cab859b

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

sdk/python/feast/entity.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import warnings
1415
from datetime import datetime
1516
from typing import Dict, Optional
1617

@@ -53,17 +54,35 @@ class Entity:
5354
@log_exceptions
5455
def __init__(
5556
self,
56-
name: str,
57+
*args,
58+
name: Optional[str] = None,
5759
value_type: ValueType = ValueType.UNKNOWN,
5860
description: str = "",
5961
join_key: Optional[str] = None,
6062
tags: Optional[Dict[str, str]] = None,
6163
owner: str = "",
6264
):
6365
"""Creates an Entity object."""
64-
self.name = name
66+
if len(args) == 1:
67+
warnings.warn(
68+
(
69+
"Entity name should be specified as a keyword argument instead of a positional arg."
70+
"Feast 0.23+ will not support positional arguments to construct Entities"
71+
),
72+
DeprecationWarning,
73+
)
74+
if len(args) > 1:
75+
raise ValueError(
76+
"All arguments to construct an entity should be specified as keyword arguments only"
77+
)
78+
79+
self.name = args[0] if len(args) > 0 else name
80+
81+
if not self.name:
82+
raise ValueError("Name needs to be specified")
83+
6584
self.value_type = value_type
66-
self.join_key = join_key if join_key else name
85+
self.join_key = join_key if join_key else self.name
6786
self.description = description
6887
self.tags = tags if tags is not None else {}
6988
self.owner = owner

sdk/python/tests/unit/test_entity.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,57 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import assertpy
15+
import pytest
1416

1517
from feast.entity import Entity
1618
from feast.value_type import ValueType
1719

1820

1921
def test_join_key_default():
20-
entity = Entity("my-entity", description="My entity", value_type=ValueType.STRING)
22+
with pytest.deprecated_call():
23+
entity = Entity(
24+
"my-entity", description="My entity", value_type=ValueType.STRING
25+
)
2126
assert entity.join_key == "my-entity"
2227

2328

2429
def test_entity_class_contains_tags():
25-
entity = Entity(
26-
"my-entity",
27-
description="My entity",
28-
value_type=ValueType.STRING,
29-
tags={"key1": "val1", "key2": "val2"},
30-
)
30+
with pytest.deprecated_call():
31+
entity = Entity(
32+
"my-entity",
33+
description="My entity",
34+
value_type=ValueType.STRING,
35+
tags={"key1": "val1", "key2": "val2"},
36+
)
3137
assert "key1" in entity.tags.keys() and entity.tags["key1"] == "val1"
3238
assert "key2" in entity.tags.keys() and entity.tags["key2"] == "val2"
3339

3440

3541
def test_entity_without_tags_empty_dict():
36-
entity = Entity("my-entity", description="My entity", value_type=ValueType.STRING)
42+
with pytest.deprecated_call():
43+
entity = Entity(
44+
"my-entity", description="My entity", value_type=ValueType.STRING
45+
)
3746
assert entity.tags == dict()
3847
assert len(entity.tags) == 0
3948

4049

4150
def test_entity_without_description():
42-
Entity("my-entity", value_type=ValueType.STRING)
51+
with pytest.deprecated_call():
52+
Entity("my-entity", value_type=ValueType.STRING)
53+
54+
55+
def test_name_not_specified():
56+
assertpy.assert_that(lambda: Entity(value_type=ValueType.STRING)).raises(ValueError)
57+
58+
59+
def test_multiple_args():
60+
assertpy.assert_that(lambda: Entity("a", "b", value_type=ValueType.STRING)).raises(
61+
ValueError
62+
)
63+
64+
65+
def test_name_keyword(recwarn):
66+
Entity(name="my-entity", value_type=ValueType.STRING)
67+
assert len(recwarn) == 0

0 commit comments

Comments
 (0)