Skip to content

no blind exceptions#569

Merged
chrisaddy merged 6 commits intomasterfrom
ble
Jun 3, 2025
Merged

no blind exceptions#569
chrisaddy merged 6 commits intomasterfrom
ble

Conversation

@chrisaddy
Copy link
Copy Markdown
Collaborator

@chrisaddy chrisaddy commented May 28, 2025

This pull request introduces several updates across multiple files to improve type safety, enhance error handling, and refine functionality in the datamanager and positionmanager services. Key changes include the addition of type annotations, improved exception handling, and updates to configuration and validation logic.

Type Safety Enhancements:

  • Added type annotations to function signatures in application/datamanager/features/environment.py and application/datamanager/features/steps/equity_bars_steps.py to improve code clarity and enforce type checks. [1] [2]
  • Updated Pydantic model validators in application/datamanager/src/datamanager/models.py and application/positionmanager/src/positionmanager/models.py to use type annotations for input parameters and validation logic. [1] [2] [3] [4]

Error Handling Improvements:

  • Enhanced exception handling in application/datamanager/src/datamanager/main.py and application/positionmanager/src/positionmanager/main.py by replacing generic Exception blocks with specific exceptions like requests.RequestException, ComputeError, and ValidationError. [1] [2] [3] [4] [5]

Configuration and Validation Updates:

  • Updated environment variable-dependent fields in application/datamanager/src/datamanager/config.py and application/positionmanager/src/positionmanager/clients.py to use optional types (str | None) for better handling of missing values. [1] [2] [3]
  • Added stricter validation for date ranges in application/datamanager/src/datamanager/models.py and application/positionmanager/src/positionmanager/models.py to ensure logical consistency (e.g., end date must be after start date). [1] [2]

Dependency and Timeout Adjustments:

  • Introduced uvx to enhance task execution in .mise.toml, and added a timeout parameter to HTTP requests in application/datamanager/features/steps/equity_bars_steps.py and application/datamanager/features/steps/health_steps.py for better reliability. [1] [2] [3] [4]

Miscellaneous Changes:

  • Refactored imports in application/datamanager/src/datamanager/main.py and application/positionmanager/src/positionmanager/main.py to improve organization and readability. [1] [2] [3]
  • Updated test cases in application/positionmanager/tests/test_positionmanager_main.py to include type annotations for better test maintainability.

Summary by CodeRabbit

  • New Features

    • Added new project dependencies: Flytekit, Pulumi Docker Build, Pulumi GCP, and Requests.
    • Introduced additional type checking in Python linting tasks.
  • Bug Fixes

    • Enforced 30-second timeouts on HTTP requests in feature tests to prevent hanging.
  • Refactor

    • Improved exception handling in API endpoints for more precise error reporting.
    • Enhanced type annotations across codebase for better clarity and type safety.
  • Tests

    • Updated test suite with improved type hints and more accurate error simulation.
  • Chores

    • Updated linting and type checking configurations for stricter code quality enforcement.

update to fix blind exceptions
@chrisaddy chrisaddy requested a review from forstmeier May 28, 2025 21:06
@chrisaddy chrisaddy self-assigned this May 28, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 28, 2025

Warning

Rate limit exceeded

@chrisaddy has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 9 minutes and 36 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 66249bb and 37e81da.

📒 Files selected for processing (12)
  • application/datamanager/src/datamanager/main.py (5 hunks)
  • application/datamanager/src/datamanager/models.py (3 hunks)
  • application/positionmanager/src/positionmanager/clients.py (2 hunks)
  • application/positionmanager/src/positionmanager/main.py (6 hunks)
  • application/positionmanager/src/positionmanager/models.py (2 hunks)
  • application/positionmanager/src/positionmanager/portfolio.py (1 hunks)
  • application/positionmanager/tests/test_positionmanager_main.py (4 hunks)
  • infrastructure/cloud_run.py (3 hunks)
  • infrastructure/images.py (3 hunks)
  • infrastructure/monitoring.py (3 hunks)
  • infrastructure/project.py (1 hunks)
  • pyproject.toml (1 hunks)

Walkthrough

This update introduces explicit type annotations across multiple modules, refines exception handling in FastAPI endpoints, and enforces HTTP request timeouts. Project dependencies and linting configurations are expanded, and command-line tasks are updated to use the uvx tool. Minor code formatting and import adjustments are also included.

Changes

File(s) Change Summary
.mise.toml, pyproject.toml Updated task commands to use uvx; added/updated dependencies and introduced new lint/type-check configs.
application/datamanager/features/environment.py, .../steps/equity_bars_steps.py, .../steps/health_steps.py Added explicit type annotations to step functions; enforced 30s timeout on HTTP requests; minor comment and import changes.
application/datamanager/src/datamanager/config.py Changed certain class attribute types from str to `str
application/datamanager/src/datamanager/main.py Refined exception handling to catch specific errors; added/updated return type annotations; minor SQL and import changes.
application/datamanager/src/datamanager/models.py Added explicit type annotations to field validator methods; imported core_schema for typing.
application/positionmanager/src/positionmanager/clients.py Allowed constructor parameters to be `str
application/positionmanager/src/positionmanager/main.py Refined exception handling to specific error types in endpoints; added return type annotation to health endpoint; updated imports.
application/positionmanager/src/positionmanager/models.py Added explicit type annotations to validator methods; imported core_schema.
application/positionmanager/src/positionmanager/portfolio.py Added explicit return type annotation to PortfolioOptimizer.__init__.
application/positionmanager/tests/test_positionmanager_main.py Improved type annotations in test methods; simulated errors with more accurate exceptions; adjusted assertions.
infrastructure/project.py Reformatted lambda expression for IAM member; added type ignore comment; no logic changes.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant FastAPI_App
    participant ExternalService

    Client->>FastAPI_App: Send API request (e.g., /equity-bars)
    FastAPI_App->>ExternalService: Make HTTP request with 30s timeout
    ExternalService-->>FastAPI_App: Respond (or error)
    alt Success
        FastAPI_App-->>Client: Return result
    else Specific Error (Requests, API, Validation, etc.)
        FastAPI_App-->>Client: Return HTTP 500 with error message
    end
Loading

Suggested labels

python, bug

Suggested reviewers

  • forstmeier

Poem

In the warren where code does hop,
Type hints now bloom atop,
Exceptions caught with care anew,
Linting rabbits checking through.
With uvx and Ruff, our code’s robust—
A garden tidy, reviewed, and just!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@chrisaddy chrisaddy changed the base branch from master to bandit-fixes May 28, 2025 21:07
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Caution

Inline review comments failed to post. This is likely due to GitHub's limits when posting large numbers of comments. If you are seeing this consistently it is likely a permissions issue. Please check "Moderation" -> "Code review limits" under your organization settings.

Actionable comments posted: 5

🛑 Comments failed to post (5)
application/datamanager/src/datamanager/models.py (1)

32-34: ⚠️ Potential issue

Mismatch in type annotation: use datetime.date instead of datetime.datetime.
The DateRange model fields are declared as datetime.date, so the validator will receive and should return a date, not a datetime. Update the signature accordingly:

 @field_validator("end")
 @classmethod
-def check_end_after_start(
-    cls, end_value: datetime.datetime, info: core_schema.ValidationInfo
-) -> datetime.datetime:
+def check_end_after_start(
+    cls, end_value: datetime.date, info: core_schema.ValidationInfo
+) -> datetime.date:
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

    @field_validator("end")
    @classmethod
    def check_end_after_start(
        cls, end_value: datetime.date, info: core_schema.ValidationInfo
    ) -> datetime.date:
        start_value = info.data.get("start")
🤖 Prompt for AI Agents
In application/datamanager/src/datamanager/models.py around lines 32 to 34, the
method signature uses datetime.datetime for the end_value parameter and return
type, but the DateRange model fields are datetime.date. Change the type
annotations from datetime.datetime to datetime.date for both the parameter and
the return type to match the model's field types.
application/datamanager/src/datamanager/config.py (1)

8-8: ⚠️ Potential issue

Type annotations are more accurate, but potential runtime issue detected.

The change from str to str | None correctly reflects that os.getenv() can return None when environment variables are not set. However, this introduces a potential runtime issue with the daily_bars_path computed field on line 19.

Issue: The daily_bars_path method uses self.name in an f-string, which will raise a TypeError if name is None:

return f"gs://{self.name}/equity/bars/"  # Fails if self.name is None

Suggested fix: Handle the None case in the computed field:

@computed_field
def daily_bars_path(self) -> str:
+   if self.name is None:
+       raise ValueError("Bucket name is required but not configured")
    return f"gs://{self.name}/equity/bars/"

Alternatively, you could make this field optional and return None when name is not set.

Also applies to: 14-15

🤖 Prompt for AI Agents
In application/datamanager/src/datamanager/config.py around lines 8 and 14-19,
the type annotation for api_key was changed to str | None to reflect that
os.getenv() can return None, but this causes a runtime error in the
daily_bars_path computed field when self.name is None. To fix this, modify the
daily_bars_path method to check if self.name is None before using it in the
f-string, and either return None or a safe default value when name is not set,
preventing a TypeError.
application/positionmanager/src/positionmanager/clients.py (1)

15-16: 🛠️ Refactor suggestion

Type annotation inconsistency with default values.

The parameters now accept str | None but default to empty strings, which creates confusion. If None is an acceptable value, consider defaulting to None instead of empty strings for clarity, or remove None from the type annotation if empty strings are the intended default.

Consider this approach for better consistency:

-        api_key: str | None = "",
-        api_secret: str | None = "",
+        api_key: str | None = None,
+        api_secret: str | None = None,

This aligns the type annotation with the actual intention and makes the validation logic more explicit.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

        api_key: str | None = None,
        api_secret: str | None = None,
🤖 Prompt for AI Agents
In application/positionmanager/src/positionmanager/clients.py around lines 15 to
16, the parameters api_key and api_secret are annotated as str | None but
default to empty strings, causing inconsistency. To fix this, either change the
default values to None to match the type annotation or remove None from the type
annotation if empty strings are the intended defaults. This ensures the type
hints align with the actual default values and clarifies the expected input.
application/datamanager/src/datamanager/main.py (2)

114-120: ⚠️ Potential issue

Fix typo in exception class name.

There's a typo in the exception handling - requests.RequestsException should be requests.RequestException.

Apply this diff to fix the typo:

-        requests.RequestsException,
+        requests.RequestException,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

    except (
        requests.RequestException,
        ComputeError,
        IOException,
        GoogleAPIError,
        pyarrow.lib.ArrowIOError,
    ) as e:
🤖 Prompt for AI Agents
In application/datamanager/src/datamanager/main.py around lines 114 to 120, the
exception class name is incorrectly written as requests.RequestsException.
Change it to the correct requests.RequestException to properly catch the
intended exceptions.

157-163: ⚠️ Potential issue

Fix typo in exception class name (duplicate issue).

Same typo as in the GET endpoint - requests.RequestsException should be requests.RequestException.

Apply this diff to fix the typo:

-            requests.RequestsException,
+            requests.RequestException,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

        except (
            requests.RequestException,
            ComputeError,
            IOException,
            GoogleAPIError,
            pyarrow.lib.ArrowIOError,
        ) as e:
🤖 Prompt for AI Agents
In application/datamanager/src/datamanager/main.py around lines 157 to 163, the
exception class name is incorrectly written as requests.RequestsException.
Change it to the correct requests.RequestException to fix the typo and properly
catch the intended exception.

chrisaddy added 2 commits May 28, 2025 17:12
add bugbear
add comma linting

fix bugbear and commas

add timezones
@forstmeier forstmeier added this to the Rebuild milestone May 31, 2025
forstmeier
forstmeier previously approved these changes Jun 3, 2025
Base automatically changed from bandit-fixes to master June 3, 2025 01:42
@chrisaddy chrisaddy dismissed forstmeier’s stale review June 3, 2025 01:42

The base branch was changed.

@chrisaddy chrisaddy merged commit 00d9a33 into master Jun 3, 2025
0 of 2 checks passed
@chrisaddy chrisaddy deleted the ble branch June 3, 2025 01:43
@coderabbitai coderabbitai Bot mentioned this pull request Jun 4, 2025
@coderabbitai coderabbitai Bot mentioned this pull request Sep 4, 2025
This was referenced Sep 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants