Better Internat Static Typing #46

Merged
HideyoshiNakazone merged 3 commits from feature/better-internal-typing into main 2025-09-13 22:49:17 +00:00
HideyoshiNakazone commented 2025-09-13 02:58:31 +00:00 (Migrated from github.com)

This pull request refactors the type parsing logic across several parser modules to consistently use the JSONSchema type for schema properties. It also improves type safety and clarity, adds missing type annotations, and introduces better error handling for $ref parsing. The changes affect the core type parser, as well as the allOf, enum, object, oneOf, and $ref type parsers.

Type annotation and type safety improvements:

  • Updated method signatures in GenericTypeParser and all derived parsers (allOf, enum, object, $ref) to use the JSONSchema type for schema properties, replacing generic dict[str, Any] usage. This ensures consistent type checking and better IDE support. [1] [2] [3] [4] [5]
  • Added or refined type annotations for type variables, return types, and function arguments, including the introduction of the Annotation alias for clarity in the oneOf parser. [1] [2] [3] [4]

Error handling and logic improvements:

  • Improved $ref parsing in RefTypeParser by adding explicit error handling for unsupported or invalid references, and refactored cache logic to handle missing references more robustly. [1] [2] [3] [4] [5]
  • Added a missing required=True flag when building object types in the schema converter, ensuring required fields are properly handled.

Miscellaneous fixes and improvements:

  • Added # type: ignore comments where necessary to suppress type checker warnings for dynamic or runtime behaviors, such as dynamic enum creation and model generation. [1] [2] [3] [4]
  • Updated internal logic for merging properties and building models to use the new type annotations, improving maintainability and readability. [1] [2]

These changes collectively make the codebase more robust, easier to maintain, and safer for future development.

This pull request refactors the type parsing logic across several parser modules to consistently use the `JSONSchema` type for schema properties. It also improves type safety and clarity, adds missing type annotations, and introduces better error handling for `$ref` parsing. The changes affect the core type parser, as well as the `allOf`, `enum`, `object`, `oneOf`, and `$ref` type parsers. **Type annotation and type safety improvements:** * Updated method signatures in `GenericTypeParser` and all derived parsers (`allOf`, `enum`, `object`, `$ref`) to use the `JSONSchema` type for schema properties, replacing generic `dict[str, Any]` usage. This ensures consistent type checking and better IDE support. [[1]](diffhunk://#diff-0097348f93a37e2544eab3784f4902dd7d43900f05bd35e5b8cab85cfd868a77L1-R13) [[2]](diffhunk://#diff-04776a26401d735346b094272bbd9fe82b158e1402a6b8f79df76236954b05a8R2-R5) [[3]](diffhunk://#diff-90c8440c12d9b522b4dd3c5241db82c4fa3310c13f51600d7fc6a4501dc65d0dL3-R3) [[4]](diffhunk://#diff-34c0afbd0b65cfb477a4af7877931a3d3a07b25525b054633ef7db5feba83c1dR2-R7) [[5]](diffhunk://#diff-d16be762c5d7769e8b1823a352f89dc50dda662776e67a912ab1ca41aa7e0a8dR2-R8) * Added or refined type annotations for type variables, return types, and function arguments, including the introduction of the `Annotation` alias for clarity in the `oneOf` parser. [[1]](diffhunk://#diff-0097348f93a37e2544eab3784f4902dd7d43900f05bd35e5b8cab85cfd868a77L1-R13) [[2]](diffhunk://#diff-ee90b057908899d31f1e4b43d4dc255af6db0c81d03f76260f4957aad12f1742R8-R10) [[3]](diffhunk://#diff-ee90b057908899d31f1e4b43d4dc255af6db0c81d03f76260f4957aad12f1742L52-R56) [[4]](diffhunk://#diff-ee90b057908899d31f1e4b43d4dc255af6db0c81d03f76260f4957aad12f1742L77-R80) **Error handling and logic improvements:** * Improved `$ref` parsing in `RefTypeParser` by adding explicit error handling for unsupported or invalid references, and refactored cache logic to handle missing references more robustly. [[1]](diffhunk://#diff-d16be762c5d7769e8b1823a352f89dc50dda662776e67a912ab1ca41aa7e0a8dL35-R64) [[2]](diffhunk://#diff-d16be762c5d7769e8b1823a352f89dc50dda662776e67a912ab1ca41aa7e0a8dL72-R80) [[3]](diffhunk://#diff-d16be762c5d7769e8b1823a352f89dc50dda662776e67a912ab1ca41aa7e0a8dR95-R99) [[4]](diffhunk://#diff-d16be762c5d7769e8b1823a352f89dc50dda662776e67a912ab1ca41aa7e0a8dL109-R120) [[5]](diffhunk://#diff-d16be762c5d7769e8b1823a352f89dc50dda662776e67a912ab1ca41aa7e0a8dL120-R132) * Added a missing `required=True` flag when building object types in the schema converter, ensuring required fields are properly handled. **Miscellaneous fixes and improvements:** * Added `# type: ignore` comments where necessary to suppress type checker warnings for dynamic or runtime behaviors, such as dynamic enum creation and model generation. [[1]](diffhunk://#diff-90c8440c12d9b522b4dd3c5241db82c4fa3310c13f51600d7fc6a4501dc65d0dL30-R30) [[2]](diffhunk://#diff-34c0afbd0b65cfb477a4af7877931a3d3a07b25525b054633ef7db5feba83c1dL35-R59) [[3]](diffhunk://#diff-34c0afbd0b65cfb477a4af7877931a3d3a07b25525b054633ef7db5feba83c1dL66-R68) [[4]](diffhunk://#diff-5eb39c1cffff2b2a9a900defd2b0298fc810cf4d9206a237ecf69ece8fe07561L28-R28) * Updated internal logic for merging properties and building models to use the new type annotations, improving maintainability and readability. [[1]](diffhunk://#diff-04776a26401d735346b094272bbd9fe82b158e1402a6b8f79df76236954b05a8L47-R58) [[2]](diffhunk://#diff-34c0afbd0b65cfb477a4af7877931a3d3a07b25525b054633ef7db5feba83c1dL35-R59) These changes collectively make the codebase more robust, easier to maintain, and safer for future development.
codecov[bot] commented 2025-09-13 03:07:36 +00:00 (Migrated from github.com)

Codecov Report

All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

## [Codecov](https://app.codecov.io/gh/HideyoshiNakazone/jambo/pull/46?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Vitor+Hideyoshi) Report :white_check_mark: All modified and coverable lines are covered by tests. :loudspeaker: Thoughts on this report? [Let us know!](https://github.com/codecov/feedback/issues/255)
Sign in to join this conversation.