Skip to content

Commit

Permalink
Add more typeguards, fix append, commonTest
Browse files Browse the repository at this point in the history
  • Loading branch information
mscuthbert committed Oct 5, 2022
1 parent 3d4b69c commit 0ea1ef5
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 9 deletions.
6 changes: 3 additions & 3 deletions music21/common/classTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ 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[Number]:
def isNum(usrData: t.Any) -> t.TypeGuard[numbers.Number]:
'''
check if usrData is a number (float, int, long, Decimal),
return boolean
Expand Down Expand Up @@ -85,7 +85,7 @@ def isNum(usrData: t.Any) -> t.TypeGuard[Number]:
return False


def isListLike(usrData: t.Any) -> bool:
def isListLike(usrData: t.Any) -> t.TypeGuard[list | tuple]:
'''
Returns True if is a List or Tuple or their subclasses.
Expand Down Expand Up @@ -145,7 +145,7 @@ def isIterable(usrData: t.Any) -> t.TypeGuard[Iterable]:
return False


def holdsType(usrData: t.Any, checkType: type[_T]) -> TypeGuard[Collection[_T]]:
def holdsType(usrData: t.Any, checkType: type[_T]) -> t.TypeGuard[Collection[_T]]:
'''
Returns True if usrData is a Collection of type checkType.
Expand Down
2 changes: 1 addition & 1 deletion music21/musicxml/xmlToM21.py
Original file line number Diff line number Diff line change
Expand Up @@ -4732,7 +4732,7 @@ def xmlBarline(self, mxBarline):
# not complete

# TODO: this should also filter by number (in theory.)
rbSpanners = self.spannerBundle.getByClass('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
Expand Down
3 changes: 1 addition & 2 deletions music21/spanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,6 @@ def _setNumber(self, value: int | str | Iterable[int]):
# undefined.
self.numberRange = [0]
elif common.holdsType(value, int):
value = t.cast(Collection[int], value) # unnecessary, but not working in mypy 0.982
self.numberRange = [] # clear
for x in value:
self.numberRange.append(x)
Expand All @@ -1364,7 +1363,7 @@ def _setNumber(self, value: int | str | Iterable[int]):
one_number = int(one_letter_value.strip())
self.numberRange.append(one_number)
elif value.isdigit():
self.numberRange.append(int(value))
self.numberRange = [int(value)]
else:
raise SpannerException(f'number for RepeatBracket must be a number, not {value!r}')
elif common.isInt(value):
Expand Down
6 changes: 3 additions & 3 deletions music21/test/commonTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ def load_source(name: str, path: str) -> types.ModuleType:
raise FileNotFoundError(f'No such file or directory: {path!r}')
if name in sys.modules:
module = sys.modules[name]
spec.loader.exec_module(module)
else:
module = importlib.util.module_from_spec(spec)
if module is None:
raise FileNotFoundError(f'No such file or directory: {path!r}')
spec.loader.exec_module(module)
sys.modules[name] = module
spec.loader.exec_module(module)

return module

Expand Down Expand Up @@ -424,7 +424,7 @@ def getModule(self, fp, restoreEnvironmentDefaults=False):
with warnings.catch_warnings():
mod = load_source(name, fp)
except Exception as excp: # pylint: disable=broad-except
environLocal.warn(['failed import:', fp, '\n',
environLocal.warn(['failed import:', name, '\t', fp, '\n',
'\tEXCEPTION:', str(excp).strip()])
return None

Expand Down

0 comments on commit 0ea1ef5

Please sign in to comment.