Skip to content
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

Handle optional fields in dataclasses #32

Closed
wants to merge 1 commit into from

Conversation

silamon
Copy link
Contributor

@silamon silamon commented Jan 7, 2025

Summary by CodeRabbit

  • Improvements
    • Enhanced data model flexibility by making certain fields optional
    • Added support for nullable values in occupancy, departure connection, table availability, and luggage section information

Copy link
Contributor

coderabbitai bot commented Jan 7, 2025

📝 Walkthrough

Walkthrough

The pull request introduces modifications to the data models in the pyrail/models.py file, focusing on enhancing type flexibility. Several class attributes across LiveboardDeparture, VehicleStop, ConnectionDeparture, and Unit classes have been updated to use Optional types. This change allows these attributes to accept None values, providing more robust handling of scenarios where specific data might be unavailable or not applicable.

Changes

File Changes
pyrail/models.py - occupancy in LiveboardDeparture, VehicleStop, and ConnectionDeparture changed to Optional[Occupancy]
- departure_connection in VehicleStop changed to Optional[str]
- has_tables and has_luggage_section in Unit changed to Optional[bool]

Poem

🚂 In models of rail, where data flows free,
Optional types dance with glee!
None now welcome, no longer denied,
Flexibility rides on this coding ride! 🐰
Adaptable models, a programmer's art


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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.

Copy link
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.

Actionable comments posted: 0

🧹 Nitpick comments (3)
pyrail/models.py (3)

84-84: Consider adding a default value for consistency.

While the change to make occupancy optional is good, consider adding an explicit default value using field(default=None) for consistency with other optional fields in the codebase (like in VehicleStop class).

-    occupancy: Optional[Occupancy]  # Occupancy level
+    occupancy: Optional[Occupancy] = field(default=None)  # Occupancy level

265-270: Simplify default value implementation.

Using default_factory=lambda: None is unnecessarily complex. Since None is a singleton, you can simply use default=None.

-    occupancy: Optional[Occupancy] = field(
-        default_factory=lambda: None, metadata=field_options(alias="occupancy")
-    )  # Occupancy level
+    occupancy: Optional[Occupancy] = field(
+        default=None, metadata=field_options(alias="occupancy")
+    )  # Occupancy level
-    departure_connection: Optional[str] = field(
-        default_factory=lambda: None, metadata=field_options(alias="departureConnection")
-    )  # Departure connection link
+    departure_connection: Optional[str] = field(
+        default=None, metadata=field_options(alias="departureConnection")
+    )  # Departure connection link

Line range hint 84-351: Consider standardizing optional field declarations across the codebase.

There are three different patterns for declaring optional fields in this file:

  1. field: Optional[Type] (no default)
  2. field: Optional[Type] = field(default=None)
  3. field: Optional[Type] = field(default_factory=lambda: None)

Consider standardizing on field(default=None) pattern across all optional fields for better maintainability.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6c3a6a9 and f60f1d2.

📒 Files selected for processing (1)
  • pyrail/models.py (4 hunks)
🔇 Additional comments (2)
pyrail/models.py (2)

4-4: LGTM!

The Optional import is correctly added to support the type hint changes.


346-351: Simplify default value implementation and verify consumers.

  1. Using default_factory=lambda: None is unnecessarily complex. Since None is a singleton, you can simply use default=None.

  2. This change from bool to Optional[bool] might affect code that consumes these fields. Ensure that consumers handle the None case appropriately.

-    has_tables: Optional[bool] = field(
-        default_factory=lambda: None, metadata=field_options(alias="hasTables")
-    )  # Whether the unit has tables
+    has_tables: Optional[bool] = field(
+        default=None, metadata=field_options(alias="hasTables")
+    )  # Whether the unit has tables
-    has_luggage_section: Optional[bool] = field(
-        default_factory=lambda: None, metadata=field_options(alias="hasLuggageSection")
-    )  # Whether the unit has a luggage section
+    has_luggage_section: Optional[bool] = field(
+        default=None, metadata=field_options(alias="hasLuggageSection")
+    )  # Whether the unit has a luggage section

Let's verify the impact on consumers:

@tjorim
Copy link
Owner

tjorim commented Jan 8, 2025

Thanks. Already addressed in 557a1c7. Since liveboard endpoint returns departures, it always has an occupancy. The last stop of a vehicle does not (iRail/iRail#548 (comment)). hasTables has been confirmed to be not in the data (iRail/iRail#549 (comment)), I will assume this also applies to hasLuggageSection. Also did not see materialNumber or materialSubTypeName in the actual response.

@tjorim tjorim closed this Jan 8, 2025
@tjorim
Copy link
Owner

tjorim commented Jan 8, 2025

I forgot we can also query liveboard arrivals which also contains different data: #33

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