Skip to content

NameError: name 'asdict' is not defined #6

Description

@nesi73

When running the inference script, I encounter an error in utils/misc.py on the line asdict(obj).items(). The error message indicates that asdict is not defined. I realized that the asdict function from the dataclasses module was not imported. I added dataclasses.asdict(obj).items() at the beginning to resolve this.

However, as mentioned in the previous comment, this solution does not work for NamedTuple instances. Therefore, I modified the code to differentiate between dataclass and NamedTuple instances. The final code looks like this:

if isinstance(obj, Mapping):
            return ty((k, map_fields(func, v, only_type)) for (k, v) in obj.items())
        elif dataclasses.is_dataclass(obj):
            # Dataclass
            return ty(
                **{k: map_fields(func, v, only_type) for (k, v) in dataclasses.asdict(obj).items()}
            )
        else:
            # NamedTuple
            return ty(
                **{k: map_fields(func, v, only_type) for (k, v) in obj._asdict().items()}
            )

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions