Skip to content

Commit

Permalink
Fix strict type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kernitus committed Oct 29, 2023
1 parent 3be815a commit f7aa52c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ jobs:
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Type checking with mypy
run: mypy --ignore-missing-imports --strict .
run: mypy --ignore-missing-imports --strict beetsplug
- name: Test with unittest
run: python -m unittest discover -s tests
18 changes: 12 additions & 6 deletions beetsplug/date_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import datetime
from typing import Optional
from typing import Optional, TypeVar

from dateutil import parser

Expand All @@ -12,7 +12,7 @@ class DateWrapper(datetime.datetime):
"""

def __new__(cls, y: Optional[int] = None, m: Optional[int] = None, d: Optional[int] = None,
iso_string: Optional[str] = None):
iso_string: Optional[str] = None) -> DateWrapper:
"""
Create a new datetime object using a convenience wrapper.
Must specify at least one of either year or iso_string.
Expand All @@ -37,12 +37,12 @@ def __new__(cls, y: Optional[int] = None, m: Optional[int] = None, d: Optional[i
return datetime.datetime.__new__(cls, year, month, day)

@classmethod
def today(cls):
def today(cls) -> DateWrapper:
today = datetime.date.today()
return DateWrapper(today.year, today.month, today.day)

def __init__(self, y: Optional[int] = None, m: Optional[int] = None, d: Optional[int] = None,
iso_string: Optional[str] = None):
iso_string: Optional[str] = None) -> None:
if y is not None:
self.y = min(max(y, datetime.MINYEAR), datetime.MAXYEAR)
self.m = m if (m is None or 0 < m <= 12) else 1
Expand Down Expand Up @@ -74,7 +74,10 @@ def __init__(self, y: Optional[int] = None, m: Optional[int] = None, d: Optional
else:
raise TypeError("Must specify a value for year or a date string")

def __lt__(self, other):
def __lt__(self, other: datetime.date) -> bool:
if not isinstance(other, DateWrapper):
return NotImplemented

if self.y != other.y:
return self.y < other.y
elif self.m is None:
Expand All @@ -93,7 +96,10 @@ def __lt__(self, other):
else:
return self.m < other.m

def __eq__(self, other):
def __eq__(self, other: object) -> bool:
if not isinstance(other, DateWrapper):
return NotImplemented

if self.y != other.y:
return False
elif self.m is not None and other.m is not None:
Expand Down
14 changes: 8 additions & 6 deletions beetsplug/oldestdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

# Type alias
Recording = dict[str, Any]
Work = dict[str, Any]


def _get_work_id_from_recording(recording: Recording) -> Optional[str]:
Expand Down Expand Up @@ -70,16 +71,17 @@ def _is_cover(recording: Recording) -> bool:
return False


def _fetch_work(work_id: str) -> Recording:
def _fetch_work(work_id: str) -> dict[str, Any]:
"""Fetch work, including recording relations"""
return musicbrainzngs.get_work_by_id(work_id, ['recording-rels'])['work']
work: Work = musicbrainzngs.get_work_by_id(work_id, ['recording-rels'])['work']
return work


class OldestDatePlugin(BeetsPlugin):
class OldestDatePlugin(BeetsPlugin): # type: ignore
_importing: bool = False
_recordings_cache: dict[str, Recording] = dict()

def __init__(self):
def __init__(self) -> None:
super(OldestDatePlugin, self).__init__()
self.import_stages = [self._on_import]
self.config.add({
Expand Down Expand Up @@ -122,7 +124,7 @@ def __init__(self):
mediafile.StorageStyle(recording_field))
self.add_media_field(recording_field, field)

def commands(self):
def commands(self) -> list[ui.Subcommand]:
recording_date_command = ui.Subcommand(
'oldestdate',
help="Retrieve the date of the oldest known recording or release of a track.",
Expand Down Expand Up @@ -240,7 +242,7 @@ def _process_file(self, item: Item) -> None:

def _fetch_recording(self, recording_id: str) -> Recording:
"""Fetch and cache recording from MusicBrainz, including releases and work relations"""
recording = musicbrainzngs.get_recording_by_id(recording_id, ['artists', 'releases', 'work-rels'])['recording']
recording: Recording = musicbrainzngs.get_recording_by_id(recording_id, ['artists', 'releases', 'work-rels'])['recording']
self._recordings_cache[recording_id] = recording
return recording

Expand Down

0 comments on commit f7aa52c

Please sign in to comment.