Skip to content

Commit 299974a

Browse files
authored
Merge pull request #26 from Mathics3/type-annotations
Add some type annotations and format strings
2 parents 2b55db1 + a590b03 commit 299974a

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

mathics_scanner/feed.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"""
66

77
from abc import abstractmethod, ABCMeta
8-
from chardet import detect
98

109

1110
class LineFeeder(metaclass=ABCMeta):
@@ -30,39 +29,43 @@ def feed(self):
3029
Consume and return next line of code. Each line should be followed by a
3130
newline character. Returns '' after all lines are consumed.
3231
"""
32+
3333
return ""
3434

3535
@abstractmethod
36-
def empty(self):
36+
def empty(self) -> bool:
3737
"""
3838
Return True once all lines have been consumed.
3939
"""
40+
4041
return True
4142

42-
def message(self, sym: str, tag, *args):
43+
def message(self, sym: str, tag: str, *args) -> None:
4344
"""
4445
Append a generic message of type ``sym`` to the message queue.
4546
"""
47+
4648
if sym == "Syntax":
4749
message = self.syntax_message(sym, tag, *args)
4850
else:
4951
message = [sym, tag] + list(args)
5052
self.messages.append(message)
5153

52-
def syntax_message(self, sym: str, tag, *args):
54+
def syntax_message(self, sym: str, tag: str, *args):
5355
"""
5456
Append a message concerning syntax errors to the message queue.
5557
"""
58+
5659
if len(args) > 3:
5760
raise ValueError("Too many args.")
5861
message = [sym, tag]
5962
for i in range(3):
6063
if i < len(args):
61-
message.append('"' + args[i] + '"')
64+
message.append(f'"{args[i]}"')
6265
else:
6366
message.append('""')
64-
message.append(self.lineno)
65-
message.append('"' + self.filename + '"')
67+
message.append(str(self.lineno))
68+
message.append(f'"{self.filename}"')
6669
assert len(message) == 7
6770
return message
6871

@@ -97,7 +100,7 @@ def feed(self):
97100
result = ""
98101
return result
99102

100-
def empty(self):
103+
def empty(self) -> bool:
101104
return self.lineno >= len(self.lines)
102105

103106

@@ -121,7 +124,7 @@ def feed(self):
121124
self.lineno += 1
122125
return self.code
123126

124-
def empty(self):
127+
def empty(self) -> bool:
125128
return self._empty
126129

127130

mathics_scanner/prescanner.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22

3-
43
from mathics_scanner.characters import named_characters
54
from mathics_scanner.errors import ScanError, IncompleteSyntaxError
65

@@ -67,11 +66,11 @@ def scan(self):
6766
# reduce
6867
return "".join(self.stubs)
6968

70-
def newstub(self, pos):
69+
def newstub(self, pos: int) -> None:
7170
self.pos = pos
7271
self.start = pos
7372

74-
def try_parse_base(self, start_shift, end_shift, base):
73+
def try_parse_base(self, start_shift: int, end_shift: int, base: int) -> None:
7574
start, end = self.pos + start_shift, self.pos + end_shift
7675
result = None
7776
if end <= len(self.code):

mathics_scanner/tokeniser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,9 @@
272272
literal_tokens[c] = ["Number"]
273273

274274

275-
def find_indices(literals):
275+
def find_indices(literals) -> dict:
276276
"find indices of literal tokens"
277+
277278
literal_indices = {}
278279
for key, tags in literals.items():
279280
indices = []
@@ -282,8 +283,8 @@ def find_indices(literals):
282283
if tag == tag2:
283284
indices.append(i)
284285
break
285-
literal_indices[key] = tuple(indices)
286286
assert len(indices) == len(tags)
287+
literal_indices[key] = tuple(indices)
287288
return literal_indices
288289

289290

0 commit comments

Comments
 (0)