Skip to content

Commit

Permalink
Merge pull request #1749 from cuthbertLab/tab-typing
Browse files Browse the repository at this point in the history
update interval Typing overloads
  • Loading branch information
mscuthbert authored Feb 7, 2025
2 parents 6c74d50 + 49ce42e commit f4ecd7e
Showing 1 changed file with 54 additions and 5 deletions.
59 changes: 54 additions & 5 deletions music21/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ class Direction(enum.IntEnum):

prefixSpecs = ('ERROR', 'P', 'M', 'm', 'A', 'd', 'AA', 'dd', 'AAA', 'ddd', 'AAAA', 'dddd')


# helper for comparisons
def _same_class(note1: note.Note|pitch.Pitch, note2: note.Note|pitch.Pitch) -> None:
'''
Raise a ValueError if note1 and note2 are not both Notes or both Pitches
'''
if ((hasattr(note1, 'pitch') and not hasattr(note2, 'pitch'))
or (hasattr(note2, 'pitch') and not hasattr(note1, 'pitch'))):
raise ValueError(f'note1 {note1!r} and note2 {note2!r} must both be notes or pitches')


class Specifier(enum.IntEnum):
'''
An enumeration for "specifiers" such as Major, Minor, etc.
Expand Down Expand Up @@ -3708,6 +3719,19 @@ def noteEnd(self, n: music21.note.Note|None):


# ------------------------------------------------------------------------------
@overload
def getWrittenHigherNote(note1: note.Note,
note2: note.Note|pitch.Pitch
) -> note.Note:
...

@overload
def getWrittenHigherNote(note1: pitch.Pitch,
note2: note.Note|pitch.Pitch
) -> pitch.Pitch:
...


def getWrittenHigherNote(note1: note.Note|pitch.Pitch,
note2: note.Note|pitch.Pitch
) -> note.Note|pitch.Pitch:
Expand All @@ -3734,6 +3758,7 @@ def getWrittenHigherNote(note1: note.Note|pitch.Pitch,
>>> interval.getWrittenHigherNote(aNote, bNote) is aNote
True
'''
_same_class(note1, note2)
(p1, p2) = (_extractPitch(note1), _extractPitch(note2))

num1 = p1.diatonicNoteNum
Expand All @@ -3746,6 +3771,19 @@ def getWrittenHigherNote(note1: note.Note|pitch.Pitch,
return getAbsoluteHigherNote(note1, note2)



@overload
def getAbsoluteHigherNote(note1: note.Note,
note2: note.Note|pitch.Pitch
) -> note.Note:
...

@overload
def getAbsoluteHigherNote(note1: pitch.Pitch,
note2: note.Note|pitch.Pitch
) -> pitch.Pitch:
...

def getAbsoluteHigherNote(note1: note.Note|pitch.Pitch,
note2: note.Note|pitch.Pitch
) -> note.Note|pitch.Pitch:
Expand All @@ -3759,6 +3797,7 @@ def getAbsoluteHigherNote(note1: note.Note|pitch.Pitch,
>>> interval.getAbsoluteHigherNote(aNote, bNote)
<music21.note.Note C#>
'''
_same_class(note1, note2)
chromatic = notesToChromatic(note1, note2)
semitones = chromatic.semitones
if semitones > 0:
Expand All @@ -3769,7 +3808,6 @@ def getAbsoluteHigherNote(note1: note.Note|pitch.Pitch,
return note1



@overload
def getWrittenLowerNote(note1: note.Note,
note2: note.Note|pitch.Pitch
Expand Down Expand Up @@ -3813,10 +3851,7 @@ def getWrittenLowerNote(note1: note.Note|pitch.Pitch,
ValueError: note1 <music21.pitch.Pitch C#4>
and note2 <music21.note.Note C#> must both be notes or pitches
'''
if ((hasattr(note1, 'pitch') and not hasattr(note2, 'pitch'))
or (hasattr(note2, 'pitch') and not hasattr(note1, 'pitch'))):
raise ValueError(f'note1 {note1!r} and note2 {note2!r} must both be notes or pitches')

_same_class(note1, note2)
(p1, p2) = (_extractPitch(note1), _extractPitch(note2))

num1 = p1.diatonicNoteNum
Expand All @@ -3829,6 +3864,19 @@ def getWrittenLowerNote(note1: note.Note|pitch.Pitch,
return getAbsoluteLowerNote(note1, note2)


@overload
def getAbsoluteLowerNote(note1: note.Note,
note2: note.Note|pitch.Pitch
) -> note.Note:
...

@overload
def getAbsoluteLowerNote(note1: pitch.Pitch,
note2: note.Note|pitch.Pitch
) -> pitch.Pitch:
...


def getAbsoluteLowerNote(note1: note.Note|pitch.Pitch,
note2: note.Note|pitch.Pitch
) -> note.Note|pitch.Pitch:
Expand All @@ -3842,6 +3890,7 @@ def getAbsoluteLowerNote(note1: note.Note|pitch.Pitch,
>>> interval.getAbsoluteLowerNote(aNote, bNote)
<music21.pitch.Pitch D--3>
'''
_same_class(note1, note2)
chromatic = notesToChromatic(note1, note2)
semitones = chromatic.semitones
if semitones > 0:
Expand Down

0 comments on commit f4ecd7e

Please sign in to comment.