Skip to content

Commit 9345103

Browse files
authored
Fix the TypeError when import.set_fields is provided non-string 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.
2 parents fa10dcf + a98cf47 commit 9345103

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

beets/importer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ def set_fields(self, lib):
605605
"""
606606
items = self.imported_items()
607607
for field, view in config["import"]["set_fields"].items():
608-
value = view.get()
608+
value = str(view.get())
609609
log.debug(
610610
"Set field {1}={2} for {0}",
611611
displayable_path(self.paths),
@@ -1062,7 +1062,7 @@ def set_fields(self, lib):
10621062
values, for the singleton item.
10631063
"""
10641064
for field, view in config["import"]["set_fields"].items():
1065-
value = view.get()
1065+
value = str(view.get())
10661066
log.debug(
10671067
"Set field {1}={2} for {0}",
10681068
displayable_path(self.paths),

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Bug fixes:
5151
that it no longer treats "and" and "or" queries the same. To maintain
5252
previous behaviour add commas between your query keywords. For help see
5353
:ref:`combiningqueries`.
54+
* Fix the ``TypeError`` when :ref:`set_fields` is provided non-string values. :bug:`4840`
5455

5556
For packagers:
5657

test/test_importer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,13 @@ def test_import_single_files(self):
395395
def test_set_fields(self):
396396
genre = "\U0001f3b7 Jazz"
397397
collection = "To Listen"
398+
disc = 0
398399

399400
config["import"]["set_fields"] = {
400401
"collection": collection,
401402
"genre": genre,
402403
"title": "$title - formatted",
404+
"disc": disc,
403405
}
404406

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

@@ -426,6 +429,7 @@ def test_set_fields(self):
426429
assert item.genre == genre
427430
assert item.collection == collection
428431
assert item.title == "Applied Track 1 - formatted"
432+
assert item.disc == disc
429433

430434

431435
class ImportTest(ImportTestCase):
@@ -583,12 +587,14 @@ def test_set_fields(self):
583587
genre = "\U0001f3b7 Jazz"
584588
collection = "To Listen"
585589
comments = "managed by beets"
590+
disc = 0
586591

587592
config["import"]["set_fields"] = {
588593
"genre": genre,
589594
"collection": collection,
590595
"comments": comments,
591596
"album": "$album - formatted",
597+
"disc": disc,
592598
}
593599

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

@@ -629,6 +636,7 @@ def test_set_fields(self):
629636
item.get("album", with_album=False)
630637
== "Applied Album - formatted"
631638
)
639+
assert item.disc == disc
632640

633641

634642
class ImportTracksTest(ImportTestCase):

0 commit comments

Comments
 (0)