Skip to content

Fix reportOptionalMemberAccess errors in event_processor.py #214

@vredchenko

Description

@vredchenko

Summary

Pyright reports reportOptionalMemberAccess errors in src/smartem_agent/event_processor.py where code accesses attributes on values that could potentially be None.

Locations

Location 1: Lines 105-107

grid.acquisition_data = self.parser.parse_epu_session_manifest(str(event.file_path))

grid.acquisition_data.uuid = self.datastore.acquisition.uuid  # ← Error here

Problem: parse_epu_session_manifest() returns AcquisitionData | None. If it returns None, the next line crashes accessing .uuid on None.

Is this a real bug? Yes, potentially. If parsing fails, this would raise AttributeError. It's inside a try/except so it gets caught, but the error message would be confusing ("NoneType has no attribute uuid" rather than "failed to parse manifest").


Location 2: Lines 150-151

grid = self.datastore.get_grid(grid_uuid)
grid.atlas_data = atlas_data  # ← Error here

Problem: get_grid() returns self.grids.get(uuid) which is GridData | None.

Is this a real bug? Unlikely in practice - grid_uuid was validated earlier (lines 133-143 check if not grid_uuid and return ORPHANED). But there's a theoretical race condition if the grid was removed between the check and this call.


Location 3: Line 161

grid_uuid=grid.uuid,  # ← Error here (same grid variable from Location 2)

Same issue as Location 2 - if grid is None, accessing .uuid fails.


Recommended Fix

Add explicit None checks with early returns for clearer error handling:

grid.acquisition_data = self.parser.parse_epu_session_manifest(str(event.file_path))
if not grid.acquisition_data:
    logger.error(f"Failed to parse session manifest: {event.file_path}")
    return ProcessingResult.FAILED

grid.acquisition_data.uuid = self.datastore.acquisition.uuid

And similarly for the get_grid() case:

grid = self.datastore.get_grid(grid_uuid)
if not grid:
    logger.error(f"Grid {grid_uuid} not found in datastore")
    return ProcessingResult.FAILED

Current Workaround

Type ignore comments have been added referencing this issue to unblock CI while a proper fix is implemented.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugfixingFixing defects or unexpected behavior in existing codedevelopmentNew features or functionality implementationrefactoringCode restructuring without changing external behaviortestingWriting, updating, or fixing automated tests

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions