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

fix: Enforce kw args featureservice #2575

Merged
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
38 changes: 34 additions & 4 deletions sdk/python/feast/feature_service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from datetime import datetime
from typing import Dict, List, Optional, Union

Expand Down Expand Up @@ -47,8 +48,9 @@ class FeatureService:
@log_exceptions
def __init__(
self,
name: str,
features: List[Union[FeatureView, OnDemandFeatureView]],
*args,
name: Optional[str] = None,
features: Optional[List[Union[FeatureView, OnDemandFeatureView]]] = None,
tags: Dict[str, str] = None,
description: str = "",
owner: str = "",
Expand All @@ -59,10 +61,38 @@ def __init__(
Raises:
ValueError: If one of the specified features is not a valid type.
"""
self.name = name
positional_attributes = ["name", "features"]
_name = name
_features = features
if args:
warnings.warn(
(
"Feature service parameters should be specified as a keyword argument instead of a positional arg."
"Feast 0.23+ will not support positional arguments to construct feature service"
),
DeprecationWarning,
)
if len(args) > len(positional_attributes):
raise ValueError(
f"Only {', '.join(positional_attributes)} are allowed as positional args when defining "
f"feature service, for backwards compatibility."
)
if len(args) >= 1:
_name = args[0]
if len(args) >= 2:
_features = args[1]

if not _name:
raise ValueError("Feature service name needs to be specified")

if not _features:
# Technically, legal to create feature service with no feature views before.
_features = []

self.name = _name
self.feature_view_projections = []

for feature_grouping in features:
for feature_grouping in _features:
if isinstance(feature_grouping, BaseFeatureView):
self.feature_view_projections.append(feature_grouping.projection)
else:
Expand Down
48 changes: 48 additions & 0 deletions sdk/python/tests/unit/test_feature_service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from feast.feature_service import FeatureService
from feast.feature_view import FeatureView
from feast.field import Field
Expand Down Expand Up @@ -55,3 +57,49 @@ def test_hash():

s4 = {feature_service_1, feature_service_2, feature_service_3, feature_service_4}
assert len(s4) == 3


def test_feature_view_kw_args_warning():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test for the kwargs version not raising any deprecation warnings?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

with pytest.warns(DeprecationWarning):
service = FeatureService("name", [], tags={"tag_1": "tag"}, description="desc")
assert service.name == "name"
assert service.tags == {"tag_1": "tag"}
assert service.description == "desc"

# More positional args than name and features
with pytest.raises(ValueError):
service = FeatureService("name", [], {"tag_1": "tag"}, "desc")

# No name defined.
with pytest.raises(ValueError):
service = FeatureService(features=[], tags={"tag_1": "tag"}, description="desc")


def no_warnings(func):
def wrapper_no_warnings(*args, **kwargs):
with pytest.warns(None) as warnings:
func(*args, **kwargs)

if len(warnings) > 0:
raise AssertionError(
"Warnings were raised: " + ", ".join([str(w) for w in warnings])
)

return wrapper_no_warnings


@no_warnings
def test_feature_view_kw_args_normal():
file_source = FileSource(name="my-file-source", path="test.parquet")
feature_view = FeatureView(
name="my-feature-view",
entities=[],
schema=[
Field(name="feature1", dtype=Float32),
Field(name="feature2", dtype=Float32),
],
source=file_source,
)
_ = FeatureService(
name="my-feature-service", features=[feature_view[["feature1", "feature2"]]]
)