Skip to content

Conversation

@dmarek-flex
Copy link
Contributor

@dmarek-flex dmarek-flex commented Oct 22, 2025

Testing out better type hints for these commonly used method, especially updated_copy

Greptile Overview

Updated On: 2025-10-22 14:25:57 UTC

Greptile Summary

This PR modernizes type hints in Tidy3dBaseModel by replacing explicit Tidy3dBaseModel return types with the Self type from typing_extensions. This affects 11 methods including copy(), updated_copy(), and several from_*() classmethods. The change enables type checkers to correctly infer that when a subclass (e.g., Simulation, Structure) calls these methods, they return instances of the subclass rather than the base class. This improves IDE autocomplete, catches type errors earlier, and follows modern Python typing best practices. Since Self is a pure type-checking construct available via typing_extensions, this change has zero runtime impact and maintains full backward compatibility while significantly improving the developer experience across the entire Tidy3D codebase.

Changed Files
Filename Score Overview
tidy3d/components/base.py 5/5 Updated return type annotations from Tidy3dBaseModel to Self for 11 methods to improve type inference for subclasses

Confidence score: 5/5

  • This PR is extremely safe to merge with no risk of runtime issues or breaking changes
  • Score reflects that Self is a pure type-checking construct with zero runtime behavior changes and is fully compatible with pydantic v1 via typing_extensions
  • No files require special attention; this is a straightforward typing improvement that benefits the entire codebase

@dmarek-flex dmarek-flex force-pushed the dmarek/improve_type_hints branch from 4b8ac48 to 1748154 Compare October 22, 2025 14:25
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

1 file reviewed, no comments

Edit Code Review Agent Settings | Greptile

@github-actions
Copy link
Contributor

Diff Coverage

Diff: origin/develop...HEAD, staged and unstaged changes

  • tidy3d/components/base.py (100%)

Summary

  • Total: 7 lines
  • Missing: 0 lines
  • Coverage: 100%

@dmarek-flex dmarek-flex changed the title improving type hints for BaseModel FXC-3795 improving type hints for Tidy3dBaseModel Oct 22, 2025
@dmarek-flex dmarek-flex force-pushed the dmarek/improve_type_hints branch 2 times, most recently from 0a0ac44 to c9dff28 Compare October 22, 2025 15:26
@dmarek-flex
Copy link
Contributor Author

dmarek-flex commented Oct 22, 2025

@yaugenst-flex Not sure about best way to put it in the docstring. But that approach is what AI suggested

Copy link
Collaborator

@yaugenst-flex yaugenst-flex left a comment

Choose a reason for hiding this comment

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

Thanks @dmarek-flex this is useful! The Self in the docstrings is fine I think, it's definitely correct. We'll have to see how confusing it is in the docs.
Note that we should add typing_extensions as an explicit dependency if we require it, even though it's already available as a transient dependency.

@dmarek-flex dmarek-flex force-pushed the dmarek/improve_type_hints branch from c9dff28 to 35fabbf Compare October 22, 2025 18:22
@dmarek-flex dmarek-flex self-assigned this Oct 22, 2025
@dmarek-flex dmarek-flex force-pushed the dmarek/improve_type_hints branch from 765d13d to c58959d Compare October 23, 2025 03:08
@yaugenst-flex yaugenst-flex added this pull request to the merge queue Oct 23, 2025
Merged via the queue into develop with commit e95dc7f Oct 23, 2025
35 checks passed
@yaugenst-flex yaugenst-flex deleted the dmarek/improve_type_hints branch October 23, 2025 08:56
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.

3 participants