Skip to content

Commit b751596

Browse files
committed
Change type() to isinstance() to check object types
1 parent 8ff54b7 commit b751596

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

nemoguardrails/actions/llm/generation.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def _extract_user_message_example(self, flow: Flow) -> None:
155155
# The SpecOp.spec type is Union[Spec, dict]. Convert Dict to Spec if it's provided
156156
match_spec: Spec = (
157157
spec_op.spec
158-
if type(spec_op.spec) == Spec
158+
if isinstance(spec_op.spec, Spec)
159159
else Spec(**cast(Dict, spec_op.spec))
160160
)
161161

@@ -178,7 +178,7 @@ def _extract_user_message_example(self, flow: Flow) -> None:
178178
# which isn't in the Spec definition
179179
await_spec_dict: Dict[str, Any] = (
180180
asdict(spec_op.spec)
181-
if type(spec_op.spec) == Spec
181+
if isinstance(spec_op.spec, Spec)
182182
else cast(Dict, spec_op.spec)
183183
)
184184

@@ -212,15 +212,15 @@ def _extract_bot_message_example(self, flow: Flow):
212212

213213
el = flow.elements[1]
214214

215-
if type(el) != SpecOp:
215+
if not isinstance(el, SpecOp):
216216
return
217217

218218
spec_op: SpecOp = cast(SpecOp, el)
219219
spec: Dict[str, Any] = (
220220
asdict(
221221
spec_op.spec
222-
) # TODO! Refactor thiss function as it's duplicated in many places
223-
if type(spec_op.spec) == Spec
222+
) # TODO! Refactor this function as it's duplicated in many places
223+
if isinstance(spec_op.spec, Spec)
224224
else cast(Dict, spec_op.spec)
225225
)
226226

@@ -240,7 +240,7 @@ def _process_flows(self):
240240
"""Process the provided flows to extract the user utterance examples."""
241241
# Flows can be either Flow or Dict. Convert them all to Flow for following code
242242
flows: List[Flow] = [
243-
cast(Flow, flow) if type(flow) == Flow else Flow(**cast(Dict, flow))
243+
cast(Flow, flow) if isinstance(flow, Flow) else Flow(**cast(Dict, flow))
244244
for flow in self.config.flows
245245
]
246246

nemoguardrails/actions/v2_x/generation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ async def _collect_user_intent_and_examples(
216216
heads = find_all_active_event_matchers(state)
217217
for head in heads:
218218
el = get_element_from_head(state, head)
219-
element = el if type(el) == SpecOp else SpecOp(**cast(Dict, el))
219+
element = el if isinstance(el, SpecOp) else SpecOp(**cast(Dict, el))
220220
flow_state = state.flow_states[head.flow_state_uid]
221221
event = get_event_from_element(state, flow_state, element)
222222
if (

0 commit comments

Comments
 (0)