diff --git a/music21/instrument.py b/music21/instrument.py index 25abce154..43e2c7e45 100644 --- a/music21/instrument.py +++ b/music21/instrument.py @@ -200,7 +200,7 @@ def __deepcopy__(self, memo=None): new.instrumentIdRandomize() return new - def bestName(self): + def bestName(self) -> str|None: ''' Find a viable name, looking first at instrument, then part, then abbreviations. diff --git a/music21/musicxml/m21ToXml.py b/music21/musicxml/m21ToXml.py index a06cb2c6a..aaa6efb7f 100644 --- a/music21/musicxml/m21ToXml.py +++ b/music21/musicxml/m21ToXml.py @@ -1399,7 +1399,7 @@ def __init__(self, score: stream.Score|None = None, makeNotation: bool = True): self.instrumentList: list[instrument.Instrument] = [] self.instrumentIdList: list[str|None] = [] - self.midiChannelList: list[int|None] = [] + self.midiChannelList: list[int|None] = [] # TODO: just list[int]? self.parts: list[stream.Part] = [] @@ -2596,6 +2596,8 @@ class PartExporter(XMLExporterBase): in the :class:`~music21.layout.StaffGroup`, if any. (E.g. if this is the left hand, store a reference to the right hand.)''', } + midiChannelList: list[int|None] # TODO: just list[int]? + meterStream: stream.Stream[meter.TimeSignatureBase] def __init__(self, partObj: stream.Part|stream.Score|None = None, @@ -2611,14 +2613,15 @@ def __init__(self, self.xmlRoot = Element('part') if parent is None: - self.meterStream: stream.Stream[meter.TimeSignatureBase] = stream.Stream() + self.meterStream = stream.Stream[meter.TimeSignatureBase]() self.refStreamOrTimeRange = [0.0, 0.0] self.midiChannelList = [] self.makeNotation = True else: + # else should not be executed. self.meterStream = (parent.meterStream if parent.meterStream is not None - else stream.Stream()) # else should not be executed. + else stream.Stream[meter.TimeSignatureBase]()) self.refStreamOrTimeRange = parent.refStreamOrTimeRange self.midiChannelList = parent.midiChannelList # shared list self.makeNotation = parent.makeNotation @@ -2626,7 +2629,7 @@ def __init__(self, self.previousPartStaffInGroup: stream.PartStaff|None = None self.instrumentStream: stream.Stream[instrument.Instrument]|None = None - self.firstInstrumentObject = None + self.firstInstrumentObject: instrument.Instrument|None = None # keep track of this so that we only put out new attributes when something # has changed @@ -2737,16 +2740,19 @@ def instrumentSetup(self): else: # get a default instrument if not assigned self.instrumentStream = self.stream.getInstruments(returnDefault=True, recurse=True) - self.firstInstrumentObject = self.instrumentStream[0] # store first, as handled differently + firstInstrumentObject = self.instrumentStream[0] # store first, as handled differently + if t.TYPE_CHECKING: + assert isinstance(firstInstrumentObject, instrument.Instrument) + self.firstInstrumentObject = firstInstrumentObject if self.parent is not None: instIdList = [x.partId for x in self.parent.instrumentList] else: instIdList = [self.stream.id] - firstInstId = self.firstInstrumentObject.partId + firstInstId = firstInstrumentObject.partId if firstInstId in instIdList or firstInstId is None: # must have unique ids - self.firstInstrumentObject.partIdRandomize() # set new random id + firstInstrumentObject.partIdRandomize() # set new random id should_short_circuit = self.mergeInstrumentStreamPartStaffAware() if should_short_circuit: @@ -2769,6 +2775,7 @@ def instrumentSetup(self): # this is shared among all PartExporters, so long as they are created by a # ScoreExporter + # TODO: skip if .midiChannel is None? self.midiChannelList.append(thisInstrument.midiChannel) # environLocal.printDebug(['midiChannel list', self.midiChannelList]) @@ -2781,7 +2788,7 @@ def instrumentSetup(self): # add to the lists for checking on next part if self.parent is not None: self.parent.instrumentIdList.append(thisInstrument.instrumentId) - if thisInstrument is self.firstInstrumentObject: + if thisInstrument is firstInstrumentObject: self.parent.instrumentList.append(thisInstrument) def mergeInstrumentStreamPartStaffAware(self) -> bool: