Repository: numpy/numpy
File: numpy/lib/_npyio_impl.py (Python)

Code before:
        """
        Close the file.

        """
        if self.zip is not None:
            self.zip.close()
            self.zip = None
        if self.fid is not None:
            self.fid.close()
            self.fid = None
        self.f = None  # break reference cycle

    def __del__(self):
        self.close()

    # Implement the Mapping ABC
    def __iter__(self):
        return iter(self.files)

    def __len__(self):
        return len(self.files)

    def __getitem__(self, key):
        # FIXME: This seems like it will copy strings around
        #   more than is strictly necessary.  The zipfile
        #   will read the string and then
        #   the format.read_array will copy the string
        #   to another place in memory.
        #   It would be better if the zipfile could read
        #   (or at least uncompress) the data
        #   directly into the array memory.
        member = False
        if key in self._files:
            member = True
        elif key in self.files:
            member = True
            key += '.npy'
        if member:
            bytes = self.zip.open(key)
            magic = bytes.read(len(format.MAGIC_PREFIX))
            bytes.close()
            if magic == format.MAGIC_PREFIX:
                bytes = self.zip.open(key)
                return format.read_array(bytes,
                                         allow_pickle=self.allow_pickle,
                                         pickle_kwargs=self.pickle_kwargs,
                                         max_header_size=self.max_header_size)
            else:
                return self.zip.read(key)
        else:
            raise KeyError(f"{key} is not a file in the archive")

Review comment (suggestion):
Thanks for making this a context manager use also.  Might have omitted the `else:` myself here, but I think that is just personal taste.  No need for new tests here I would agree.

Code after:
        """
        if self.zip is not None:
            self.zip.close()
            self.zip = None
        if self.fid is not None:
            self.fid.close()
            self.fid = None
        self.f = None  # break reference cycle

    def __del__(self):
        self.close()

    # Implement the Mapping ABC
    def __iter__(self):
        return iter(self.files)

    def __len__(self):
        return len(self.files)

    def __getitem__(self, key):
        try:
            key = self._files[key]
        except KeyError:
            raise KeyError(f"{key} is not a file in the archive") from None
        else:
            with self.zip.open(key) as bytes:
                magic = bytes.read(len(format.MAGIC_PREFIX))
                bytes.seek(0)
                if magic == format.MAGIC_PREFIX:
                    # FIXME: This seems like it will copy strings around
                    #   more than is strictly necessary.  The zipfile
                    #   will read the string and then
                    #   the format.read_array will copy the string
                    #   to another place in memory.
                    #   It would be better if the zipfile could read
                    #   (or at least uncompress) the data
                    #   directly into the array memory.
                    return format.read_array(
                        bytes,
                        allow_pickle=self.allow_pickle,
                        pickle_kwargs=self.pickle_kwargs,
                        max_header_size=self.max_header_size
                    )
                else:
                    return bytes.read(key)

    def __contains__(self, key):
        return (key in self._files)

    def __repr__(self):
        # Get filename or default to `object`