Skip to content

Commit

Permalink
lint/type
Browse files Browse the repository at this point in the history
  • Loading branch information
mscuthbert committed Sep 18, 2024
1 parent 65b898b commit fcb49fd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
17 changes: 14 additions & 3 deletions music21/meter/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
46 changes: 20 additions & 26 deletions music21/meter/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ def duration(self):
>>> d.quarterLength
1.5
'''

if self._overriddenDuration:
return self._overriddenDuration
else:
Expand Down Expand Up @@ -414,21 +413,21 @@ 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
# on the sum of its component parts.
# 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)
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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}')
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions music21/meter/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class MeterTerminalTuple(t.NamedTuple):
denominator: int
division: MeterDivision


NumDenom = tuple[int, int]
NumDenomTuple = tuple[NumDenom, ...]
MeterOptions = tuple[tuple[str, ...], ...]
Expand Down

0 comments on commit fcb49fd

Please sign in to comment.