Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: Handle class definitions with empty argument list #6

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/codetext/parser/python_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ def get_class_metadata(class_node, blob: str=None) -> Dict[str, str]:
argument_list = get_node_text(child).split(',')
for arg in argument_list:
item = re.sub(r'[^a-zA-Z0-9\_]', ' ', arg).split()
metadata['parameters'][item[0].strip()] = None
# Handle class definitions with empty argument list class ABC()
if len(item) > 0:
metadata['parameters'][item[0].strip()] = None

# get __init__ function
return metadata
27 changes: 23 additions & 4 deletions tests/test_parser/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def test_get_class_list(self):
root = self.root_node

class_list = PythonParser.get_class_list(root)

self.assertEqual(len(class_list), 1)

def test_get_docstring(self):
Expand Down Expand Up @@ -59,21 +58,41 @@ def test_sample(arg1: str = "string", arg2 = "another_string"):

def test_get_class_metadata(self):
code_sample = '''
class ABC():
pass

class Sample(ABC):
def __init__(self):
pass

def test_sample(self, arg1: str = "string", arg2 = "another_string"):
return NotImplement()

class ThisIsalsoAclass(ABC, Sample):
pass
'''
root = parse_code(code_sample, 'python').root_node

classes = list(PythonParser.get_class_list(root))[0]
metadata = PythonParser.get_class_metadata(classes)


classes = list(PythonParser.get_class_list(root))
self.assertEqual(len(classes), 3)

metadata = PythonParser.get_class_metadata(classes[0])
self.assertEqual(metadata['parameters'], {})
self.assertEqual(metadata['identifier'], 'ABC')


metadata = PythonParser.get_class_metadata(classes[1])
self.assertEqual(metadata['parameters'], {'ABC': None})
self.assertEqual(metadata['identifier'], 'Sample')


metadata = PythonParser.get_class_metadata(classes[2])
self.assertEqual(metadata['parameters'], {'ABC': None, 'Sample': None})
self.assertEqual(metadata['identifier'], 'ThisIsalsoAclass')



def test_get_comment_list(self):
root = self.root_node

Expand Down
Loading