Skip to content

Suppress boilerplate warnings from pydantic error messages: #2620

@jewettaijfc

Description

@jewettaijfc

User-facing pydantic error messages are frequently unnecessarily long because they list warning messages for every single validator that failed to run (as well as stack-traces).

Example input

import tidy3d as td

source = td.UniformCurrentSource(
    center=(0, -1.0, 0),
    size=(1, 0, 0.5),
    polarization="Ex",
    source_time=td.GaussianPulse(
        freq0=100e14,
        fwidth=10e14,
    ),
)

# make simulation
sim = td.Simulation(
    size=(1, 1, 1),
    grid_spec=td.GridSpec.auto(wavelength=4),
    boundary_spec=td.BoundarySpec(
        x=td.Boundary.pml(num_layers=10),
        y=td.Boundary.periodic(),
        z=td.Boundary.pml(num_layers=10),
    ),
    sources=[source],
    run_time=1e-12,
)

Example output

14:39:13 EDT ERROR: simulation.sources[0] is outside of the simulation domain.  
             WARNING: Could not execute validator 'plane_wave_boundaries'       
             because field 'sources' failed validation.                         
             WARNING: Could not execute validator 'tfsf_boundaries' because     
             field 'sources' failed validation.                                 
             WARNING: Could not execute validator '_structures_not_close_pml'   
             because field 'sources' failed validation.                         
             WARNING: Could not execute validator 'bloch_boundaries_diff_mnt'   
             because field 'sources' failed validation.                         
             WARNING: Could not execute validator                               
             '_warn_monitor_simulation_frequency_range' because field 'sources' 
             failed validation.                                                 
             WARNING: Could not execute validator                               
             '_validate_auto_grid_wavelength' because field 'sources' failed    
             validation.                                                        
             WARNING: Could not execute validator '_warn_grid_size_too_small'   
             because field 'sources' failed validation.                         
             WARNING: Could not execute validator '_check_normalize_index'      
             because field 'sources' failed validation.                         
             WARNING: Could not execute validator 'check_fixed_angle_components'
             because field 'sources' failed validation.                         
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Cell In[7], line 16
     13 monitor = td.FieldMonitor(center=(-1.0, 0, 0), size=(0.5, 0, 1), freqs=[100e14], name="field_monitor_1")
     15 # make simulation
---> 16 sim = td.Simulation(
     17     size=(1, 1, 1),
     18     grid_spec=td.GridSpec.auto(wavelength=4),
     19     boundary_spec=td.BoundarySpec(
     20         x=td.Boundary.pml(num_layers=10),
     21         y=td.Boundary.periodic(),
     22         z=td.Boundary.pml(num_layers=10),
     23     ),
     24     # structures=[structure],
     25     sources=[source],
     26     # monitors=[monitor],
     27     run_time=1e-12,
     28 )

File [~/tidy3d/tidy3d/components/base.py:147](http://localhost:8888/home/jewett/tidy3d/tidy3d/components/base.py#line=146), in Tidy3dBaseModel.__init__(self, **kwargs)
    145 """Init method, includes post-init validators."""
    146 log.begin_capture()
--> 147 super().__init__(**kwargs)
    148 self._post_init_validators()
    149 log.end_capture(self)

File [~/anaconda3/envs/tidy3d_frontend_dev/lib/python3.11/site-packages/pydantic/v1/main.py:347](http://localhost:8888/home/jewett/anaconda3/envs/tidy3d_frontend_dev/lib/python3.11/site-packages/pydantic/v1/main.py#line=346), in BaseModel.__init__(__pydantic_self__, **data)
    345 values, fields_set, validation_error = validate_model(__pydantic_self__.__class__, data)
    346 if validation_error:
--> 347     raise validation_error
    348 try:
    349     object_setattr(__pydantic_self__, '__dict__', values)

ValidationError: 1 validation error for Simulation
sources
  simulation.sources[0] is outside of the simulation domain. (type=value_error.setup)

Requested solution:

Would it be possible to suppress these extra warning messages by default, so that our users see something like this instead?:

ValidationError: 1 validation error for Simulation
sources
  simulation.sources[0] is outside of the simulation domain. (type=value_error.setup)

(Simplifying the ugly stack trace is a separate issue.)

Priority

Low. (User complaints and requests should come first.)

It can wait until after the pydantic v2 refactor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions