-
Notifications
You must be signed in to change notification settings - Fork 542
chore(types): Type-clean /actions (189 errors) #1361
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
base: chore/type-clean-guardrails
Are you sure you want to change the base?
chore(types): Type-clean /actions (189 errors) #1361
Conversation
…mo_guardrails/nemoguardrails/actions/llm/generation.py:537:40 - error: Never is not awaitable (reportGeneralTypeIssues)
…s() to remove Optional qualifier from bool
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #1361 +/- ##
===========================================
- Coverage 71.68% 71.62% -0.07%
===========================================
Files 168 171 +3
Lines 16862 17053 +191
===========================================
+ Hits 12088 12214 +126
- Misses 4774 4839 +65
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
@tgasser-nv, any guidance on how to review this? I'm struggling a bit. If we're more strongly enforcing types, do we mainly rely on test coverage to ensure we haven't broken anything with the new constraints? |
…m_action_invoked_via_logs test failure.
Yes, I run the unit-tests continuously as I'm cleaning Pyright errors. We have far too many optional types in our Pydantic models and function/method signatures, the biggest bucket of fixes are asserting these aren't None before going on to use them. The lines which are either too complex to fix (i.e. generation.py |
Status: Pyright errors: 0, waived: 7. |
Fixing the types exposed a bug in an untested function. The
|
Review Report for PR #1361: Static Typing Fixes
All modifications are strictly related to type annotations and associated safety checks. No functional logic has been altered, and all existing unit tests continue to pass, indicating that these changes have not introduced any regressions.
Category A: Improved None Safety Why it's necessary: This is critical for preventing AttributeError or TypeError exceptions at runtime. Without these changes, a caller might assume a function always returns a valid object and attempt to call a method on it, leading to a crash if None is returned. Examples from the PR: nemoguardrails/actions/action_dispatcher.py: The return types for _get_action_info_for_instance and _get_action_info_for_name were updated to Optional[dict], with corresponding if not info: checks added where they are called. nemoguardrails/llm/providers/openai.py: Added multiple None checks for attributes on the completion object from the API response (e.g., tool_calls, function.name, function.arguments) to handle cases where these optional fields are not present. nemoguardrails/llm/task_manager.py: Numerous None checks were added for dictionary lookups (e.g., config = self.llm_configs.get(llm_name)). The return types for functions like get_llm_instance_or_register were also updated to Optional[LLM]. nemoguardrails/rails/llm/llmrails.py: Added checks for potentially None attributes like self.main_llm_info and self.streaming_handler before they are accessed. nemoguardrails/utils.py: In new_event_dict, a check if meta: was added before attempting to call meta.update() to prevent a crash if the "meta" key does not exist. Category B: Correcting Incompatible Argument/Variable Types Why it's necessary: This enforces data integrity and ensures that functions operate on the data types they were designed for. It prevents subtle bugs that can be hard to trace, where a function might behave unexpectedly due to receiving the wrong type of input. Examples from the PR: nemoguardrails/rails/llm/bot_message.py: In parse_raw_llm_output, the input to json.loads is now explicitly cast using str(value). This corrects a type error, as json.loads expects a string or byte-like object, but the input value could have been of Any type. tests/test_flow_execution.py: The return_value in an ActionFinished event was explicitly set to None to match the updated type hints from the source code, ensuring the test case accurately reflects expected data types. Category C: Strengthening Generic Type Definitions Why it's necessary: While dict is a valid type hint, Dict[str, Any] is much more descriptive. It tells other developers and the type checker what kind of keys and values to expect. This allows the type checker to catch more sophisticated errors and improves overall code readability. Examples from the PR: nemoguardrails/actions/knowledge_base.py: The type hint for the docs variable was changed from a generic List to the more specific List[Document]. This allows the type checker to validate that subsequent access to attributes like doc.page_content is correct.
|
Description
Nemo Guardrails doesn't have type-checking enforced in the project today. This PR will be used to compile type-cleaning related to the
nemoguardrails/actions
module prior to merging into the top-level type-cleaning PR1367.Progress
Prior to this work the
actions
module had 189 errors, 0 warnings, and 0 informations.Related Issue(s)
Checklist