A tool to detect duplicate variable names within the same scope in Python files
Project description
check-duplicate-variables
A Python tool that detects duplicate variable names within the same scope (module or class) in Python files. This tool is designed to help identify potential copy-paste errors, refactoring issues, and maintain code quality in automated testing frameworks, particularly page object models.
Features
- AST-based Analysis: Uses Python's Abstract Syntax Tree (AST) for accurate parsing
- Scope-aware Detection: Identifies duplicates within module-level and class-level scopes separately
- Value Comparison: Reports whether duplicate assignments have the same or different values
- Multiple Assignment Types: Handles standard assignments, type-annotated assignments, tuple unpacking, and attribute assignments
- Recursive Directory Scanning: Automatically scans all Python files in a directory tree
- CI/CD Friendly: Provides exit codes and formatted output suitable for continuous integration pipelines
- Error Handling: Gracefully handles file I/O errors, encoding issues, and syntax errors
Installation
Install from PyPI
pip install check-duplicate-variables
Install from Source
git clone https://github.com/pandiyarajk/check-duplicate-python-variables.git
cd check-duplicate-python-variables
pip install .
Requirements
- Python 3.7 or higher
- No additional packages required (uses only standard library:
ast,os,sys,collections,typing)
Usage
Basic Usage
After installation, use the check-duplicate-variables command:
check-duplicate-variables
This will look for a pageobjects directory in the current working directory.
Custom Directory
Specify a custom directory to scan:
check-duplicate-variables /path/to/your/python/files
As a Python Module
You can also run it as a Python module:
python -m check_duplicate_variables [directory]
or
python -m check_duplicate_variables [filepath]
Standalone Script
If you have the standalone script, you can run it directly:
python check_duplicate_variables.py [directory]
Exit Codes
0: No duplicates found and no errors encountered1: Duplicates found or I/O/parse errors occurred
Output Format
The tool outputs one line per duplicate variable found:
file_path: variable_name (same values) - line1, line2, line3
file_path: variable_name (different values) - line1, line2
Example Output
pageobjects/login_page.py: username (same values) - 15, 23, 45
pageobjects/login_page.py: password (different values) - 18, 32
pageobjects/dashboard.py: button_text (same values) - 10, 25
(same values): All assignments have identical values (potential copy-paste error)(different values): Assignments have different values (intentional reassignment or bug)
How It Works
- File Scanning: Recursively walks the specified directory and finds all
.pyfiles - AST Parsing: Each file is parsed using Python's
astmodule - Variable Tracking: The
VariableAnalyzervisitor class traverses the AST and tracks all variable assignments per scope - Duplicate Detection: Identifies variables assigned more than once in the same scope
- Value Comparison: Extracts and normalizes the source code of assigned values to compare them
- Reporting: Outputs results in a CI-friendly format with line numbers and value comparison status
Supported Assignment Types
The tool detects duplicates in the following assignment patterns:
- Simple assignments:
x = 1 - Type-annotated assignments:
x: int = 1 - Multiple assignments:
x = y = 1 - Tuple unpacking:
a, b = 1, 2 - Attribute assignments:
self.attr = value - Subscript assignments:
x[i] = value
Use Cases
- Page Object Models: Detect duplicate locators or element definitions in Selenium/Playwright page objects
- Code Quality: Identify copy-paste errors and refactoring opportunities
- Pre-commit Hooks: Integrate into Git pre-commit hooks to catch issues before commit
- CI/CD Pipelines: Add to GitHub Actions, GitLab CI, or other CI/CD workflows
- Code Reviews: Automated detection of duplicate variable definitions
Integration Examples
GitHub Actions
name: Check Duplicate Variables
on: [push, pull_request]
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install check-duplicate-variables
run: pip install check-duplicate-variables
- name: Check for duplicate variables
run: check-duplicate-variables ./src
Pre-commit Hook
Create .git/hooks/pre-commit:
#!/bin/bash
check-duplicate-variables ./pageobjects
if [ $? -ne 0 ]; then
echo "Duplicate variables detected. Please fix before committing."
exit 1
fi
Limitations
- Only detects duplicates within the same scope (module or class)
- Does not detect duplicates across different scopes (e.g., module variable vs. class variable with same name)
- Value comparison is based on source code normalization, not runtime evaluation
- Does not handle dynamically generated variable names
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Pandiyaraj Karuppasamy
- Email: pandiyarajk@live.com
- GitHub: @pandiyarajk
Repository
https://github.com/pandiyarajk/check-duplicate-python-variables.git
Changelog
See CHANGE_LOG.md for version history and changes.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file check_duplicate_variables-1.0.2.tar.gz.
File metadata
- Download URL: check_duplicate_variables-1.0.2.tar.gz
- Upload date:
- Size: 13.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b409755a6ac4403e0a0247f5776746b1e29cc0249ee3d0a49e695947f6d0b0e7
|
|
| MD5 |
3bae1f84f66182a126499cb335b793ea
|
|
| BLAKE2b-256 |
9d391b83d56c10b657e15d0ee28cb6bdac5d9482cc4708813243b7d0f651b066
|
File details
Details for the file check_duplicate_variables-1.0.2-py3-none-any.whl.
File metadata
- Download URL: check_duplicate_variables-1.0.2-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aba240d27b9e1c0975a80b3c3ee225b9f226649c8485614d5627a7d9a98cc53a
|
|
| MD5 |
605dea2de221c5aa9d8856aa3ddd3e19
|
|
| BLAKE2b-256 |
c5a790c65411102e81a64b1b5ca8acf8b5a259b8a7dfb5345b8f9fbeff3f7c36
|