Skip to content

Conversation

@marcorudolphflex
Copy link
Contributor

@marcorudolphflex marcorudolphflex commented Dec 22, 2025

Greptile Summary

Added support for NumPy 2.4.0 by addressing breaking changes in scalar handling, array indexing, and deprecated functions.

Key Changes:

  • Added automatic numpy scalar coercion in Tidy3dBaseModel via root validator to convert np.generic and 0-d arrays to Python scalars
  • Replaced np.sum() with built-in sum() for generator expressions to avoid numpy scalar issues
  • Fixed np.argwhere() calls to explicitly extract first column ([:, 0]) since numpy 2.4.0 always returns 2D arrays
  • Added .item() calls before float()/complex() conversions of xarray coordinates
  • Added compatibility shim for np.trapezoid (numpy ≥2.0) vs np.trapz (numpy <2.0)
  • Updated imports: moved Callable to collections.abc and Self to typing module

Issues Found:

  • Two similar locations (tidy3d/components/data/sim_data.py:649 and tidy3d/components/tcad/data/sim_data.py:436) still use float(field_data.coords[...]) without .item() and will fail with numpy 2.4.0

Confidence Score: 3/5

  • This PR has critical gaps - two runtime errors remain unfixed that will fail with numpy 2.4.0
  • The PR makes good progress on numpy 2.4.0 compatibility with thoughtful changes including a root validator for automatic scalar coercion. However, two identical issues were missed in sim_data.py files that use the same pattern (float(coords[...])) fixed elsewhere. These will cause runtime failures with numpy 2.4.0, making the compatibility incomplete.
  • Pay close attention to tidy3d/components/data/sim_data.py and tidy3d/components/tcad/data/sim_data.py - both have missing .item() calls that will cause runtime failures

Important Files Changed

Filename Overview
tidy3d/components/base.py Added numpy scalar coercion via root validator to handle numpy 2.4.0 changes; moved Callable import to collections.abc and Self to typing
tidy3d/components/geometry/utils.py Added [:, 0] indexing to extract first column from np.argwhere result for numpy 2.4.0 compatibility
tidy3d/components/data/sim_data.py Missing .item() call when converting xarray coordinate to float - will fail with numpy 2.4.0
tidy3d/components/tcad/data/sim_data.py Missing .item() call when converting xarray coordinate to float - will fail with numpy 2.4.0

Sequence Diagram

sequenceDiagram
    participant User as User Code
    participant Base as Tidy3dBaseModel
    participant Geom as GeometryGroup
    participant Utils as geometry/utils
    participant XArray as xarray Coords
    participant NumPy as NumPy 2.4.0

    Note over NumPy: NumPy 2.4.0 changes:<br/>- Scalars no longer cast implicitly<br/>- argwhere returns 2D array<br/>- trapz renamed to trapezoid

    User->>Base: Create model with numpy scalar
    Base->>Base: _coerce_numpy_scalars (root validator)
    Base->>NumPy: Check if np.generic or 0-d array
    NumPy-->>Base: Return numpy scalar type
    Base->>Base: Call .item() to extract Python scalar
    Base-->>User: Model created with Python scalars

    User->>Geom: Calculate volume/surface_area
    Geom->>Geom: Use sum() instead of np.sum()
    Note over Geom: Built-in sum() works with<br/>generators, avoids numpy issues
    Geom-->>User: Return calculated value

    User->>Utils: _shift_value_signed
    Utils->>NumPy: np.argwhere(condition)
    NumPy-->>Utils: 2D array [[idx], ...]
    Utils->>Utils: Extract [:, 0] to get 1D array
    Utils-->>User: Return correct indices

    User->>XArray: Access xarray coordinate
    XArray-->>User: Returns numpy scalar (2.4.0)
    User->>User: Call .item() before float()/complex()
    User-->>User: Correctly converted to Python type
Loading

@marcorudolphflex
Copy link
Contributor Author

@greptile

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.

Additional Comments (1)

  1. tests/test_components/test_mode_interp.py, line 627-628 (link)

    logic: Remove temporary debugging print statements

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

    Context Used: Rule from dashboard - Remove temporary debugging code (print() calls), commented-out code, and other workarounds before fi... (source)

6 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@marcorudolphflex marcorudolphflex force-pushed the FXC-4644-tidy-3-d-client-add-support-for-num-py-2-4-0 branch from a0cbfdf to 94cef22 Compare December 22, 2025 15:42
@marcorudolphflex marcorudolphflex marked this pull request as ready for review December 22, 2025 15:43
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.

Additional Comments (2)

  1. tidy3d/components/data/sim_data.py, line 649 (link)

    logic: needs .item() call for numpy 2.4.0 compatibility

  2. tidy3d/components/tcad/data/sim_data.py, line 436 (link)

    logic: needs .item() call for numpy 2.4.0 compatibility

6 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

@marcorudolphflex marcorudolphflex force-pushed the FXC-4644-tidy-3-d-client-add-support-for-num-py-2-4-0 branch from 94cef22 to bdd890c Compare December 23, 2025 07:58
@marcorudolphflex marcorudolphflex force-pushed the FXC-4644-tidy-3-d-client-add-support-for-num-py-2-4-0 branch from bdd890c to a463d68 Compare December 23, 2025 09:55
@github-actions
Copy link
Contributor

Diff Coverage

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

  • tidy3d/components/base.py (96.0%): Missing lines 210
  • tidy3d/components/geometry/base.py (100%)
  • tidy3d/components/geometry/utils.py (100%)
  • tidy3d/components/medium.py (100%)

Summary

  • Total: 32 lines
  • Missing: 1 line
  • Coverage: 96%

tidy3d/components/base.py

Lines 206-214

  206 
  207     @pydantic.root_validator(pre=True)
  208     def _coerce_numpy_scalars(cls, values: dict[str, Any]) -> dict[str, Any]:
  209         if not isinstance(values, dict):
! 210             return values
  211 
  212         for name, field in cls.__fields__.items():
  213             if name not in values or not cls._field_allows_scalar(field):
  214                 continue

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