[JIT] NamedTuple's _fields attribute

Don’t know is it a bug or just partial coverage but the _fields attribute of NamedTuple unavailable while scripting.

class _Contents(NamedTuple):
    a : Optional[torch.Tensor]
    b : Optional[torch.Tensor]
    c : Optional[torch.Tensor]
    d : Optional[torch.Tensor]

@torch.jit.script
class Container:
   def __init__(self, fields):
      # type: (Tuple[int, int], _Contents)
      self.fields = fields
  def has(self, name: str) -> bool:
        """
        Returns:
            bool: whether the field called `name` exists.
        """
        return name in self.fields._fields
RuntimeError: 
Unknown attribute to named tuple:
at container.py:91:23
    def has(self, name: str) -> bool:
        """
        Returns:
            bool: whether the field called `name` exists.
        """
        return name in self.fields._fields
                       ~~~~~~~~~~~~~~~~~~~~ <--- HERE

We have not bound _fields into NamedTuples. If you file a feature request on GitHub we can take a look at implementing it :slight_smile:

1 Like