diff --git a/beets/library.py b/beets/library.py index fbd0893a7..7200061f6 100644 --- a/beets/library.py +++ b/beets/library.py @@ -550,7 +550,7 @@ class Item(LibModel): defaults to the item's path. `tags` is a dictionary of additional metadata the should be - written to the file. + written to the file. (These tags need not be in `_media_fields`.) Can raise either a `ReadError` or a `WriteError`. """ @@ -559,17 +559,22 @@ class Item(LibModel): else: path = normpath(path) + # Get the data to write to the file. item_tags = dict(self) + item_tags = {k: v for k, v in item_tags.items() + if k in self._media_fields} # Only write media fields. if tags is not None: item_tags.update(tags) plugins.send('write', item=self, path=path, tags=item_tags) + # Open the file. try: mediafile = MediaFile(syspath(path), id3v23=beets.config['id3v23'].get(bool)) except (OSError, IOError, UnreadableFileError) as exc: raise ReadError(self.path, exc) + # Write the tags to the file. mediafile.update(item_tags) try: mediafile.save() diff --git a/test/test_library.py b/test/test_library.py index c3807637e..a4b4d08a8 100644 --- a/test/test_library.py +++ b/test/test_library.py @@ -1058,6 +1058,14 @@ class WriteTest(unittest.TestCase, TestHelper): self.assertNotEqual(item.artist, 'new artist') self.assertEqual(MediaFile(item.path).artist, 'new artist') + def test_write_date_field(self): + # Since `date` is not a MediaField, this should do nothing. + item = self.add_item_fixture() + clean_year = item.year + item.date = 'foo' + item.write() + self.assertEqual(MediaFile(item.path).year, clean_year) + class ItemReadTest(unittest.TestCase):