Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade mypy to 0.991 #1495

Merged
merged 16 commits into from
Dec 7, 2022
5 changes: 4 additions & 1 deletion .github/workflows/maincheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install wheel
pip install -r requirements.txt
pip install -r requirements_dev.txt
- name: Install music21 in editable mode
Expand All @@ -67,6 +68,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install wheel
pip install -r requirements.txt
pip install -r requirements_dev.txt
- name: PEP8 with flake8
Expand All @@ -81,10 +83,11 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10.6' # pinned until mypy 0.990 is released
python-version: '3.10'
cache: 'pip'
- name: Install dependencies
run: |
pip install wheel
python -m pip install -r requirements.txt
python -m pip install -r requirements_dev.txt
- name: Type-check all modules with mypy
Expand Down
45 changes: 26 additions & 19 deletions music21/abcFormat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class ABCToken(prebase.ProtoM21Object, common.objects.EqualSlottedObjectMixin):
'''
__slots__ = ('src',)

def __init__(self, src=''):
def __init__(self, src: str = ''):
self.src: str = src # store source character sequence

def _reprInternal(self):
Expand Down Expand Up @@ -237,7 +237,7 @@ class ABCMetadata(ABCToken):

# given a logical unit, create an object
# may be a chord, notes, metadata, bars
def __init__(self, src=''):
def __init__(self, src: str = ''):
super().__init__(src)
self.tag: str = ''
self.data: str = ''
Expand Down Expand Up @@ -803,7 +803,7 @@ def __init__(self, src):
self.barStyle = '' # regular, heavy-light, etc
self.repeatForm = '' # end, start, bidrectional, first, second

def parse(self):
def parse(self) -> None:
'''
Assign the bar-type based on the source string.

Expand Down Expand Up @@ -1154,7 +1154,7 @@ class ABCSlurStart(ABCToken):
'''
__slots__ = ('slurObj',)

def __init__(self, src=''):
def __init__(self, src: str = ''):
super().__init__(src)
self.slurObj: spanner.Slur | None = None

Expand Down Expand Up @@ -1183,11 +1183,11 @@ class ABCCrescStart(ABCToken):
'''
__slots__ = ('crescObj',)

def __init__(self, src=''):
def __init__(self, src: str = ''):
super().__init__(src)
self.crescObj: dynamics.Crescendo | None = None

def fillCresc(self):
def fillCresc(self) -> None:
from music21 import dynamics
self.crescObj = dynamics.Crescendo()

Expand All @@ -1199,7 +1199,7 @@ class ABCDimStart(ABCToken):
'''
__slots__ = ('dimObj',)

def __init__(self, src=''):
def __init__(self, src: str = ''):
super().__init__(src)
self.dimObj: dynamics.Diminuendo | None = None

Expand Down Expand Up @@ -1278,9 +1278,9 @@ class ABCBrokenRhythmMarker(ABCToken):
'''
__slots__ = ('data',)

def __init__(self, src=''):
def __init__(self, src: str = ''):
super().__init__(src)
self.data: str | None = None
self.data: str = ''

def preParse(self):
'''
Expand Down Expand Up @@ -1328,26 +1328,26 @@ def __init__(self, src='', carriedAccidental: str = ''):
self.chordSymbols: list[str] = []

# context attributes
self.inBar = None
self.inBeam = None
self.inGrace = None
self.inBar: bool | None = None
self.inBeam: bool | None = None
self.inGrace: bool | None = None

# provide default duration from handler; may change during piece
self.activeDefaultQuarterLength: float | None = None
# store if a broken symbol applies; a pair of symbols, position (left, right)
self.brokenRhythmMarker = None
self.brokenRhythmMarker: tuple[str, str] | None = None

# store key signature for pitch processing; this is an M21Object
self.activeKeySignature = None
self.activeKeySignature: key.KeySignature | None = None

# store a tuplet if active
self.activeTuplet = None
self.activeTuplet: duration.Tuplet | None = None

# store a spanner if active
self.applicableSpanners: list[spanner.Spanner] = []

# store a tie if active
self.tie = None
# store a tie type if active
self.tie: str | None = None

# store articulations if active
self.articulations: list[str] = []
Expand Down Expand Up @@ -2501,7 +2501,7 @@ def tokenize(self, strSrc: str) -> None:
# no action: normal continuation of 1 char
pass

def tokenProcess(self):
def tokenProcess(self) -> None:
'''
Process all token objects. First, calls preParse(), then
does context assignments, then calls parse().
Expand Down Expand Up @@ -2582,6 +2582,9 @@ def tokenProcess(self):
# notes within slur marks need to be added to the spanner
if isinstance(token, ABCSlurStart):
token.fillSlur()
if t.TYPE_CHECKING:
assert token.slurObj is not None

self.activeSpanners.append(token.slurObj)
self.activeParens.append('Slur')
elif isinstance(token, ABCParenStop):
Expand Down Expand Up @@ -2618,11 +2621,15 @@ def tokenProcess(self):

if isinstance(token, ABCCrescStart):
token.fillCresc()
if t.TYPE_CHECKING:
assert token.crescObj is not None
self.activeSpanners.append(token.crescObj)
self.activeParens.append('Crescendo')

if isinstance(token, ABCDimStart):
token.fillDim()
if t.TYPE_CHECKING:
assert token.dimObj is not None
self.activeSpanners.append(token.dimObj)
self.activeParens.append('Diminuendo')

Expand Down Expand Up @@ -3247,7 +3254,7 @@ class ABCHandlerBar(ABCHandler):
# divide elements of a character stream into objects and handle
# store in a list, and pass global information to components

def __init__(self):
def __init__(self) -> None:
# tokens are ABC objects in a linear stream
super().__init__()

Expand Down
15 changes: 7 additions & 8 deletions music21/analysis/discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@
from music21 import key
from music21 import pitch


if t.TYPE_CHECKING:
from music21 import stream


environLocal = environment.Environment('analysis.discrete')


Expand Down Expand Up @@ -392,7 +390,7 @@ def _convoluteDistribution(self, pcDistribution, weightType='major'):
solution[i] += (toneWeights[(j - i) % 12] * pcDistribution[j])
return solution

def _getLikelyKeys(self, keyResults, differences):
def _getLikelyKeys(self, keyResults, differences) -> list[t.Any] | None:
''' Takes in a list of probable key results in points and returns a
list of keys in letters, sorted from most likely to least likely.
'''
Expand All @@ -410,9 +408,10 @@ def _getLikelyKeys(self, keyResults, differences):
# environLocal.printDebug(['added likely key', likelyKeys[pc]])
return likelyKeys

def _getDifference(self, keyResults, pcDistribution, weightType):
''' Takes in a list of numerical probable key results and returns the
difference of the top two keys
def _getDifference(self, keyResults, pcDistribution, weightType) -> None | list[int | float]:
'''
Takes in a list of numerical probable key results and returns the
difference of the top two keys.
'''
# case of empty analysis
if keyResults is None:
Expand Down Expand Up @@ -953,14 +952,14 @@ class Ambitus(DiscreteAnalysis):
# provide possible string matches for this processor
identifiers = ['ambitus', 'span']

def __init__(self, referenceStream=None):
def __init__(self, referenceStream: stream.Stream | None = None):
super().__init__(referenceStream=referenceStream)
# Store the min and max Pitch instances for referenceStream
# set by getPitchSpan(), which is called by _generateColors()
self.minPitchObj: pitch.Pitch | None = None
self.maxPitchObj: pitch.Pitch | None = None

self._pitchSpanColors = OrderedDict()
self._pitchSpanColors: OrderedDict[int, str] = OrderedDict()
self._generateColors()

def _generateColors(self, numColors=None):
Expand Down
2 changes: 1 addition & 1 deletion music21/articulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class Articulation(base.Music21Object):
'''
_styleClass: type[style.Style] = style.TextStyle

def __init__(self, **keywords):
def __init__(self, **keywords) -> None:
super().__init__(**keywords)
self.placement = None
# declare a unit interval shift for the performance of this articulation
Expand Down
2 changes: 1 addition & 1 deletion music21/bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ class Repeat(repeat.RepeatMark, Barline):
{4.0} <music21.bar.Barline type=double>
'''
# _repeatDots = None # not sure what this is for; inherited from old modules
def __init__(self, direction='start', times=None, **keywords):
def __init__(self, direction: str = 'start', times: int | None = None, **keywords):
repeat.RepeatMark.__init__(self)
if direction == 'start':
barType = 'heavy-light'
Expand Down
7 changes: 3 additions & 4 deletions music21/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ def derivation(self) -> Derivation:
def derivation(self, newDerivation: Derivation | None) -> None:
self._derivation = newDerivation

def clearCache(self, **keywords):
def clearCache(self, **keywords) -> None:
'''
A number of music21 attributes (especially with Chords and RomanNumerals, etc.)
are expensive to compute and are therefore cached. Generally speaking
Expand All @@ -879,7 +879,7 @@ def clearCache(self, **keywords):
'''
# do not replace with self._cache.clear() -- leaves terrible
# state for shallow copies.
self._cache: dict[str, t.Any] = {}
self._cache = {}

@overload
def getOffsetBySite(
Expand Down Expand Up @@ -1304,7 +1304,6 @@ def getContextByClass(
) -> Music21Object | None:
return None # until Astroid #1015


def getContextByClass(
self,
className: type[_M21T] | str | None,
Expand Down Expand Up @@ -3315,7 +3314,7 @@ def splitAtQuarterLength(

def splitByQuarterLengths(
self,
quarterLengthList: list[int | float],
quarterLengthList: list[int | float | fractions.Fraction],
addTies=True,
displayTiedAccidentals=False
) -> _SplitTuple:
Expand Down
Loading