Ok, I understand. A workaround would be to try several possible attributes and return a warning/error if no valid attribute was found:
def get_label_max(dataset):
    max_label = 0
    if hasattr(dataset, 'train_labels'):
        max_label = torch.max(dataset.target_label)
    elif hasattr(dataset, 'labels'):
        max_label = torch.max(dataset.labels)
    elif hasattr(dataset, 'target'):
        max_label = torch.max(dataset.target)
    else:
        print('No labels found! ')
    return max_label

get_label_max(train_dataset)