In a first attempt to add types I ran into trouble. First a code snippet
( full code at
https://gist.github.com/ingoogni/460676b11e5a04ed3a3ac93ae0e1fddd )
_matchline: Pattern[str] = re.compile(r"(?P<blank>^\s*$)|(?P<ind>[\t]*)"
"((?P<lnid>[A-Z]+):)?([ \t]*)(?P<content>.*)")
def proc_matchlns(momfile: TextIO) -> Iterator[Dict[str, Union[str, int]]]:
for i, ln in enumerate(momfile, 1):
match: Match[str] = _matchline.match(ln) #error 1
m: Dict[str, str] = match.groupdict() #error 2
m['ln'] = ln.rstrip()
m['ln#'] = i #error 3
yield m #error 4
Depending on how I add types mypy nags on various aspects:
error 1:
match seems to want type Optional[Match[str]], but why? The regex
always has a result (I hope).
error 2:
when I give in to the above, the result is that it errors on not
being able to de a groupdict on a null type.
error 3:
groupdict results in a type dict[str, str], but I want to add an int.
Can I change the type? (In this specific case could change i to str(i)
as it has no influence on the program)
error 4:
result of 3 and output type, type of m and specified output type
don't match.
Ingo
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor