Reduce cognitive complexity and improve maintainability across the codebase
Overview
This PR reduces cognitive complexity across the codebase by refactoring the 4 most complex methods identified by flake8's complexity checker. All methods now comply with the project's max-complexity=7 standard while maintaining 100% behavioral compatibility.
Motivation
High cognitive complexity makes code harder to understand, test, and maintain. The following methods exceeded the complexity threshold:
-
Helpers.get_genre(complexity 9) -
Metaguru.albumtypes(complexity 9) -
Track.parse_name(complexity 8) -
Tracks.fix_track_artists(complexity 10)
Changes
1. Helpers.get_genre (9 → 4, -56%)
Extracted nested functions into reusable class methods:
-
_is_valid_mb_genre- Checks if keyword is a valid MusicBrainz genre -
_validate_genre_for_mode- Validates genre according to configured mode (classical/progressive/psychedelic) -
_is_genre_within_another- Filters sub-genres when parent genre exists
2. Metaguru.albumtypes (9 → 3, -67%)
Separated albumtype determination into focused helper methods:
-
_add_compilation_types- Adds compilation-related albumtypes -
_add_format_types- Adds LP and single format types -
_add_special_types- Adds remix, live, soundtrack types
3. Track.parse_name (8 → 2, -75%)
Extracted parsing steps into dedicated methods:
-
_extract_track_alt- Extracts track alternatives (e.g., 'A1', 'B2') -
_extract_catalognum- Extracts catalog numbers from track names -
_extract_remix- Extracts and processes remix information
4. Tracks.fix_track_artists (10 → 3, -70%)
Decomposed complex artist fixing logic:
-
_fix_the_artist- Handles edge case where artist is 'the' -
_should_move_artist_to_title- Validates whether artist should be moved to title -
_fix_misidentified_artists- Corrects artists incorrectly parsed from titles -
_try_split_title_to_artist- Attempts to split title into artist and title parts
Impact
Total Complexity Reduction: -24 points (67% average reduction)
Benefits:
- ✅ Improved Maintainability - Smaller, focused methods are easier to understand
- ✅ Better Testability - Extracted methods can be tested independently
- ✅ Enhanced Readability - Descriptive method names document intent
- ✅ Reduced Risk - Fewer code paths and edge cases per method
- ✅ Easier Debugging - Isolated logic simplifies troubleshooting
Testing
- All 370 existing tests pass
- No behavioral changes introduced
- All genre, albumtype, track parsing, and artist fixing tests validated
- Flake8 complexity checks pass for all files
Code Quality
- Removed unused imports (
functools.partial,operator.contains) - Fixed whitespace and line length issues
- All methods now meet
max-complexity=7threshold - Clean separation of concerns with well-named helper methods
[!WARNING]
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
ute-rec.bandcamp.com
- Triggering command:
python -m pytest tests/ -x --no-cov -q(dns block)If you need me to access, download, or install something from one of these locations, you can either:
- Configure Actions setup steps to set up my environment, which run before the firewall is enabled
- Add the appropriate URLs or hosts to the custom allowlist in this repository's Copilot coding agent settings (admins only)
Original prompt
Reduce cognitive complexity and improve maintainability across the entire codebase
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
Pull Request Test Coverage Report for Build 18166458457
Details
- 88 of 95 (92.63%) changed or added relevant lines in 4 files are covered.
- No unchanged relevant lines lost coverage.
- Overall coverage increased (+0.3%) to 88.568%
| Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
|---|---|---|---|
| beetsplug/bandcamp/metaguru.py | 21 | 22 | 95.45% |
| beetsplug/bandcamp/track.py | 23 | 24 | 95.83% |
| beetsplug/bandcamp/tracks.py | 20 | 25 | 80.0% |
| <!-- | Total: | 88 | 95 |
| Totals | |
|---|---|
| Change from base Build 17880031297: | 0.3% |
| Covered Lines: | 1270 |
| Relevant Lines: | 1396 |