From bfb240ee1740e358c11b1a28f624eec74b5747b6 Mon Sep 17 00:00:00 2001 From: Michael Scott Asato Cuthbert Date: Thu, 6 Oct 2022 00:04:14 -1000 Subject: [PATCH] isNum type to Rational not Number --- music21/common/classTools.py | 8 ++++---- music21/interval.py | 2 +- music21/musicxml/xmlToM21.py | 4 +++- music21/note.py | 2 +- music21/spanner.py | 2 +- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/music21/common/classTools.py b/music21/common/classTools.py index 2c4efd541e..ea9fbb5b81 100644 --- a/music21/common/classTools.py +++ b/music21/common/classTools.py @@ -40,14 +40,14 @@ def isInt(usrData: t.Any) -> t.TypeGuard[int]: return isinstance(usrData, int) and usrData is not True and usrData is not False -def isNum(usrData: t.Any) -> t.TypeGuard[numbers.Number]: +def isNum(usrData: t.Any) -> t.TypeGuard[numbers.Rational]: ''' check if usrData is a number (float, int, long, Decimal), - return boolean + return boolean and if True casts the value as a Rational number - unlike `isinstance(usrData, Number)` does not return True for `True, False`. + unlike `isinstance(usrData, Rational)` does not return True for `True, False`. - Does not use `isinstance(usrData, Number)` which is 2-6 times slower + Does not use `isinstance(usrData, Rational)` which is 2-6 times slower than calling this function (except in the case of Fraction, when it's 6 times faster, but that's rarer). (6 times slower on Py3.4, now only 2x slower in Python 3.10) diff --git a/music21/interval.py b/music21/interval.py index 7f2588ac7f..6b2c479598 100644 --- a/music21/interval.py +++ b/music21/interval.py @@ -1676,7 +1676,7 @@ def __init__(self, if isinstance(generic, GenericInterval): self.generic = generic - elif common.isNum(generic) or isinstance(generic, str): + elif common.isInt(generic) or isinstance(generic, str): self.generic = GenericInterval(generic) else: # pragma: no cover # too rare to cover. diff --git a/music21/musicxml/xmlToM21.py b/music21/musicxml/xmlToM21.py index 14673afd63..118755fe0c 100644 --- a/music21/musicxml/xmlToM21.py +++ b/music21/musicxml/xmlToM21.py @@ -4732,7 +4732,9 @@ def xmlBarline(self, mxBarline): # not complete # TODO: this should also filter by number (in theory.) - rbSpanners = self.spannerBundle.getByClass(spanner.RepeatBracket).getByCompleteStatus(False) + rbSpanners = self.spannerBundle.getByClass( + spanner.RepeatBracket + ).getByCompleteStatus(False) # if we have no complete bracket objects, must start a new one if not rbSpanners: # create with this measure as the object diff --git a/music21/note.py b/music21/note.py index 4b7126c517..5d141674f0 100644 --- a/music21/note.py +++ b/music21/note.py @@ -456,7 +456,7 @@ def number(self) -> int: @number.setter def number(self, value: int) -> None: - if not common.isNum(value): + if not common.isInt(value): raise LyricException('Number best be number') self._number = value diff --git a/music21/spanner.py b/music21/spanner.py index b6ef7533d3..080edace57 100644 --- a/music21/spanner.py +++ b/music21/spanner.py @@ -20,7 +20,7 @@ ''' from __future__ import annotations -from collections.abc import Collection, Sequence, Iterable +from collections.abc import Sequence, Iterable import copy import typing as t import unittest