Skip to content

LCORE-2046: Pattern match in types#1607

Merged
tisnik merged 1 commit into
lightspeed-core:mainfrom
tisnik:lcore-2046-pattern-match-in-types
Apr 28, 2026
Merged

LCORE-2046: Pattern match in types#1607
tisnik merged 1 commit into
lightspeed-core:mainfrom
tisnik:lcore-2046-pattern-match-in-types

Conversation

@tisnik
Copy link
Copy Markdown
Contributor

@tisnik tisnik commented Apr 28, 2026

Description

LCORE-2046: Pattern match in types

Type of change

  • Refactor
  • New feature
  • Bug fix
  • CVE fix
  • Optimization
  • Documentation Update
  • Configuration Update
  • Bump-up service version
  • Bump-up dependent library
  • Bump-up library or tool used for development (does not change the final image)
  • CI configuration change
  • Konflux configuration change
  • Unit tests improvement
  • Integration tests improvement
  • End to end tests improvement
  • Benchmarks improvement

Tools used to create PR

  • Assisted-by: N/A
  • Generated by: N/A

Related Tickets & Documents

  • Related Issue #LCORE-2046

Summary by CodeRabbit

  • Refactor
    • Internal implementation improvements with no user-facing changes.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 28, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 5d9e2e4b-eb31-405a-824a-6693279db8b8

📥 Commits

Reviewing files that changed from the base of the PR and between c0fd451 and 782a2ac.

📒 Files selected for processing (1)
  • src/utils/types.py
📜 Recent review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: Konflux kflux-prd-rh02 / lightspeed-stack-on-pull-request
  • GitHub Check: E2E: server mode / ci / group 2
  • GitHub Check: E2E: library mode / ci / group 1
  • GitHub Check: E2E: server mode / ci / group 1
  • GitHub Check: E2E: server mode / ci / group 3
  • GitHub Check: E2E: library mode / ci / group 3
  • GitHub Check: E2E Tests for Lightspeed Evaluation job
  • GitHub Check: E2E: library mode / ci / group 2
🧰 Additional context used
📓 Path-based instructions (2)
**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

**/*.py: Use absolute imports for internal modules: from authentication import get_auth_dependency
Import FastAPI dependencies with: from fastapi import APIRouter, HTTPException, Request, status, Depends
Import Llama Stack client with: from llama_stack_client import AsyncLlamaStackClient
Check constants.py for shared constants before defining new ones
All modules start with descriptive docstrings explaining purpose
Use logger = get_logger(__name__) from log.py for module logging
Type aliases defined at module level for clarity
Use Final[type] as type hint for all constants
All functions require docstrings with brief descriptions
Complete type annotations for parameters and return types in functions
Use typing_extensions.Self for model validators in Pydantic models
Use modern union type syntax str | int instead of Union[str, int]
Use Optional[Type] for optional type hints
Use snake_case with descriptive, action-oriented function names (get_, validate_, check_)
Avoid in-place parameter modification anti-patterns; return new data structures instead
Use async def for I/O operations and external API calls
Handle APIConnectionError from Llama Stack in error handling
Use standard log levels with clear purposes: debug, info, warning, error
All classes require descriptive docstrings explaining purpose
Use PascalCase for class names with standard suffixes: Configuration, Error/Exception, Resolver, Interface
Use ABC for abstract base classes with @abstractmethod decorators
Use @model_validator and @field_validator for Pydantic model validation
Complete type annotations for all class attributes; use specific types, not Any
Follow Google Python docstring conventions with Parameters, Returns, Raises, and Attributes sections

Files:

  • src/utils/types.py
src/**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

Pydantic models extend ConfigurationBase for config, BaseModel for data models

Files:

  • src/utils/types.py
🧠 Learnings (1)
📓 Common learnings
Learnt from: CR
Repo: lightspeed-core/lightspeed-stack PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-04-19T15:40:25.624Z
Learning: Applies to **/*.py : Use modern union type syntax `str | int` instead of `Union[str, int]`
🔇 Additional comments (2)
src/utils/types.py (2)

68-80: Clean refactor to pattern matching.

The implementation correctly uses structural pattern matching to replace the sequential if/isinstance checks. The pattern order is appropriate (specific types before the wildcard), and all conversion behaviors are preserved:

  • None""
  • String instances return as-is
  • TextContentItem extracts .text
  • ImageContentItem returns placeholder "<image>"
  • Lists recursively convert and join elements
  • Other types fall back to str()

68-80: Pattern matching syntax is compatible with project's Python 3.12+ requirement.

The refactored content_to_str function correctly implements structural pattern matching with the proper sequence of patterns (specific types before wildcard). The project's pyproject.toml specifies requires-python = ">=3.12,<3.14", which fully supports the match/case syntax introduced in Python 3.10. No compatibility issues.


Walkthrough

The content_to_str function in src/utils/types.py was refactored to use Python structural pattern matching (match/case) instead of sequential if/isinstance checks, while preserving identical type conversion behavior for None, str, TextContentItem, ImageContentItem, list, and other types.

Changes

Cohort / File(s) Summary
Pattern Matching Refactor
src/utils/types.py
Replaced if/isinstance chain in content_to_str with structural pattern matching via match/case statements, maintaining equivalent conversions for all input types.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main change: refactoring to use pattern matching in the types module (LCORE-2046), which aligns with the file changes and PR objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
✨ Simplify code
  • Create PR with simplified code

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@tisnik tisnik merged commit d4935dd into lightspeed-core:main Apr 28, 2026
30 checks passed
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.

1 participant