Skip to content

Commit d3b1874

Browse files
committed
Remove unused functions, adjust the tests for the new ones
1 parent a9f9315 commit d3b1874

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

src/tap/utils.py

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,12 @@ def is_positional_arg(*name_or_flags) -> bool:
185185
return not is_option_arg(*name_or_flags)
186186

187187

188-
def _tokenize_source(source: str) -> Generator[tokenize.TokenInfo, None, None]:
188+
def tokenize_source(source: str) -> Generator[tokenize.TokenInfo, None, None]:
189189
"""Returns a generator for the tokens of the object's source code, given the source code."""
190190
return tokenize.generate_tokens(StringIO(source).readline)
191191

192192

193-
def tokenize_source(obj: object) -> Generator:
194-
"""Returns a generator for the tokens of the object's source code."""
195-
return _tokenize_source(inspect.getsource(obj))
196-
197-
198-
def _get_class_column(tokens: Iterable[tokenize.TokenInfo]) -> int:
193+
def get_class_column(tokens: Iterable[tokenize.TokenInfo]) -> int:
199194
"""Determines the column number for class variables in a class, given the tokens of the class."""
200195
first_line = 1
201196
for token_type, token, (start_line, start_column), (end_line, end_column), line in tokens:
@@ -205,14 +200,10 @@ def _get_class_column(tokens: Iterable[tokenize.TokenInfo]) -> int:
205200
continue
206201

207202
return start_column
203+
raise ValueError("Could not find any class variables in the class.")
208204

209205

210-
def get_class_column(cls: type) -> int:
211-
"""Determines the column number for class variables in a class."""
212-
return _get_class_column(tokenize_source(cls))
213-
214-
215-
def _source_line_to_tokens(tokens: Iterable[tokenize.TokenInfo]) -> Dict[int, List[Dict[str, Union[str, int]]]]:
206+
def source_line_to_tokens(tokens: Iterable[tokenize.TokenInfo]) -> Dict[int, List[Dict[str, Union[str, int]]]]:
216207
"""
217208
Gets a dictionary mapping from line number to a dictionary of tokens on that line for an object's source code,
218209
given the tokens of the object's source code.
@@ -232,12 +223,7 @@ def _source_line_to_tokens(tokens: Iterable[tokenize.TokenInfo]) -> Dict[int, Li
232223
return line_to_tokens
233224

234225

235-
def source_line_to_tokens(obj: object) -> Dict[int, List[Dict[str, Union[str, int]]]]:
236-
"""Gets a dictionary mapping from line number to a dictionary of tokens on that line for an object's source code."""
237-
return _source_line_to_tokens(tokenize_source(obj))
238-
239-
240-
def _get_subsequent_assign_lines(source_cls: str) -> Set[int]:
226+
def get_subsequent_assign_lines(source_cls: str) -> Set[int]:
241227
"""
242228
For all multiline assign statements, get the line numbers after the first line of the assignment,
243229
given the source code of the object.
@@ -283,25 +269,22 @@ def _get_subsequent_assign_lines(source_cls: str) -> Set[int]:
283269

284270
return assign_lines
285271

286-
def get_subsequent_assign_lines(cls: type) -> Set[int]:
287-
"""For all multiline assign statements, get the line numbers after the first line of the assignment."""
288-
return _get_subsequent_assign_lines(inspect.getsource(cls))
289272

290273
def get_class_variables(cls: type) -> Dict[str, Dict[str, str]]:
291274
"""Returns a dictionary mapping class variables to their additional information (currently just comments)."""
292275
# Get the source code and tokens of the class
293276
source_cls = inspect.getsource(cls)
294-
tokens = tuple(_tokenize_source(source_cls))
277+
tokens = tuple(tokenize_source(source_cls))
295278

296279
# Get mapping from line number to tokens
297-
line_to_tokens = _source_line_to_tokens(tokens)
280+
line_to_tokens = source_line_to_tokens(tokens)
298281

299282
# Get class variable column number
300-
class_variable_column = _get_class_column(tokens)
283+
class_variable_column = get_class_column(tokens)
301284

302285
# For all multiline assign statements, get the line numbers after the first line of the assignment
303286
# This is used to avoid identifying comments in multiline assign statements
304-
subsequent_assign_lines = _get_subsequent_assign_lines(source_cls)
287+
subsequent_assign_lines = get_subsequent_assign_lines(source_cls)
305288

306289
# Extract class variables
307290
class_variable = None

tests/test_utils.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from argparse import ArgumentTypeError
2+
import inspect
23
import json
34
import os
45
import subprocess
@@ -11,6 +12,7 @@
1112
get_class_column,
1213
get_class_variables,
1314
GitInfo,
15+
tokenize_source,
1416
type_to_str,
1517
get_literals,
1618
TupleTypeEnforcer,
@@ -145,7 +147,8 @@ def test_column_simple(self):
145147
class SimpleColumn:
146148
arg = 2
147149

148-
self.assertEqual(get_class_column(SimpleColumn), 12)
150+
tokens = tokenize_source(inspect.getsource(SimpleColumn))
151+
self.assertEqual(get_class_column(tokens), 12)
149152

150153
def test_column_comment(self):
151154
class CommentColumn:
@@ -158,28 +161,32 @@ class CommentColumn:
158161

159162
arg = 2
160163

161-
self.assertEqual(get_class_column(CommentColumn), 12)
164+
tokens = tokenize_source(inspect.getsource(CommentColumn))
165+
self.assertEqual(get_class_column(tokens), 12)
162166

163167
def test_column_space(self):
164168
class SpaceColumn:
165169

166170
arg = 2
167171

168-
self.assertEqual(get_class_column(SpaceColumn), 12)
172+
tokens = tokenize_source(inspect.getsource(SpaceColumn))
173+
self.assertEqual(get_class_column(tokens), 12)
169174

170175
def test_column_method(self):
171176
class FuncColumn:
172177
def func(self):
173178
pass
174179

175-
self.assertEqual(get_class_column(FuncColumn), 12)
180+
tokens = tokenize_source(inspect.getsource(FuncColumn))
181+
self.assertEqual(get_class_column(tokens), 12)
176182

177183
def test_dataclass(self):
178184
@class_decorator
179185
class DataclassColumn:
180186
arg: int = 5
181187

182-
self.assertEqual(get_class_column(DataclassColumn), 12)
188+
tokens = tokenize_source(inspect.getsource(DataclassColumn))
189+
self.assertEqual(get_class_column(tokens), 12)
183190

184191
def test_dataclass_method(self):
185192
def wrapper(f):
@@ -191,7 +198,8 @@ class DataclassColumn:
191198
def func(self):
192199
pass
193200

194-
self.assertEqual(get_class_column(DataclassColumn), 12)
201+
tokens = tokenize_source(inspect.getsource(DataclassColumn))
202+
self.assertEqual(get_class_column(tokens), 12)
195203

196204

197205
class ClassVariableTests(TestCase):

0 commit comments

Comments
 (0)