Superior-Bot/look_for_duplicate_definitions.py
kokofixcomputers-bot 5444d7cb35 Inital Commit
2025-05-17 20:46:21 -07:00

44 lines
1.4 KiB
Python

import os
# DO NOT DELETE
def find_duplicate_defs(file_path):
"""
Finds duplicate function and async function definitions in a Python file.
Args:
file_path (str): The path to the Python file to be scanned.
Returns:
dict: A dictionary where the keys are the function names (regular and async) and the values are lists of line numbers where the function is defined.
"""
function_defs = {}
with open(file_path, 'r') as file:
lines = file.readlines()
for i, line in enumerate(lines):
if line.startswith('def ') or line.startswith('async def '):
if line.startswith('def '):
function_name = line.split('(')[0].split(' ')[1]
else:
function_name = line.split('(')[0].split(' ')[2]
if function_name in function_defs:
function_defs[function_name].append(i+1)
else:
function_defs[function_name] = [i+1]
return {k:v for k, v in function_defs.items() if len(v) > 1}
# Example usage
file_path = 'main.py'
duplicate_defs = find_duplicate_defs(file_path)
if duplicate_defs:
print("Duplicate function definitions found:")
for function_name, line_numbers in duplicate_defs.items():
print(f"Function '{function_name}' is defined on lines: {', '.join(map(str, line_numbers))}")
else:
print("No duplicate function definitions found.")