Skip to content

Satisfy linters and type checkers#9

Open
srnnkls wants to merge 2 commits intoclaude/fix-thread-safety-011CUVadQR4koPDAzN6K78zXfrom
satisfy-linters-and-type-checkers
Open

Satisfy linters and type checkers#9
srnnkls wants to merge 2 commits intoclaude/fix-thread-safety-011CUVadQR4koPDAzN6K78zXfrom
satisfy-linters-and-type-checkers

Conversation

@srnnkls
Copy link
Contributor

@srnnkls srnnkls commented Oct 26, 2025

No description provided.

LRU caches

- Implement snapshot semantics; return primitives directly Add mapping property to expose dict methods without field collisions
- Add refresh() and unwrap(); implement get, __contains__, __reversed__
- Add bounded LRU caches for schemas, proxy types, and attrs with locks
- Improve subschema extraction (nullable, defs, chain, unions, tuples, lists, dict)
- Support serializer functions with info_arg; avoid mutating schema type
- Do not set __pydantic_validator__ on child proxies
- Use Sequence for serialized data; wrap dicts in MappingProxyType
issues

- Add pyright and ruff to dev dependencies and lockfile
- Tighten typing across codebase for pyright:
- Use runtime_checkable on Protocols
- Refine template decorators to return type[T]
- Apply pydantic_dataclass directly to preserve class types
- Add targeted pyright ignore comments for dynamic attrs/returns
- Update transform utilities to access dynamic template attrs safely
- Clean up tests: adopt TypeAlias/Annotated, remove unused imports/vars

No functional behavior changes; improves static
type safety and linting compliance
@gemini-code-assist
Copy link

Summary of Changes

Hello @srnnkls, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on improving the code quality and maintainability of the project by integrating 'pyright' and 'ruff' for static analysis. It includes a substantial refactor of the 'SerializationProxy' class to enhance its Pydantic 'CoreSchema' handling, optimize caching mechanisms, and provide a more ergonomic API. Additionally, various type-hinting adjustments and linter compliance fixes have been applied throughout the codebase to satisfy the newly introduced static analysis tools.

Highlights

  • Linter and Type Checker Integration: Added 'pyright' and 'ruff' to development dependencies to enforce stricter code quality and type safety across the project.
  • Enhanced SerializationProxy Logic: Significantly refactored the '_extract_subschema' function within 'SerializationProxy' to support a wider range of Pydantic 'CoreSchema' types, improving the robustness and accuracy of subschema extraction.
  • Improved Cache Management: Implemented LRU (Least Recently Used) eviction for attribute caches within 'SerializationProxy' and streamlined cache access by removing lock-free fast paths, relying on 'OrderedDict' and explicit locks for thread safety.
  • SerializationProxy Ergonomics: Introduced new methods ('unwrap', 'mapping', 'refresh', 'contains', 'get', 'reversed') and refined existing dunder methods ('iter', 'len', 'index', 'bool') in 'SerializationProxy' for better usability and compatibility with templating engines like Jinja.
  • Type Hinting and Linter Compliance: Addressed numerous type-checking issues, particularly for dynamically added attributes in template classes, by adjusting type hints and adding 'pyright: ignore' comments where static analysis cannot infer types.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request does an excellent job of satisfying linters and type checkers, as the title suggests. The changes go beyond simple style fixes, introducing significant improvements to thread safety, robustness, and performance, particularly within the SerializationProxy. The addition of proper locking for cache access and the implementation of LRU eviction policies are great enhancements. The type hints are now much more accurate, and the codebase is more solid as a result. I've identified a few areas for further improvement: a bug that causes a crash when slicing a proxy object, and a couple of opportunities to make type hints even more precise. Overall, this is a very high-quality contribution.

# implement this correctly than to just keep piggybacking on pydantic's CoreSchema
# internals.
def _extract_subschema(schema: CoreSchema, name: str) -> CoreSchema:
def _extract_subschema(schema: CoreSchema, name_or_idx) -> CoreSchema:

Choose a reason for hiding this comment

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

medium

For better type safety and clarity, it's a good practice to add a type hint for the name_or_idx parameter. Based on its usage and the docstring, it appears to be a str or an int.

Suggested change
def _extract_subschema(schema: CoreSchema, name_or_idx) -> CoreSchema:
def _extract_subschema(schema: CoreSchema, name_or_idx: str | int) -> CoreSchema:

return cls


def with_(cls_or_instance: type[_T] | _T, **kwargs: Unpack[TemplateKwargs]) -> type[_T]:

Choose a reason for hiding this comment

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

medium

The return type hint for this function appears to be incorrect. It's declared as type[_T], but when an instance is passed as cls_or_instance, the function returns a new instance of _T (on line 65), not a type. This causes a type mismatch that is currently suppressed with pyright: ignore[reportReturnType].

The return type should be type[_T] | _T to accurately reflect both execution paths. For even better type inference, you could consider using @overload to define separate signatures for when a type is passed versus when an instance is passed.

Suggested change
def with_(cls_or_instance: type[_T] | _T, **kwargs: Unpack[TemplateKwargs]) -> type[_T]:
def with_(cls_or_instance: type[_T] | _T, **kwargs: Unpack[TemplateKwargs]) -> type[_T] | _T:

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