Skip to content

Add deserialize method to Trigger base class and annotate classes dict#596

Merged
gnachman merged 2 commits intognachman:masterfrom
mkusaka:fix/triggers-base-class-deserialize
Mar 2, 2026
Merged

Add deserialize method to Trigger base class and annotate classes dict#596
gnachman merged 2 commits intognachman:masterfrom
mkusaka:fix/triggers-base-class-deserialize

Conversation

@mkusaka
Copy link
Contributor

@mkusaka mkusaka commented Feb 28, 2026

Summary

Added a deserialize static method to the Trigger base class and a type annotation to the classes dict in decode_trigger, so that mypy can verify the classes[name].deserialize(...) call.

Background

  • decode_trigger() builds a dict mapping trigger names to subclasses, then calls .deserialize() on the looked-up class
  • mypy inferred the dict value type as type (without the annotation) or type[Trigger] (with the annotation), neither of which has a deserialize attribute
  • Both changes are needed: the annotation narrows the type from type to type[Trigger], and the base class method ensures type[Trigger] has deserialize
  • At runtime deserialize is always called on a concrete subclass, but the base class definition is required for static type checking

Alternatives considered

Annotating the dict value type as a Union of all 27 subclass types (e.g. Union[Type[AlertTrigger], Type[AnnotateTrigger], ...]) would make the base class method unnecessary, since each subclass already defines deserialize. However, this would be verbose and harder to maintain as new trigger types are added.

Verification

Confirmed that mypy --ignore-missing-imports iterm2/triggers.py no longer reports "type" has no attribute "deserialize" [attr-defined] or Returning Any [no-any-return] on line 81.

mkusaka added 2 commits March 1, 2026 05:11
The classes dict in decode_trigger was inferred as dict[str, type] by
mypy, causing "type has no attribute deserialize" error. Add a type
annotation and a base class deserialize method to fix this.
@gnachman gnachman merged commit c220d3e into gnachman:master Mar 2, 2026
2 checks passed
@mkusaka mkusaka deleted the fix/triggers-base-class-deserialize branch March 2, 2026 06:37
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.

2 participants