Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions music21/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ def __init__(self,
if quarterLength is not None:
self.duration.quarterLength = quarterLength

def __eq__(self: _M21T, other) -> t.TypeGuard[_M21T]:
def __eq__(self, other) -> t.TypeGuard[t.Self]:
'''
Define equality for Music21Objects. See main class docs.
'''
Expand Down Expand Up @@ -612,10 +612,10 @@ def mergeAttributes(self, other: Music21Object) -> None:
self.id = other.id
self.groups = copy.deepcopy(other.groups)

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

return new

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

Expand Down
43 changes: 20 additions & 23 deletions music21/chord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@

environLocal = environment.Environment('chord')

_ChordBaseType = t.TypeVar('_ChordBaseType', bound='music21.chord.ChordBase')
_ChordType = t.TypeVar('_ChordType', bound='music21.chord.Chord')

# ------------------------------------------------------------------------------
class ChordException(exceptions21.Music21Exception):
pass
Expand Down Expand Up @@ -162,7 +159,7 @@ def __eq__(self, other):
def __hash__(self):
return super().__hash__()

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

def _removePitchByRedundantAttribute(
self: _ChordType,
self,
attribute: str,
*,
inPlace=False
) -> _ChordType|list[pitch.Pitch]:
) -> t.Self|list[pitch.Pitch]:
'''
Common method for stripping pitches based on redundancy of one pitch
attribute. The `attribute` is provided by a string.
Expand Down Expand Up @@ -1113,7 +1110,7 @@ def add(

@overload
def annotateIntervals(
self: _ChordType,
self,
*,
inPlace: bool = False,
stripSpecifiers: bool = True,
Expand All @@ -1124,7 +1121,7 @@ def annotateIntervals(

@overload
def annotateIntervals(
self: _ChordType,
self,
*,
inPlace: t.Literal[True],
stripSpecifiers: bool = True,
Expand All @@ -1135,23 +1132,23 @@ def annotateIntervals(

@overload
def annotateIntervals(
self: _ChordType,
self,
*,
inPlace: t.Literal[False] = False,
stripSpecifiers: bool = True,
sortPitches: bool = True,
returnList: t.Literal[False] = False
) -> _ChordType:
) -> t.Self:
...

def annotateIntervals(
self: _ChordType,
self,
*,
inPlace: bool = False,
stripSpecifiers: bool = True,
sortPitches: bool = True,
returnList: bool = False
) -> _ChordType|None|list[str]:
) -> t.Self|None|list[str]:
# noinspection PyShadowingNames
'''
Add lyrics to the chord that show the distance of each note from
Expand Down Expand Up @@ -1251,7 +1248,7 @@ def annotateIntervals(
if not inPlace:
return c

def areZRelations(self: _ChordType, other: _ChordType) -> bool:
def areZRelations(self, other: t.Self) -> bool:
'''
Check if another Chord is a z-relation to this Chord.

Expand Down Expand Up @@ -1489,7 +1486,7 @@ def canBeTonic(self) -> bool:

@overload
def closedPosition(
self: _ChordType,
self,
*,
forceOctave: int|None = None,
inPlace: t.Literal[True],
Expand All @@ -1499,21 +1496,21 @@ def closedPosition(

@overload
def closedPosition(
self: _ChordType,
self,
*,
forceOctave: int|None = None,
inPlace: t.Literal[False] = False,
leaveRedundantPitches: bool = False
) -> _ChordType:
) -> t.Self:
...

def closedPosition(
self: _ChordType,
self,
*,
forceOctave: int|None = None,
inPlace: bool = False,
leaveRedundantPitches: bool = False
) -> _ChordType|None:
) -> t.Self|None:
'''
Returns a new Chord object with the same pitch classes,
but now in closed position.
Expand Down Expand Up @@ -4015,7 +4012,7 @@ def root(self,

@overload
def semiClosedPosition(
self: _ChordType,
self,
*,
forceOctave,
inPlace: t.Literal[True],
Expand All @@ -4025,21 +4022,21 @@ def semiClosedPosition(

@overload
def semiClosedPosition(
self: _ChordType,
self,
*,
forceOctave=None,
inPlace: t.Literal[False] = False,
leaveRedundantPitches=False
) -> _ChordType:
) -> t.Self:
return self

def semiClosedPosition(
self: _ChordType,
self,
*,
forceOctave: int|None = None,
inPlace: t.Literal[True]|t.Literal[False] = False,
leaveRedundantPitches: bool = False
) -> None|_ChordType:
) -> None|t.Self:
# noinspection PyShadowingNames
'''
Similar to :meth:`~music21.chord.Chord.ClosedPosition` in that it
Expand Down
7 changes: 2 additions & 5 deletions music21/harmony.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@

environLocal = environment.Environment('harmony')

T = t.TypeVar('T', bound='ChordSymbol')
NCT = t.TypeVar('NCT', bound='NoChord')

if t.TYPE_CHECKING:
from music21.figuredBass import realizerScale

Expand Down Expand Up @@ -2394,7 +2391,7 @@ def inversionIsValid(self, inversion):
else:
return False

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

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

Expand Down
22 changes: 10 additions & 12 deletions music21/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@

environLocal = environment.Environment('key')

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


Expand Down Expand Up @@ -661,23 +659,23 @@ def accidentalByStep(self, step: StepName) -> pitch.Accidental|None:
# --------------------------------------------------------------------------
# methods
@overload
def transpose(self: KeySignatureType,
def transpose(self,
value: TransposeTypes,
*,
inPlace: t.Literal[False] = False) -> KeySignatureType:
inPlace: t.Literal[False] = False) -> t.Self:
...

@overload
def transpose(self: KeySignatureType,
def transpose(self,
value: TransposeTypes,
*,
inPlace: t.Literal[True]) -> None:
...

def transpose(self: KeySignatureType,
def transpose(self,
value: TransposeTypes,
*,
inPlace: bool = False) -> KeySignatureType|None:
inPlace: bool = False) -> t.Self|None:
'''
Transpose the KeySignature by the user-provided value.
If the value is an integer, the transposition is treated
Expand Down Expand Up @@ -1232,26 +1230,26 @@ def tonalCertainty(self, method='correlationCoefficient') -> float:
raise ValueError(f'Unknown method: {method}')

@overload
def transpose(self: KeyType,
def transpose(self,
value: TransposeTypes,
*,
inPlace: t.Literal[False] = False
) -> KeyType:
) -> t.Self:
...

@overload
def transpose(self: KeyType,
def transpose(self,
value: TransposeTypes,
*,
inPlace: t.Literal[True]
) -> None:
...

def transpose(self: KeyType,
def transpose(self,
value: TransposeTypes,
*,
inPlace: bool = False
) -> KeyType|None:
) -> t.Self|None:
'''
Transpose the Key by the user-provided value.
If the value is an integer, the transposition is treated
Expand Down
19 changes: 8 additions & 11 deletions music21/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
from music21 import chord
from music21 import instrument
from music21 import percussion
from music21.style import Style

_NotRestType = t.TypeVar('_NotRestType', bound='NotRest')

environLocal = environment.Environment('note')

Expand Down Expand Up @@ -666,7 +663,7 @@ def __hash__(self):

# --------------------------------------------------------------------------
@property
def tie(self) -> tie.Tie|None:
def tie(self) -> 'tie.Tie|None':
'''
Return and set a :class:`~music21.note.Tie` object, or None.

Expand All @@ -678,7 +675,7 @@ def tie(self) -> tie.Tie|None:
return self._tie

@tie.setter
def tie(self, value: tie.Tie|None):
def tie(self, value: 'tie.Tie|None'):
self._tie = value

@property
Expand Down Expand Up @@ -1046,13 +1043,13 @@ def __init__(self,
# Special functions
# ==============================================================================================

def _deepcopySubclassable(self: _NotRestType,
def _deepcopySubclassable(self,
memo: dict[int, t.Any]|None = None,
*,
ignoreAttributes: set[str]|None = None) -> _NotRestType:
ignoreAttributes: set[str]|None = None) -> t.Self:
new = super()._deepcopySubclassable(memo, ignoreAttributes={'_chordAttached'})
if t.TYPE_CHECKING:
new = t.cast(_NotRestType, new)
new = t.cast(t.Self, new)
# let the chord restore _chordAttached

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

@property
def volume(self) -> volume.Volume:
def volume(self) -> 'volume.Volume':
'''
Get and set the :class:`~music21.volume.Volume` object of this object.
Volume objects are created on demand.
Expand All @@ -1309,7 +1306,7 @@ def volume(self) -> volume.Volume:
return self._getVolume()

@volume.setter
def volume(self, value: None|volume.Volume|int|float):
def volume(self, value: 'None|volume.Volume|int|float'):
self._setVolume(value)

@property
Expand Down Expand Up @@ -1606,7 +1603,7 @@ def __ge__(self, other):
except AttributeError:
return NotImplemented

def __deepcopy__(self: Note, memo=None) -> Note:
def __deepcopy__(self, memo=None) -> t.Self:
'''
After doing a deepcopy of the pitch, be sure to set the client
'''
Expand Down
Loading