Skip to content

Commit 62b0bf4

Browse files
authored
Merge pull request #1745 from cuthbertLab/2025-jan-misc
Astroid 1015 + misc improvements jan
2 parents 851e674 + 1097e77 commit 62b0bf4

File tree

17 files changed

+153
-156
lines changed

17 files changed

+153
-156
lines changed

music21/base.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ def getOffsetBySite(
930930
*,
931931
returnSpecial: t.Literal[False] = False,
932932
) -> OffsetQL:
933-
return 0.0 # dummy until Astroid #1015 is fixed. Replace with ...
933+
...
934934

935935
@overload
936936
def getOffsetBySite(
@@ -939,8 +939,7 @@ def getOffsetBySite(
939939
*,
940940
returnSpecial: bool = False,
941941
) -> OffsetQL|OffsetSpecial:
942-
return 0.0 # dummy until Astroid #1015 is fixed. Replace with ...
943-
# using bool instead of t.Literal[True] because of other errors
942+
...
944943

945944
def getOffsetBySite(
946945
self,
@@ -1332,7 +1331,7 @@ def getContextByClass(
13321331
followDerivation=True,
13331332
priorityTargetOnly=False,
13341333
) -> _M21T|None:
1335-
return None # until Astroid #1015
1334+
...
13361335

13371336
@overload
13381337
def getContextByClass(
@@ -1344,7 +1343,7 @@ def getContextByClass(
13441343
followDerivation=True,
13451344
priorityTargetOnly=False,
13461345
) -> Music21Object|None:
1347-
return None # until Astroid #1015
1346+
...
13481347

13491348
def getContextByClass(
13501349
self,
@@ -1830,7 +1829,7 @@ def contextSites(
18301829
followDerivation=True,
18311830
priorityTargetOnly=False,
18321831
) -> Generator[ContextSortTuple, None, None]:
1833-
pass
1832+
...
18341833

18351834
@overload
18361835
def contextSites(
@@ -1845,8 +1844,7 @@ def contextSites(
18451844
followDerivation=True,
18461845
priorityTargetOnly=False,
18471846
) -> Generator[ContextTuple, None, None]:
1848-
pass
1849-
1847+
...
18501848

18511849
def contextSites(
18521850
self,

music21/chord/__init__.py

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ def annotateIntervals(
11131113
sortPitches: bool = True,
11141114
returnList: t.Literal[True]
11151115
) -> list[str]:
1116-
pass
1116+
...
11171117

11181118
@overload
11191119
def annotateIntervals(
@@ -1124,7 +1124,7 @@ def annotateIntervals(
11241124
sortPitches: bool = True,
11251125
returnList: t.Literal[False] = False
11261126
) -> None:
1127-
pass
1127+
...
11281128

11291129
@overload
11301130
def annotateIntervals(
@@ -1135,7 +1135,7 @@ def annotateIntervals(
11351135
sortPitches: bool = True,
11361136
returnList: t.Literal[False] = False
11371137
) -> _ChordType:
1138-
pass
1138+
...
11391139

11401140
def annotateIntervals(
11411141
self: _ChordType,
@@ -1271,29 +1271,32 @@ def areZRelations(self: _ChordType, other: _ChordType) -> bool:
12711271
return False
12721272

12731273
@overload
1274-
def bass(self,
1275-
newbass: None = None,
1276-
*,
1277-
find: bool|None = None,
1278-
allow_add: bool = False,
1279-
) -> pitch.Pitch:
1280-
return self.pitches[0] # dummy until Astroid 1015 is fixed.
1274+
def bass(
1275+
self,
1276+
newbass: None = None,
1277+
*,
1278+
find: bool|None = None,
1279+
allow_add: bool = False,
1280+
) -> pitch.Pitch:
1281+
...
12811282

12821283
@overload
1283-
def bass(self,
1284-
newbass: str|pitch.Pitch|note.Note,
1285-
*,
1286-
find: bool|None = None,
1287-
allow_add: bool = False,
1288-
) -> None:
1289-
return None
1284+
def bass(
1285+
self,
1286+
newbass: str|pitch.Pitch|note.Note,
1287+
*,
1288+
find: bool|None = None,
1289+
allow_add: bool = False,
1290+
) -> None:
1291+
...
12901292

1291-
def bass(self,
1292-
newbass: None|str|pitch.Pitch|note.Note = None,
1293-
*,
1294-
find: bool|None = None,
1295-
allow_add: bool = False,
1296-
) -> pitch.Pitch|None:
1293+
def bass(
1294+
self,
1295+
newbass: None|str|pitch.Pitch|note.Note = None,
1296+
*,
1297+
find: bool|None = None,
1298+
allow_add: bool = False,
1299+
) -> pitch.Pitch|None:
12971300
'''
12981301
Generally used to find and return the bass Pitch:
12991302
@@ -1481,12 +1484,11 @@ def canBeTonic(self) -> bool:
14811484
def closedPosition(
14821485
self: _ChordType,
14831486
*,
1484-
forceOctave: int|None,
1487+
forceOctave: int|None = None,
14851488
inPlace: t.Literal[True],
1486-
leaveRedundantPitches=False
1489+
leaveRedundantPitches: bool = False
14871490
) -> None:
1488-
# astroid 1003
1489-
return None
1491+
...
14901492

14911493
@overload
14921494
def closedPosition(
@@ -1496,8 +1498,7 @@ def closedPosition(
14961498
inPlace: t.Literal[False] = False,
14971499
leaveRedundantPitches: bool = False
14981500
) -> _ChordType:
1499-
# astroid 1003
1500-
return self
1501+
...
15011502

15021503
def closedPosition(
15031504
self: _ChordType,
@@ -2298,7 +2299,7 @@ def inversion(
22982299
testRoot: pitch.Pitch|None = None,
22992300
transposeOnSet: bool = True
23002301
) -> None:
2301-
return None # dummy until Astroid 1015 is fixed
2302+
...
23022303

23032304
@overload
23042305
def inversion(
@@ -2309,7 +2310,7 @@ def inversion(
23092310
testRoot: pitch.Pitch|None = None,
23102311
transposeOnSet: bool = True
23112312
) -> int:
2312-
return -1 # dummy until Astroid 1015 is fixed
2313+
...
23132314

23142315
def inversion(
23152316
self,
@@ -3799,15 +3800,15 @@ def root(self,
37993800
*,
38003801
find: bool|None = None
38013802
) -> pitch.Pitch:
3802-
return self.pitches[0] # dummy until Astroid 1015 is fixed.
3803+
...
38033804

38043805
@overload
38053806
def root(self,
38063807
newroot: str|pitch.Pitch|note.Note,
38073808
*,
38083809
find: bool|None = None
38093810
) -> None:
3810-
return None # dummy until Astroid 1015 is fixed.
3811+
...
38113812

38123813
def root(self,
38133814
newroot: None|str|pitch.Pitch|note.Note = None,

music21/common/enums.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,17 @@ def __repr__(self):
9898

9999
class StrEnum(str, Enum, metaclass=StrEnumMeta):
100100
'''
101-
An enumeration where strings can equal the value.
101+
An enumeration where strings can equal the value, and strings
102+
can be found "in" the enum.
102103
103104
See :class:`music21.common.enums.OffsetSpecial` for an
104-
example of how these work.
105+
example of how subclassing this would work.
106+
107+
* Note: This class predates the equivalent StrEnum in Python 3.11
108+
and the changes to Enum `__contains__` in 3.12. Once
109+
Python 3.12 is the minimum version of music21, this class
110+
will no longer be used internally and will eventually become
111+
deprecated (2027?) and removed (2030?).
105112
'''
106113
def __repr__(self):
107114
return f'<{self.__class__.__name__}.{self.name}>'
@@ -139,8 +146,6 @@ class OffsetSpecial(StrEnum):
139146
140147
The enum `AT_END` is equal to the string 'highestTime'
141148
142-
In version 9, the string comparisons will be removed.
143-
144149
>>> from music21.common.enums import OffsetSpecial
145150
>>> OffsetSpecial.AT_END
146151
<OffsetSpecial.AT_END>
@@ -156,6 +161,13 @@ class OffsetSpecial(StrEnum):
156161
'highestTime'
157162
158163
* New in v7.
164+
* Note -- a previous note said that the 'highestTime' == OffsetSpecial.AT_END
165+
would be removed in v9 or an upcoming music21 release. Since then, Python has
166+
changed direction and in 3.11 added StrEnum to the standard library and in 3.12
167+
allows for containment checks of strings in StrEnum (such as
168+
`'lowestOffset' in OffsetSpecial` returning True). Therefore there is no
169+
reason for music21 to ever remove this valuable and backwards compatible
170+
tool.
159171
'''
160172
AT_END = 'highestTime'
161173
LOWEST_OFFSET = 'lowestOffset'

music21/common/numberTools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,11 @@ def _preFracLimitDenominator(n: int, d: int) -> tuple[int, int]:
238238

239239
@overload
240240
def opFrac(num: int) -> float:
241-
pass
241+
...
242242

243243
@overload
244244
def opFrac(num: float|Fraction) -> float|Fraction:
245-
pass
245+
...
246246

247247
# no type checking due to accessing protected attributes (for speed)
248248
def opFrac(num: OffsetQLIn) -> OffsetQL:

music21/common/pathTools.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,22 @@ def relativepath(path: StrOrPath, start: str|None = None) -> StrOrPath|str:
154154
@overload
155155
def cleanpath(path: pathlib.Path, *,
156156
returnPathlib: t.Literal[None] = None) -> pathlib.Path:
157-
return pathlib.Path('/') # dummy until Astroid #1015 is fixed.
157+
...
158158

159159
@overload
160160
def cleanpath(path: str, *,
161161
returnPathlib: t.Literal[None] = None) -> str:
162-
return '/' # dummy until Astroid #1015 is fixed.
162+
...
163163

164164
@overload
165165
def cleanpath(path: str|pathlib.Path, *,
166166
returnPathlib: t.Literal[True]) -> pathlib.Path:
167-
return pathlib.Path('/') # dummy until Astroid #1015 is fixed.
167+
...
168168

169169
@overload
170170
def cleanpath(path: str|pathlib.Path, *,
171171
returnPathlib: t.Literal[False]) -> str:
172-
return '/' # dummy until Astroid #1015 is fixed.
172+
...
173173

174174
def cleanpath(path: str|pathlib.Path, *,
175175
returnPathlib: bool|None = None) -> str|pathlib.Path:

music21/environment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,11 +1004,11 @@ def getSettingsPath(self):
10041004

10051005
@overload
10061006
def getTempFile(self, suffix: str, returnPathlib: t.Literal[False]) -> str:
1007-
return '/' # astroid #1015
1007+
...
10081008

10091009
@overload
10101010
def getTempFile(self, suffix: str = '', returnPathlib: t.Literal[True] = True) -> pathlib.Path:
1011-
return pathlib.Path('/') # astroid #1015
1011+
...
10121012

10131013
def getTempFile(self, suffix: str = '', returnPathlib=True) -> str|pathlib.Path:
10141014
'''

music21/graph/axis.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import collections
2020
import math
2121
import re
22+
import typing as t
2223
import unittest
2324

2425
from music21.graph.utilities import accidentalLabelToUnicode, GraphException
@@ -35,6 +36,10 @@
3536
from music21.analysis import pitchAnalysis
3637

3738

39+
if t.TYPE_CHECKING:
40+
from music21 import note
41+
42+
3843
USE_GRACE_NOTE_SPACING = -1
3944

4045

@@ -180,7 +185,7 @@ def stream(self):
180185
else:
181186
return c.streamObj
182187

183-
def extractOneElement(self, n, formatDict):
188+
def extractOneElement(self, n: note.GeneralNote, formatDict: dict[str, t.Any]) -> t.Any:
184189
'''
185190
Override in subclasses
186191
'''
@@ -319,7 +324,7 @@ def __init__(self, client=None, axisName='x'):
319324
self.hideUnused = True
320325

321326
@staticmethod
322-
def makePitchLabelsUnicode(ticks):
327+
def makePitchLabelsUnicode(ticks: list[tuple[t.Any, str]]) -> list[tuple[t.Any, str]]:
323328
# noinspection PyShadowingNames
324329
'''
325330
Given a list of ticks, replace all labels with alternative/unicode symbols where necessary.
@@ -452,9 +457,10 @@ def __init__(self, client=None, axisName='x'):
452457
self.minValue = 0
453458
self.maxValue = 11
454459

455-
def extractOneElement(self, n, formatDict):
460+
def extractOneElement(self, n, formatDict) -> int|None:
456461
if hasattr(n, 'pitch'):
457462
return n.pitch.pitchClass
463+
return None
458464

459465
def ticks(self):
460466
'''

music21/graph/plot.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,8 @@ def postProcessData(self):
11381138
# sort these tuples, ignoring unhashable dict.
11391139
v.sort(key=lambda point: (point[0], point[1]))
11401140

1141+
1142+
# seen_numericValues = set()
11411143
for numericValue, label in yTicks:
11421144
# make sure there is an entry for each yTick, regardless
11431145
# of whether we have any data for it or not.
@@ -1147,6 +1149,18 @@ def postProcessData(self):
11471149
dictOfFormatDicts[numericValue]])
11481150
else:
11491151
newData.append([label, [], {}])
1152+
# seen_numericValues.add(numericValue)
1153+
1154+
# # now find anything in pitchSpanDict that wasn't in the yTicks, for
1155+
# # instance, microtones!
1156+
# for numericValue, data_triplet in pitchSpanDict.items():
1157+
# if numericValue not in seen_numericValues:
1158+
# newData.append([
1159+
# '', # no label
1160+
# data_triplet,
1161+
# dictOfFormatDicts[numericValue],
1162+
# ])
1163+
11501164
self.data = newData
11511165

11521166

0 commit comments

Comments
 (0)