Skip to content

Commit df59a5d

Browse files
committed
CR updates
Signed-off-by: Achal Shah <achals@gmail.com>
1 parent 0b3d14c commit df59a5d

5 files changed

Lines changed: 42 additions & 58 deletions

File tree

‎sdk/python/feast/errors.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ def __init__(self, online_store_class_name: str):
8585
)
8686

8787

88-
class FeastStoreConfigInvalidName(Exception):
89-
def __init__(self, store_config_class_name: str, store_type: str):
88+
class FeastClassInvalidName(Exception):
89+
def __init__(self, class_name: str, class_type: str):
9090
super().__init__(
91-
f"Config Class '{store_config_class_name}' "
92-
f"should end with the string `{store_type}Config`.'"
91+
f"Config Class '{class_name}' "
92+
f"should end with the string `{class_type}`.'"
9393
)
9494

9595

‎sdk/python/feast/importer.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import importlib
2+
3+
from feast import errors
4+
5+
6+
def get_class_from_type(module_name: str, class_name: str, class_type: str):
7+
if not class_name.endswith(class_type):
8+
raise errors.FeastClassInvalidName(class_name, class_type)
9+
10+
# Try importing the module that contains the custom provider
11+
try:
12+
module = importlib.import_module(module_name)
13+
except Exception as e:
14+
# The original exception can be anything - either module not found,
15+
# or any other kind of error happening during the module import time.
16+
# So we should include the original error as well in the stack trace.
17+
raise errors.FeastModuleImportError(module_name, class_type) from e
18+
19+
# Try getting the provider class definition
20+
try:
21+
_class = getattr(module, class_name)
22+
except AttributeError:
23+
# This can only be one type of error, when class_name attribute does not exist in the module
24+
# So we don't have to include the original exception here
25+
raise errors.FeastClassImportError(
26+
module_name, class_name, class_type=class_type
27+
) from None
28+
return _class

‎sdk/python/feast/infra/provider.py‎

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import abc
2-
import importlib
32
from datetime import datetime
43
from pathlib import Path
54
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union
@@ -8,7 +7,7 @@
87
import pyarrow
98
from tqdm import tqdm
109

11-
from feast import errors
10+
from feast import errors, importer
1211
from feast.entity import Entity
1312
from feast.feature_table import FeatureTable
1413
from feast.feature_view import FeatureView
@@ -156,24 +155,9 @@ def get_provider(config: RepoConfig, repo_path: Path) -> Provider:
156155
# For example, provider 'foo.bar.MyProvider' will be parsed into 'foo.bar' and 'MyProvider'
157156
module_name, class_name = config.provider.rsplit(".", 1)
158157

159-
# Try importing the module that contains the custom provider
160-
try:
161-
module = importlib.import_module(module_name)
162-
except Exception as e:
163-
# The original exception can be anything - either module not found,
164-
# or any other kind of error happening during the module import time.
165-
# So we should include the original error as well in the stack trace.
166-
raise errors.FeastModuleImportError(module_name, "provider") from e
167-
168-
# Try getting the provider class definition
169-
try:
170-
ProviderCls = getattr(module, class_name)
171-
except AttributeError:
172-
# This can only be one type of error, when class_name attribute does not exist in the module
173-
# So we don't have to include the original exception here
174-
raise errors.FeastClassImportError(module_name, class_name) from None
175-
176-
return ProviderCls(config, repo_path)
158+
cls = importer.get_class_from_type(module_name, class_name, "Provider")
159+
160+
return cls(config, repo_path)
177161

178162

179163
def _get_requested_feature_views_to_features_dict(

‎sdk/python/feast/repo_config.py‎

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import importlib
21
from pathlib import Path
32
from typing import Any
43

@@ -7,7 +6,7 @@
76
from pydantic.error_wrappers import ErrorWrapper
87
from pydantic.typing import Dict, Optional, Union
98

10-
from feast import errors
9+
from feast.importer import get_class_from_type
1110
from feast.telemetry import log_exceptions
1211

1312
# These dict exists so that:
@@ -185,33 +184,6 @@ def __repr__(self) -> str:
185184
)
186185

187186

188-
def get_config_class_from_type(
189-
module_name: str, config_class_name: str, store_type: str
190-
):
191-
if not config_class_name.endswith(f"{store_type}Config"):
192-
raise errors.FeastStoreConfigInvalidName(config_class_name, store_type)
193-
194-
# Try importing the module that contains the custom provider
195-
try:
196-
module = importlib.import_module(module_name)
197-
except Exception as e:
198-
# The original exception can be anything - either module not found,
199-
# or any other kind of error happening during the module import time.
200-
# So we should include the original error as well in the stack trace.
201-
raise errors.FeastModuleImportError(module_name, store_type) from e
202-
203-
# Try getting the provider class definition
204-
try:
205-
online_store_config_class = getattr(module, config_class_name)
206-
except AttributeError:
207-
# This can only be one type of error, when class_name attribute does not exist in the module
208-
# So we don't have to include the original exception here
209-
raise errors.FeastClassImportError(
210-
module_name, config_class_name, class_type=f"{store_type}Config"
211-
) from None
212-
return online_store_config_class
213-
214-
215187
def get_online_config_from_type(online_store_type: str):
216188
if online_store_type in ONLINE_STORE_CLASS_FOR_TYPE:
217189
online_store_type = ONLINE_STORE_CLASS_FOR_TYPE[online_store_type]
@@ -220,7 +192,7 @@ def get_online_config_from_type(online_store_type: str):
220192
module_name, online_store_class_type = online_store_type.rsplit(".", 1)
221193
config_class_name = f"{online_store_class_type}Config"
222194

223-
return get_config_class_from_type(module_name, config_class_name, "OnlineStore")
195+
return get_class_from_type(module_name, config_class_name, config_class_name)
224196

225197

226198
def get_offline_config_from_type(offline_store_type: str):
@@ -231,7 +203,7 @@ def get_offline_config_from_type(offline_store_type: str):
231203
module_name, offline_store_class_type = offline_store_type.rsplit(".", 1)
232204
config_class_name = f"{offline_store_class_type}Config"
233205

234-
return get_config_class_from_type(module_name, config_class_name, "OfflineStore")
206+
return get_class_from_type(module_name, config_class_name, config_class_name)
235207

236208

237209
def load_repo_config(repo_path: Path) -> RepoConfig:

‎sdk/python/tests/test_cli_local.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,18 @@ def test_3rd_party_providers() -> None:
164164
assertpy.assert_that(return_code).is_equal_to(1)
165165
assertpy.assert_that(output).contains(b"Provider 'feast123' is not implemented")
166166
# Check with incorrect third-party provider name (with dots)
167-
with setup_third_party_provider_repo("feast_foo.provider") as repo_path:
167+
with setup_third_party_provider_repo("feast_foo.Provider") as repo_path:
168168
return_code, output = runner.run_with_output(["apply"], cwd=repo_path)
169169
assertpy.assert_that(return_code).is_equal_to(1)
170170
assertpy.assert_that(output).contains(
171-
b"Could not import provider module 'feast_foo'"
171+
b"Could not import Provider module 'feast_foo'"
172172
)
173173
# Check with incorrect third-party provider name (with dots)
174174
with setup_third_party_provider_repo("foo.FooProvider") as repo_path:
175175
return_code, output = runner.run_with_output(["apply"], cwd=repo_path)
176176
assertpy.assert_that(return_code).is_equal_to(1)
177177
assertpy.assert_that(output).contains(
178-
b"Could not import provider 'FooProvider' from module 'foo'"
178+
b"Could not import Provider 'FooProvider' from module 'foo'"
179179
)
180180
# Check with correct third-party provider name
181181
with setup_third_party_provider_repo("foo.provider.FooProvider") as repo_path:

0 commit comments

Comments
 (0)