Skip to content

Add type hints and modernize code style with ruff/mypy#130

Merged
ad-m merged 4 commits intomasterfrom
claude/add-github-actions-ci-75v3Y
Mar 7, 2026
Merged

Add type hints and modernize code style with ruff/mypy#130
ad-m merged 4 commits intomasterfrom
claude/add-github-actions-ci-75v3Y

Conversation

@ad-m
Copy link
Owner

@ad-m ad-m commented Mar 7, 2026

Summary

This PR modernizes the codebase by adding comprehensive type hints throughout the project and applying consistent code formatting using ruff and mypy. The changes improve code quality, maintainability, and IDE support while maintaining full backward compatibility.

Key Changes

Type Hints & Type Safety

  • Added complete type annotations to all function signatures in base.py, tasks.py, and exceptions.py
  • Added type hints to class attributes (e.g., client: AnticaptchaClient, task_id: int)
  • Added assertions in Job methods to help type checkers understand _last_result is not None
  • Updated return type of AnticaptchaClient.__exit__() to Literal[False] for accuracy
  • Replaced Union types with modern | syntax (e.g., str | Path | bytes | BinaryIO)

Code Formatting & Style

  • Organized imports alphabetically and by category (stdlib, third-party, local)
  • Converted .format() calls to f-strings throughout the codebase
  • Fixed line length and formatting to match ruff standards
  • Removed unnecessary Union imports in favor of | operator
  • Improved code consistency across examples and tests

Bug Fixes & Improvements

  • Fixed report_incorrect() to pass task_id parameter to reportIncorrectImage()
  • Added missing stacklevel=2 to deprecation warning
  • Added missing exception raise in createTaskSmee() when no matching response received
  • Improved boolean logic in reportIncorrectImage() using bool() conversion
  • Fixed variable naming in createTaskSmee() (tasktask_data to avoid shadowing)

Tooling & Configuration

  • Added .pre-commit-config.yaml with ruff and mypy hooks
  • Added ruff and mypy configuration to pyproject.toml
  • Added lint and typecheck targets to Makefile
  • Added GitHub Actions workflow for linting
  • Added tox environment for lint testing
  • Replaced flake8/black Docker-based linting with native ruff commands

Documentation

  • Removed unnecessary # -*- coding: utf-8 -*- headers
  • Removed unnecessary u"" string prefixes in docs/conf.py
  • Improved code examples with modern Python syntax

Notable Implementation Details

  • Type hints use Python 3.9+ syntax (matching the project's minimum version)
  • All assertions added to Job methods are strategically placed after _update() calls to ensure _last_result is populated
  • The on_check callback parameter now has explicit type signature: Callable[[int, str | None], None] | None
  • mypy configuration includes overrides to ignore missing imports for the requests library

https://claude.ai/code/session_01NzfYD4hhH5nqhu8mgnMXPt

claude added 4 commits March 7, 2026 15:23
- Add .pre-commit-config.yaml with ruff (lint + format) and mypy hooks
- Add ruff and mypy tool configuration to pyproject.toml
- Add prek-action lint job to GitHub Actions workflow
- Replace Docker-based flake8/black lint targets with ruff in Makefile
- Add lint/typecheck environments to tox.ini
- Fix type annotations: properly type Job class attributes, BaseTask.type,
  on_check callback parameter, and __exit__ return type
- Fix ruff issues: remove unused Union import, sort imports, remove
  unicode literals, use f-strings, add stacklevel to warnings.warn
- Exclude examples/ from ruff (documentation scripts with intentional patterns)

https://claude.ai/code/session_01NzfYD4hhH5nqhu8mgnMXPt
…ctions-ci-75v3Y

# Conflicts:
#	python_anticaptcha/base.py
The hook was checking all files (tests, examples, docs) which have
untyped functions. Restrict to the package directory only.

https://claude.ai/code/session_01NzfYD4hhH5nqhu8mgnMXPt
Rename second assignment to create_response to avoid type conflict
with the Response object from the initial HEAD request.

https://claude.ai/code/session_01NzfYD4hhH5nqhu8mgnMXPt
@ad-m ad-m merged commit f6d348c into master Mar 7, 2026
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants