From f3fac08d4aa0e3d7b7dddb9a1b756fbdcab93750 Mon Sep 17 00:00:00 2001 From: Michael Scott Asato Cuthbert Date: Tue, 17 Sep 2024 17:25:18 -1000 Subject: [PATCH] mypy --- music21/meter/base.py | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/music21/meter/base.py b/music21/meter/base.py index db971662f..ce280bd5a 100644 --- a/music21/meter/base.py +++ b/music21/meter/base.py @@ -985,7 +985,8 @@ def beatDivisionCount(self) -> int: return 1 # need to see if first-level subdivisions are partitioned - if not isinstance(self.beatSequence[0], MeterSequence): + beat_seq_0 = self.beatSequence[0] + if not isinstance(beat_seq_0, MeterSequence): return 1 # getting length here gives number of subdivisions @@ -994,7 +995,7 @@ def beatDivisionCount(self) -> int: # convert this to a set; if length is 1, then all beats are uniform if len(set(post)) == 1: - return len(self.beatSequence[0]) # all are the same + return len(beat_seq_0) # all are the same else: return 1 @@ -1054,18 +1055,40 @@ def beatDivisionDurations(self) -> list[duration.Duration]: Value returned of non-uniform beat divisions will change at any time after v7.1 to avoid raising an exception. + + OMIT_FROM_DOCS + + Previously a time signature with beatSequence containing only + MeterTerminals would raise exceptions. + + >>> ts = meter.TimeSignature('2/128') + >>> ts.beatSequence[0] + + >>> ts.beatDivisionDurations + [] + + >>> ts = meter.TimeSignature('1/128') + >>> ts.beatSequence[0] + + >>> ts.beatDivisionDurations + [] ''' post = [] - if len(self.beatSequence) == 1: - raise TimeSignatureException( - 'cannot determine beat division for a non-partitioned beat') for mt in self.beatSequence: - for subMt in mt: - post.append(subMt.duration.quarterLength) + if isinstance(mt, MeterSequence): + for subMt in mt: + post.append(subMt.duration.quarterLength) + else: + post.append(mt.duration.quarterLength) if len(set(post)) == 1: # all the same out = [] - for subMt in self.beatSequence[0]: - out.append(subMt.duration) + beat_seq_0 = self.beatSequence[0] + if isinstance(beat_seq_0, MeterSequence): + for subMt in beat_seq_0: + if subMt.duration is not None: # should not be: + out.append(subMt.duration) + elif beat_seq_0.duration is not None: # MeterTerminal w/ non-empty duration. + out.append(beat_seq_0.duration) return out else: raise TimeSignatureException(f'non uniform beat division: {post}')