Skip to content

Commit ce3ef4c

Browse files
author
Val Brodsky
committed
PR feedback: refactor / DRY pydantic_compat logic
1 parent f592269 commit ce3ef4c

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

labelbox/pydantic_compat.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ def pydantic_import(class_name, sub_module_path: Optional[str] = None):
88
# Get the version of pydantic
99
pydantic_version = pkg_resources.get_distribution("pydantic").version
1010

11-
# Check if the version is 1
12-
if pydantic_version.startswith("1"):
13-
pydantic_v1_module_name = "pydantic" if sub_module_path is None else f"pydantic.{sub_module_path}"
14-
else: # use pydantic 2 v1 thunk
15-
pydantic_v1_module_name = "pydantic.v1" if sub_module_path is None else f"pydantic.{sub_module_path}"
16-
17-
klass = getattr(importlib.import_module(pydantic_v1_module_name),
18-
class_name)
11+
# Determine the module name based on the version
12+
module_name = "pydantic" if pydantic_version.startswith(
13+
"1") else "pydantic.v1"
14+
module_name = f"{module_name}.{sub_module_path}" if sub_module_path else module_name
15+
16+
# Import the class from the module
17+
klass = getattr(importlib.import_module(module_name), class_name)
1918

2019
return klass
2120

0 commit comments

Comments
 (0)