This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Python-snap7 is a Python wrapper for the Snap7 library, providing Ethernet communication with Siemens S7 PLCs. The library supports Python 3.10+ and runs on Windows, Linux, and macOS.
- snap7/client.py: Main Client class for connecting to S7 PLCs
- snap7/server.py: Server implementation for PLC simulation
- snap7/logo.py: Logo PLC communication
- snap7/partner.py: Partner connection functionality
- snap7/util/: Utility functions for data conversion (getters.py, setters.py, db.py)
- snap7/protocol.py: Low-level protocol bindings to the native Snap7 library
- snap7/type.py: Type definitions and enums (Area, Block, WordLen, etc.)
- snap7/common.py: Common utilities including library loading
- snap7/error.py: Error handling and exceptions
The library uses ctypes to interface with the native Snap7 C library (libsnap7.so/snap7.dll/libsnap7.dylib).
# Run all tests
pytest
# Run specific test categories using markers
pytest -m "server or util or client or mainloop"
# Run tests for specific component
pytest tests/test_client.py# Type checking
mypy snap7 tests example
# Linting and formatting check
ruff check snap7 tests example
ruff format --diff snap7 tests example
# Auto-format code
ruff format snap7 tests example
ruff check --fix snap7 tests example# Run all tox environments (mypy, lint, py310-py314)
tox
# Run specific environments
tox -e mypy
tox -e lint-ruff
tox -e py310# Setup development environment
make setup
# Run tests
make test
# Type checking
make mypy
# Linting
make check
# Format code
make format# Install in development mode
pip install -e .
# Install with test dependencies
pip install -e ".[test]"
# Install with CLI tools
pip install -e ".[cli]"Tests are organized with pytest markers:
client: Client functionality testsserver: Server functionality testsutil: Utility function testslogo: Logo PLC testspartner: Partner connection testsmainloop: Main loop testscommon: Common functionality tests
Fully tested and supported Python versions:
- Python 3.10 (EOL: October 2026) ✅
- Python 3.11 (EOL: October 2027) ✅
- Python 3.12 (EOL: October 2028) ✅
- Python 3.13 (EOL: October 2029) ✅
- Python 3.14 (EOL: October 2030) ✅
All versions pass the complete test suite and have been verified for type checking, linting, and functionality.
This library supports Windows, Linux, and macOS through ctypes bindings:
- Windows: Uses
windllfrom ctypes for library loading - Linux/macOS: Uses
cdllfrom ctypes for library loading - Library files: Includes platform-specific Snap7 libraries (snap7.dll, libsnap7.so, libsnap7.dylib)
- The conditional import in
snap7/common.pyhandles platform differences automatically - No
# type: ignorecomments needed for platform-specific imports in modern mypy - Cross-platform compatibility is maintained through the ctypes interface
# MyPy should show clean results
mypy snap7 tests example
# Expected: "Success: no issues found in 27 source files"
# Ruff should pass all checks
ruff check snap7 tests example
# Expected: "All checks passed!"
# Tests should pass with high coverage
pytest tests/
# Expected: "188 passed, 4 skipped"- Print statements in production code: Replace with
logger.info()or appropriate log level - Unused type ignore comments: Remove if not needed, or make platform-specific
- Formatting inconsistencies: Run
ruff formatto auto-fix - Type annotation issues: Use strict mypy checking to catch early
- Always use logging instead of print() for debug output (except CLI error messages)
- Test across Python versions when making changes that might affect compatibility
- Use existing patterns for ctypes interactions and error handling
- Follow the established project structure when adding new functionality
- Maintain cross-platform compatibility - test platform-specific code paths
- pyproject.toml: Main project configuration with build, dependencies, and tool settings
- tox.ini: Multi-environment testing configuration
- .pre-commit-config.yaml: Pre-commit hooks for code quality
- Ruff: Line length set to 130, targets Python 3.10+
- MyPy: Strict mode enabled with specific error code exceptions
- Protocol exclusion: snap7/protocol.py is excluded from some linting due to generated bindings
- Error wrapping: Uses
@error_wrapdecorator for consistent error handling - Type safety: Extensive use of ctypes with proper type annotations
- Platform abstraction: Single codebase works across Windows/Linux/macOS
- Modular design: Clear separation between client, server, utilities, and protocol layers
- Adding new PLC operations: Extend client.py with proper error handling and logging
- Utility functions: Add to appropriate modules in snap7/util/ following existing patterns
- Type definitions: Update snap7/type.py for new enums or structures
- Cross-platform testing: Use tox environments or manual virtual environment testing