Skip to content

Conversation

@dbochkov-flexcompute
Copy link
Contributor

@dbochkov-flexcompute dbochkov-flexcompute commented Oct 20, 2025

forgot that log.error() doesn't raise an actual error

Greptile Overview

Updated On: 2025-10-20 23:29:22 UTC

Greptile Summary

This PR fixes a critical bug in simulation validation where log.error() was incorrectly assumed to raise an exception. The _validate_finalized() method in Simulation now properly captures and re-raises exceptions after logging, ensuring that validation failures when adding mode source PEC frames are surfaced to users rather than silently ignored. The corresponding test was updated to call _validate_finalized() explicitly instead of accessing the _finalized property directly, ensuring the validation logic is properly exercised. This change aligns with the codebase's error handling pattern where validation failures must raise exceptions, not just log messages, particularly important for pre-upload validation that prevents invalid simulations from being submitted.

Important Files Changed

Changed Files
Filename Score Overview
tidy3d/components/simulation.py 5/5 Fixed _validate_finalized() to properly capture and re-raise exceptions after logging, ensuring validation errors are not silently ignored
tests/test_components/test_source_frames.py 4/5 Updated test to call _validate_finalized() method instead of accessing _finalized property; inconsistency remains on line 42 where _finalized is still accessed directly

Confidence score: 4/5

  • This PR fixes a critical validation bug with minimal risk, though one test inconsistency remains unaddressed
  • Score reflects the correct fix to the core issue but potential incompleteness in test coverage (line 42 in test file still accesses _finalized directly without validation, which may or may not be intentional)
  • Pay close attention to tests/test_components/test_source_frames.py line 42 to determine if the direct _finalized access should also be updated to _validate_finalized()

Sequence Diagram

sequenceDiagram
    participant Test as test_source_absorber_frames
    participant Simulation as Simulation
    participant Log as log
    participant Exception as ValidationError

    Note over Test: Create bad_sim with conflicting<br/>mode source frame and projection monitor
    
    Test->>Simulation: bad_sim._validate_finalized()
    activate Simulation
    
    Simulation->>Simulation: try: _ = self._finalized
    activate Simulation
    
    Note over Simulation: Validation fails due to<br/>incompatible components
    
    Simulation->>Exception: Exception raised
    deactivate Simulation
    
    Simulation->>Log: log.error("Simulation fails after<br/>requested mode source PEC frames<br/>are added. Please inspect '._finalized'.")
    
    Simulation->>Exception: raise e
    deactivate Simulation
    
    Exception-->>Test: ValidationError raised
    
    Note over Test: pytest.raises(ValidationError)<br/>assertion passes
Loading

Context used:

  • Rule from dashboard - Use log.error instead of assert statements for error handling in production code. (source)

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.

2 files 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/simulation.py (100%)

Summary

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

Copy link
Collaborator

@momchil-flex momchil-flex left a comment

Choose a reason for hiding this comment

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

Yeah it's kind of the other way round, if you raise a Tidy3dError subclass, it will also get log.error-ed before exiting.

Do you want to also add anything about the frame = {} in this PR?

Copy link
Contributor

@caseyflex caseyflex left a comment

Choose a reason for hiding this comment

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

Why not just allow the original exception to be raised? or raise a new exception from it? What was the original reason for the try-catch logic?

@dbochkov-flexcompute
Copy link
Contributor Author

Do you want to also add anything about the frame = {} in this PR?

Actually, it seems like a common thing throughout tidy3d: setting {} in json to fields that are None by default results in initializing those fields with a class instance (for example, nonlinear_spec, modulation_spec, etc). Probably we need to come up with a way to prevent that from happening on the tidy3d model base level

@dbochkov-flexcompute
Copy link
Contributor Author

Why not just allow the original exception to be raised? or raise a new exception from it? What was the original reason for the try-catch logic?

to add the additional info that simulation fails after trying to add those auxiliary structures. For example:

ERROR: 2 different mediums detected on plane intersecting a        
DiffractionMonitor. Plane must be homogeneous.                     
WARNING: Could not execute validator 'check_fixed_angle_components'
because field 'monitors' failed validation.                        
ERROR: Simulation fails after requested mode source PEC frames are 
added. Please inspect '._finalized'. 

instead of just

ERROR: 2 different mediums detected on plane intersecting a        
DiffractionMonitor. Plane must be homogeneous.                     
WARNING: Could not execute validator 'check_fixed_angle_components'
because field 'monitors' failed validation.

@caseyflex
Copy link
Contributor

@dbochkov-flexcompute what about this:

except e:
    raise Exception(msg) from e

@dbochkov-flexcompute
Copy link
Contributor Author

@dbochkov-flexcompute what about this:

except e:
    raise Exception(msg) from e
Screenshot_20251021_101310

Works too, just doesn't log the error message. I don't remember to what extent it is import for propagating error information in GUI. I will switch to raise Tidy3dError(msg) from e, which does log and errors, but I don't really have a strong preference which way to go

@dbochkov-flexcompute dbochkov-flexcompute force-pushed the daniil/SCEM-11201-fix-validate-finalized branch from 3d837ec to 15a4378 Compare October 21, 2025 17:22
@momchil-flex
Copy link
Collaborator

Seems good to me, we merge, @caseyflex ?

@caseyflex
Copy link
Contributor

Seems good to me, we merge, @caseyflex ?

Sounds good to me

@dbochkov-flexcompute dbochkov-flexcompute added this pull request to the merge queue Oct 22, 2025
Merged via the queue into develop with commit 205e83d Oct 22, 2025
46 checks passed
@dbochkov-flexcompute dbochkov-flexcompute deleted the daniil/SCEM-11201-fix-validate-finalized branch October 22, 2025 18:28
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.

4 participants