Skip to content

Runtime crash in Samformer initialization when metadata is None #2212

@lomesh2312

Description

@lomesh2312

Description

When initializing the Samformer model, the constructor accepts a metadata parameter with the type hint dict | None = None. However, the __init__ method unconditionally attempts to index into self.metadata (e.g., self.metadata['max_encoder_length']), which immediately throws a TypeError: 'NoneType' object is not subscriptable and crashes initialization if metadata is None.

Location

pytorch_forecasting/models/samformer/_samformer_v2.py, specifically lines 77-79.

Steps to reproduce

import torch.nn as nn
from pytorch_forecasting.models.samformer._samformer_v2 import Samformer

model = Samformer(loss=nn.MSELoss(), hidden_size=512, use_revin=True, metadata=None)

Suggested fix

Add an explicit validation check in the __init__ method, or remove the None default to make metadata a strictly required argument.

if metadata is None:
    raise ValueError("Samformer requires 'metadata' dictionary to be provided for initialization.")

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Needs triage & validation

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions