Skip to content

Commit fd21b53

Browse files
authored
Merge pull request #1828 from cuthbertLab/t.Self
typing.Self available
2 parents 488c358 + 4523c38 commit fd21b53

File tree

9 files changed

+104
-119
lines changed

9 files changed

+104
-119
lines changed

music21/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ def __init__(self,
548548
if quarterLength is not None:
549549
self.duration.quarterLength = quarterLength
550550

551-
def __eq__(self: _M21T, other) -> t.TypeGuard[_M21T]:
551+
def __eq__(self, other) -> t.TypeGuard[t.Self]:
552552
'''
553553
Define equality for Music21Objects. See main class docs.
554554
'''
@@ -612,10 +612,10 @@ def mergeAttributes(self, other: Music21Object) -> None:
612612
self.id = other.id
613613
self.groups = copy.deepcopy(other.groups)
614614

615-
def _deepcopySubclassable(self: _M21T,
615+
def _deepcopySubclassable(self,
616616
memo: dict[int, t.Any]|None = None,
617617
*,
618-
ignoreAttributes: set[str]|None = None) -> _M21T:
618+
ignoreAttributes: set[str]|None = None) -> t.Self:
619619
'''
620620
Subclassable __deepcopy__ helper so that the same attributes
621621
do not need to be called for each Music21Object subclass.
@@ -657,7 +657,7 @@ def _deepcopySubclassable(self: _M21T,
657657

658658
return new
659659

660-
def __deepcopy__(self: _M21T, memo: dict[int, t.Any]|None = None) -> _M21T:
660+
def __deepcopy__(self, memo: dict[int, t.Any]|None = None) -> t.Self:
661661
'''
662662
Helper method to copy.py's deepcopy function. Call it from there.
663663

music21/chord/__init__.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@
5353

5454
environLocal = environment.Environment('chord')
5555

56-
_ChordBaseType = t.TypeVar('_ChordBaseType', bound='music21.chord.ChordBase')
57-
_ChordType = t.TypeVar('_ChordType', bound='music21.chord.Chord')
58-
5956
# ------------------------------------------------------------------------------
6057
class ChordException(exceptions21.Music21Exception):
6158
pass
@@ -162,7 +159,7 @@ def __eq__(self, other):
162159
def __hash__(self):
163160
return super().__hash__()
164161

165-
def __deepcopy__(self: _ChordBaseType, memo=None) -> _ChordBaseType:
162+
def __deepcopy__(self, memo=None) -> t.Self:
166163
'''
167164
As Chord objects have one or more Volume, objects, and Volume
168165
objects store weak refs to the client object, need to specialize
@@ -1014,11 +1011,11 @@ def _findBass(self) -> pitch.Pitch|None:
10141011
return lowest
10151012

10161013
def _removePitchByRedundantAttribute(
1017-
self: _ChordType,
1014+
self,
10181015
attribute: str,
10191016
*,
10201017
inPlace=False
1021-
) -> _ChordType|list[pitch.Pitch]:
1018+
) -> t.Self|list[pitch.Pitch]:
10221019
'''
10231020
Common method for stripping pitches based on redundancy of one pitch
10241021
attribute. The `attribute` is provided by a string.
@@ -1113,7 +1110,7 @@ def add(
11131110

11141111
@overload
11151112
def annotateIntervals(
1116-
self: _ChordType,
1113+
self,
11171114
*,
11181115
inPlace: bool = False,
11191116
stripSpecifiers: bool = True,
@@ -1124,7 +1121,7 @@ def annotateIntervals(
11241121

11251122
@overload
11261123
def annotateIntervals(
1127-
self: _ChordType,
1124+
self,
11281125
*,
11291126
inPlace: t.Literal[True],
11301127
stripSpecifiers: bool = True,
@@ -1135,23 +1132,23 @@ def annotateIntervals(
11351132

11361133
@overload
11371134
def annotateIntervals(
1138-
self: _ChordType,
1135+
self,
11391136
*,
11401137
inPlace: t.Literal[False] = False,
11411138
stripSpecifiers: bool = True,
11421139
sortPitches: bool = True,
11431140
returnList: t.Literal[False] = False
1144-
) -> _ChordType:
1141+
) -> t.Self:
11451142
...
11461143

11471144
def annotateIntervals(
1148-
self: _ChordType,
1145+
self,
11491146
*,
11501147
inPlace: bool = False,
11511148
stripSpecifiers: bool = True,
11521149
sortPitches: bool = True,
11531150
returnList: bool = False
1154-
) -> _ChordType|None|list[str]:
1151+
) -> t.Self|None|list[str]:
11551152
# noinspection PyShadowingNames
11561153
'''
11571154
Add lyrics to the chord that show the distance of each note from
@@ -1251,7 +1248,7 @@ def annotateIntervals(
12511248
if not inPlace:
12521249
return c
12531250

1254-
def areZRelations(self: _ChordType, other: _ChordType) -> bool:
1251+
def areZRelations(self, other: t.Self) -> bool:
12551252
'''
12561253
Check if another Chord is a z-relation to this Chord.
12571254
@@ -1489,7 +1486,7 @@ def canBeTonic(self) -> bool:
14891486

14901487
@overload
14911488
def closedPosition(
1492-
self: _ChordType,
1489+
self,
14931490
*,
14941491
forceOctave: int|None = None,
14951492
inPlace: t.Literal[True],
@@ -1499,21 +1496,21 @@ def closedPosition(
14991496

15001497
@overload
15011498
def closedPosition(
1502-
self: _ChordType,
1499+
self,
15031500
*,
15041501
forceOctave: int|None = None,
15051502
inPlace: t.Literal[False] = False,
15061503
leaveRedundantPitches: bool = False
1507-
) -> _ChordType:
1504+
) -> t.Self:
15081505
...
15091506

15101507
def closedPosition(
1511-
self: _ChordType,
1508+
self,
15121509
*,
15131510
forceOctave: int|None = None,
15141511
inPlace: bool = False,
15151512
leaveRedundantPitches: bool = False
1516-
) -> _ChordType|None:
1513+
) -> t.Self|None:
15171514
'''
15181515
Returns a new Chord object with the same pitch classes,
15191516
but now in closed position.
@@ -4015,7 +4012,7 @@ def root(self,
40154012

40164013
@overload
40174014
def semiClosedPosition(
4018-
self: _ChordType,
4015+
self,
40194016
*,
40204017
forceOctave,
40214018
inPlace: t.Literal[True],
@@ -4025,21 +4022,21 @@ def semiClosedPosition(
40254022

40264023
@overload
40274024
def semiClosedPosition(
4028-
self: _ChordType,
4025+
self,
40294026
*,
40304027
forceOctave=None,
40314028
inPlace: t.Literal[False] = False,
40324029
leaveRedundantPitches=False
4033-
) -> _ChordType:
4030+
) -> t.Self:
40344031
return self
40354032

40364033
def semiClosedPosition(
4037-
self: _ChordType,
4034+
self,
40384035
*,
40394036
forceOctave: int|None = None,
40404037
inPlace: t.Literal[True]|t.Literal[False] = False,
40414038
leaveRedundantPitches: bool = False
4042-
) -> None|_ChordType:
4039+
) -> None|t.Self:
40434040
# noinspection PyShadowingNames
40444041
'''
40454042
Similar to :meth:`~music21.chord.Chord.ClosedPosition` in that it

music21/harmony.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@
3838

3939
environLocal = environment.Environment('harmony')
4040

41-
T = t.TypeVar('T', bound='ChordSymbol')
42-
NCT = t.TypeVar('NCT', bound='NoChord')
43-
4441
if t.TYPE_CHECKING:
4542
from music21.figuredBass import realizerScale
4643

@@ -2394,7 +2391,7 @@ def inversionIsValid(self, inversion):
23942391
else:
23952392
return False
23962393

2397-
def transpose(self: T, value, *, inPlace=False) -> T|None:
2394+
def transpose(self, value, *, inPlace=False) -> t.Self|None:
23982395
'''
23992396
Overrides :meth:`~music21.chord.Chord.transpose` so that this ChordSymbol's
24002397
`figure` is appropriately cleared afterward.
@@ -2506,7 +2503,7 @@ def _parseFigure(self):
25062503
# do nothing, everything is already set.
25072504
return
25082505

2509-
def transpose(self: NCT, _value, *, inPlace=False) -> NCT|None:
2506+
def transpose(self, _value, *, inPlace=False) -> t.Self|None:
25102507
'''
25112508
Overrides :meth:`~music21.chord.Chord.transpose` to do nothing.
25122509

music21/key.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939

4040
environLocal = environment.Environment('key')
4141

42-
KeySignatureType = t.TypeVar('KeySignatureType', bound='KeySignature')
43-
KeyType = t.TypeVar('KeyType', bound='Key')
4442
TransposeTypes = int|str|interval.Interval|interval.GenericInterval
4543

4644

@@ -661,23 +659,23 @@ def accidentalByStep(self, step: StepName) -> pitch.Accidental|None:
661659
# --------------------------------------------------------------------------
662660
# methods
663661
@overload
664-
def transpose(self: KeySignatureType,
662+
def transpose(self,
665663
value: TransposeTypes,
666664
*,
667-
inPlace: t.Literal[False] = False) -> KeySignatureType:
665+
inPlace: t.Literal[False] = False) -> t.Self:
668666
...
669667

670668
@overload
671-
def transpose(self: KeySignatureType,
669+
def transpose(self,
672670
value: TransposeTypes,
673671
*,
674672
inPlace: t.Literal[True]) -> None:
675673
...
676674

677-
def transpose(self: KeySignatureType,
675+
def transpose(self,
678676
value: TransposeTypes,
679677
*,
680-
inPlace: bool = False) -> KeySignatureType|None:
678+
inPlace: bool = False) -> t.Self|None:
681679
'''
682680
Transpose the KeySignature by the user-provided value.
683681
If the value is an integer, the transposition is treated
@@ -1232,26 +1230,26 @@ def tonalCertainty(self, method='correlationCoefficient') -> float:
12321230
raise ValueError(f'Unknown method: {method}')
12331231

12341232
@overload
1235-
def transpose(self: KeyType,
1233+
def transpose(self,
12361234
value: TransposeTypes,
12371235
*,
12381236
inPlace: t.Literal[False] = False
1239-
) -> KeyType:
1237+
) -> t.Self:
12401238
...
12411239

12421240
@overload
1243-
def transpose(self: KeyType,
1241+
def transpose(self,
12441242
value: TransposeTypes,
12451243
*,
12461244
inPlace: t.Literal[True]
12471245
) -> None:
12481246
...
12491247

1250-
def transpose(self: KeyType,
1248+
def transpose(self,
12511249
value: TransposeTypes,
12521250
*,
12531251
inPlace: bool = False
1254-
) -> KeyType|None:
1252+
) -> t.Self|None:
12551253
'''
12561254
Transpose the Key by the user-provided value.
12571255
If the value is an integer, the transposition is treated

music21/note.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@
4343
from music21 import chord
4444
from music21 import instrument
4545
from music21 import percussion
46-
from music21.style import Style
47-
48-
_NotRestType = t.TypeVar('_NotRestType', bound='NotRest')
4946

5047
environLocal = environment.Environment('note')
5148

@@ -666,7 +663,7 @@ def __hash__(self):
666663

667664
# --------------------------------------------------------------------------
668665
@property
669-
def tie(self) -> tie.Tie|None:
666+
def tie(self) -> 'tie.Tie|None':
670667
'''
671668
Return and set a :class:`~music21.note.Tie` object, or None.
672669
@@ -678,7 +675,7 @@ def tie(self) -> tie.Tie|None:
678675
return self._tie
679676

680677
@tie.setter
681-
def tie(self, value: tie.Tie|None):
678+
def tie(self, value: 'tie.Tie|None'):
682679
self._tie = value
683680

684681
@property
@@ -1046,13 +1043,13 @@ def __init__(self,
10461043
# Special functions
10471044
# ==============================================================================================
10481045

1049-
def _deepcopySubclassable(self: _NotRestType,
1046+
def _deepcopySubclassable(self,
10501047
memo: dict[int, t.Any]|None = None,
10511048
*,
1052-
ignoreAttributes: set[str]|None = None) -> _NotRestType:
1049+
ignoreAttributes: set[str]|None = None) -> t.Self:
10531050
new = super()._deepcopySubclassable(memo, ignoreAttributes={'_chordAttached'})
10541051
if t.TYPE_CHECKING:
1055-
new = t.cast(_NotRestType, new)
1052+
new = t.cast(t.Self, new)
10561053
# let the chord restore _chordAttached
10571054

10581055
# after copying, if a Volume exists, it is linked to the old object
@@ -1292,7 +1289,7 @@ def _setVolume(self, value: None|volume.Volume|int|float, setClient=True):
12921289
raise TypeError(f'this must be a Volume object, not {value}')
12931290

12941291
@property
1295-
def volume(self) -> volume.Volume:
1292+
def volume(self) -> 'volume.Volume':
12961293
'''
12971294
Get and set the :class:`~music21.volume.Volume` object of this object.
12981295
Volume objects are created on demand.
@@ -1309,7 +1306,7 @@ def volume(self) -> volume.Volume:
13091306
return self._getVolume()
13101307

13111308
@volume.setter
1312-
def volume(self, value: None|volume.Volume|int|float):
1309+
def volume(self, value: 'None|volume.Volume|int|float'):
13131310
self._setVolume(value)
13141311

13151312
@property
@@ -1606,7 +1603,7 @@ def __ge__(self, other):
16061603
except AttributeError:
16071604
return NotImplemented
16081605

1609-
def __deepcopy__(self: Note, memo=None) -> Note:
1606+
def __deepcopy__(self, memo=None) -> t.Self:
16101607
'''
16111608
After doing a deepcopy of the pitch, be sure to set the client
16121609
'''

0 commit comments

Comments
 (0)