Skip to content

Commit 840be07

Browse files
committed
Fix handling of decorated classes
This should resolve the issue described in #80 where '@' decorated classes are missing attributes/arguments due to misaligned class column during tokenisation.
1 parent 55cfcad commit 840be07

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

tap/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,11 @@ def tokenize_source(obj: object) -> Generator:
190190

191191
def get_class_column(obj: type) -> int:
192192
"""Determines the column number for class variables in a class."""
193+
first_line = 1
193194
for token_type, token, (start_line, start_column), (end_line, end_column), line in tokenize_source(obj):
194-
if start_line == 1 or token.strip() == '':
195+
if token.strip() == '@':
196+
first_line += 1
197+
if start_line <= first_line or token.strip() == '':
195198
continue
196199

197200
return start_column

tests/test_utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from argparse import ArgumentTypeError
22
from collections import OrderedDict
3+
from dataclasses import dataclass
34
import json
45
import os
56
import subprocess
@@ -167,6 +168,23 @@ def func(self):
167168

168169
self.assertEqual(get_class_column(FuncColumn), 12)
169170

171+
def test_dataclass(self):
172+
@dataclass
173+
class DataclassColumn:
174+
arg: int = 5
175+
self.assertEqual(get_class_column(DataclassColumn), 12)
176+
177+
def test_dataclass_method(self):
178+
def wrapper(f):
179+
pass
180+
181+
@dataclass
182+
class DataclassColumn:
183+
@wrapper
184+
def func(self):
185+
pass
186+
self.assertEqual(get_class_column(DataclassColumn), 12)
187+
170188

171189
class ClassVariableTests(TestCase):
172190
def test_no_variables(self):
@@ -282,6 +300,14 @@ def f(self):
282300
class_variables['i'] = {'comment': ''}
283301
self.assertEqual(get_class_variables(FunctionsWithDocs), class_variables)
284302

303+
def test_dataclass(self):
304+
@dataclass
305+
class DataclassColumn:
306+
arg: int = 5
307+
class_variables = OrderedDict()
308+
class_variables['arg'] = {'comment': ''}
309+
self.assertEqual(get_class_variables(DataclassColumn), class_variables)
310+
285311

286312
class GetLiteralsTests(TestCase):
287313
def test_get_literals_string(self) -> None:

0 commit comments

Comments
 (0)