From fcb49fded4452ffb53d358e33a1b060d3ce21ba5 Mon Sep 17 00:00:00 2001 From: Michael Scott Asato Cuthbert Date: Tue, 17 Sep 2024 15:25:53 -1000 Subject: [PATCH] lint/type --- music21/meter/base.py | 17 +++++++++++++--- music21/meter/core.py | 46 ++++++++++++++++++------------------------ music21/meter/tools.py | 1 + 3 files changed, 35 insertions(+), 29 deletions(-) diff --git a/music21/meter/base.py b/music21/meter/base.py index e8dcdcf4a..1f5eee29f 100644 --- a/music21/meter/base.py +++ b/music21/meter/base.py @@ -1306,7 +1306,11 @@ def _setDefaultAccentWeights(self, depth: int = 3) -> None: # -------------------------------------------------------------------------- # access data for other processing - def getBeams(self, srcList, measureStartOffset=0.0) -> list[beam.Beams|None]: + def getBeams( + self, + srcList: stream.Stream|t.Sequence[base.Music21Object], + measureStartOffset: OffsetQL = 0.0, + ) -> list[beam.Beams|None]: ''' Given a qLen position and an iterable of Music21Objects, return a list of Beams objects. @@ -1406,11 +1410,18 @@ def getBeams(self, srcList, measureStartOffset=0.0) -> list[beam.Beams|None]: beamsList = beam.Beams.naiveBeams(srcList) # hold maximum Beams objects, all with type None beamsList = beam.Beams.removeSandwichedUnbeamables(beamsList) - def fixBeamsOneElementDepth(i, el, depth): + def fixBeamsOneElementDepth(i: int, el: base.Music21Object, depth: int): + ''' + Note that this can compute the beams for non-Note things like rests + they just cannot be applied to the object. + ''' beams = beamsList[i] if beams is None: return + if t.TYPE_CHECKING: + assert isinstance(beams, beam.Beams) + beamNumber = depth + 1 # see if there is a component defined for this beam number # if not, continue @@ -1422,7 +1433,7 @@ def fixBeamsOneElementDepth(i, el, depth): start = opFrac(pos) end = opFrac(pos + dur.quarterLength) - startNext: float|Fraction = end + startNext: OffsetQL = end isLast = (i == len(srcList) - 1) isFirst = (i == 0) diff --git a/music21/meter/core.py b/music21/meter/core.py index 80f9eec15..d71f6a650 100644 --- a/music21/meter/core.py +++ b/music21/meter/core.py @@ -368,7 +368,6 @@ def duration(self): >>> d.quarterLength 1.5 ''' - if self._overriddenDuration: return self._overriddenDuration else: @@ -414,9 +413,9 @@ def __init__( self._numerator: int = 1 # rationalized self._denominator: int = 0 # lowest common multiple - self._partition: list[MeterTerminal] = [] # a list of terminals or MeterSequences - self._overriddenDuration = None - self._levelListCache = {} + self._partition: list[MeterTerminal] = [] # a list of terminals (or MeterSequences?) + self._overriddenDuration: Duration|None = None + self._levelListCache: dict[tuple[int, bool], list[MeterTerminal]] = {} # this attribute is only used in MeterTerminals, and note # in MeterSequences; a MeterSequence's weight is based solely @@ -424,11 +423,11 @@ def __init__( # del self._weight -- no -- screws up pickling -- cannot del a slotted object # Bool stores whether this meter was provided as a summed numerator - self.summedNumerator = False + self.summedNumerator: bool = False # An optional parameter used only in meter display sequences. # Needed in cases where a meter component is parenthetical - self.parenthesis = False + self.parenthesis: bool = False if value is not None: self.load(value, partitionRequest) @@ -751,16 +750,14 @@ def partitionByList(self, numeratorList: Sequence[int] | Sequence[str]) -> None: Traceback (most recent call last): music21.exceptions21.MeterException: Cannot set partition by ['3/4', '1/8', '5/8'] ''' - optMatch = None + optMatch: MeterSequence | None | tuple[str, ...] = None # assume a list of terminal definitions if isinstance(numeratorList[0], str): # TODO: working with private methods of a created MeterSequence test = MeterSequence() - if t.TYPE_CHECKING: - numeratorList = cast(list[str], numeratorList) for mtStr in numeratorList: - test._addTerminal(mtStr) + test._addTerminal(t.cast(str, mtStr)) test._updateRatio() # if durations are equal, this can be used as a partition if self.duration.quarterLength == test.duration.quarterLength: @@ -771,9 +768,10 @@ def partitionByList(self, numeratorList: Sequence[int] | Sequence[str]) -> None: elif sum(t.cast(list[int], numeratorList)) in [self.numerator * x for x in range(1, 9)]: for i in range(1, 9): if sum(t.cast(list[int], numeratorList)) == self.numerator * i: - optMatch = [] + optMatchInner: list[str] = [] for n in numeratorList: - optMatch.append(f'{n}/{self.denominator * i}') + optMatchInner.append(f'{n}/{self.denominator * i}') + optMatch = tuple(optMatchINner) break # last resort: search options @@ -887,7 +885,7 @@ def partition( self.partitionByList(value) elif isinstance(value, MeterSequence): self.partitionByOtherMeterSequence(value) - elif common.isNum(value): + elif isinstance(value, int): self.partitionByCount(value, loadDefault=loadDefault) else: raise MeterException(f'cannot process partition argument {value}') @@ -1203,9 +1201,14 @@ def partitionStr(self): def load(self, value: str | MeterTerminal | Sequence[MeterTerminal | str], - partitionRequest: int | Sequence[str | MeterTerminal] | MeterSequence | None = None, + partitionRequest: (int + | Sequence[str] + | Sequence[MeterTerminal] + | Sequence[int] + | MeterSequence + | None) = None, autoWeight: bool = False, - targetWeight=None): + targetWeight: int|None = None): ''' This method is called when a MeterSequence is created, or if a MeterSequence is re-set. @@ -1384,14 +1387,6 @@ def weight(self, value: int | float | None) -> None: # environLocal.printDebug(['setting weight based on part, total, weight', # partRatio, totalRatio, mt.weight]) - @property - def numerator(self): - return self._numerator - - @property - def denominator(self): - return self._denominator - def _getFlatList(self): ''' Return a flattened version of this @@ -1401,7 +1396,6 @@ def _getFlatList(self): are generally immutable and thus it does not make sense to concatenate them. - >>> a = meter.MeterSequence('3/4') >>> a.partition(3) >>> b = a._getFlatList() @@ -1542,7 +1536,7 @@ def isUniformPartition(self, *, depth=0): # -------------------------------------------------------------------------- # alternative representations - def getLevelList(self, levelCount, flat=True): + def getLevelList(self, levelCount: int, flat: bool = True) -> list[MeterTerminal]: ''' Recursive utility function that gets everything at a certain level. @@ -1572,7 +1566,7 @@ def getLevelList(self, levelCount, flat=True): except KeyError: pass - mtList = [] + mtList: list[MeterTerminal] = [] for i in range(len(self._partition)): # environLocal.printDebug(['getLevelList weight', i, self[i].weight]) if not isinstance(self._partition[i], MeterSequence): diff --git a/music21/meter/tools.py b/music21/meter/tools.py index b77a72e0c..33abacee6 100644 --- a/music21/meter/tools.py +++ b/music21/meter/tools.py @@ -29,6 +29,7 @@ class MeterTerminalTuple(t.NamedTuple): denominator: int division: MeterDivision + NumDenom = tuple[int, int] NumDenomTuple = tuple[NumDenom, ...] MeterOptions = tuple[tuple[str, ...], ...]