Skip to content

Commit

Permalink
Fix the TypeError when import.set_fields is provided non-stri…
Browse files Browse the repository at this point in the history
…ng values (#5495)

Fixes #4840 by converting input values to strings before they are used.
Modifies the test cases for ``set_fields`` to appropriately test this
behavior.
  • Loading branch information
snejus authored Nov 11, 2024
2 parents fa10dcf + a98cf47 commit 9345103
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions beets/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ def set_fields(self, lib):
"""
items = self.imported_items()
for field, view in config["import"]["set_fields"].items():
value = view.get()
value = str(view.get())
log.debug(
"Set field {1}={2} for {0}",
displayable_path(self.paths),
Expand Down Expand Up @@ -1062,7 +1062,7 @@ def set_fields(self, lib):
values, for the singleton item.
"""
for field, view in config["import"]["set_fields"].items():
value = view.get()
value = str(view.get())
log.debug(
"Set field {1}={2} for {0}",
displayable_path(self.paths),
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Bug fixes:
that it no longer treats "and" and "or" queries the same. To maintain
previous behaviour add commas between your query keywords. For help see
:ref:`combiningqueries`.
* Fix the ``TypeError`` when :ref:`set_fields` is provided non-string values. :bug:`4840`

For packagers:

Expand Down
8 changes: 8 additions & 0 deletions test/test_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,13 @@ def test_import_single_files(self):
def test_set_fields(self):
genre = "\U0001f3b7 Jazz"
collection = "To Listen"
disc = 0

config["import"]["set_fields"] = {
"collection": collection,
"genre": genre,
"title": "$title - formatted",
"disc": disc,
}

# As-is item import.
Expand All @@ -412,6 +414,7 @@ def test_set_fields(self):
assert item.genre == genre
assert item.collection == collection
assert item.title == "Tag Track 1 - formatted"
assert item.disc == disc
# Remove item from library to test again with APPLY choice.
item.remove()

Expand All @@ -426,6 +429,7 @@ def test_set_fields(self):
assert item.genre == genre
assert item.collection == collection
assert item.title == "Applied Track 1 - formatted"
assert item.disc == disc


class ImportTest(ImportTestCase):
Expand Down Expand Up @@ -583,12 +587,14 @@ def test_set_fields(self):
genre = "\U0001f3b7 Jazz"
collection = "To Listen"
comments = "managed by beets"
disc = 0

config["import"]["set_fields"] = {
"genre": genre,
"collection": collection,
"comments": comments,
"album": "$album - formatted",
"disc": disc,
}

# As-is album import.
Expand All @@ -608,6 +614,7 @@ def test_set_fields(self):
item.get("album", with_album=False)
== "Tag Album - formatted"
)
assert item.disc == disc
# Remove album from library to test again with APPLY choice.
album.remove()

Expand All @@ -629,6 +636,7 @@ def test_set_fields(self):
item.get("album", with_album=False)
== "Applied Album - formatted"
)
assert item.disc == disc


class ImportTracksTest(ImportTestCase):
Expand Down

0 comments on commit 9345103

Please sign in to comment.