Skip to content

Improve NUMERIC protocol typing with proper variance and missing operators#273

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/improve-numeric-class-typing
Draft

Improve NUMERIC protocol typing with proper variance and missing operators#273
Copilot wants to merge 5 commits intomainfrom
copilot/improve-numeric-class-typing

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 2, 2025

Improve typing of the NUMERIC class

Analysis Complete: The NUMERIC Protocol class has several typing issues that can be improved.

Issues Identified:

  • Import sorting issue (ruff I001)
  • Parameters typed as NUMERIC instead of object (too restrictive, doesn't match Python's duck typing)
  • Missing common numeric operators (__neg__, __pos__, __abs__, __pow__, __floordiv__, __mod__)
  • Missing comparison operators (__lt__, __le__, __gt__, __ge__)
  • Not marked as @runtime_checkable (best practice for protocols)
  • Could use Self type for better type inference (requires typing_extensions for Python 3.10+)
  • Pre-commit checks fixed (removed unused pytest import)

Changes Made:

1. Enhanced conjugate/_typing.py:

  • Fixed imports: Sorted imports properly and added runtime_checkable
  • Added Self type: Uses typing.Self (Python 3.11+) with fallback to typing_extensions.Self (Python 3.10)
  • Improved parameter types: Changed from NUMERIC to object for proper contravariance, matching Python's duck typing
  • Added missing operators:
    • Floor division: __floordiv__, __rfloordiv__
    • Modulo: __mod__, __rmod__
    • Power: __pow__, __rpow__
    • Unary: __neg__, __pos__, __abs__
    • Comparison: __lt__, __le__, __gt__, __ge__
  • Added documentation: Comprehensive docstring explaining the protocol's design and complex number exclusion
  • Made runtime checkable: Added @runtime_checkable decorator for isinstance() checks
  • Omitted __eq__, __ne__, __hash__: Intentionally excluded to support numpy arrays (which have __hash__ = None)
  • Clarified comments: Updated comment to accurately reflect that __eq__ and __ne__ are inherited from object

2. Added dependency in pyproject.toml:

  • Added typing-extensions>=4.0.0 to support Python 3.10

3. Created comprehensive tests in tests/test_typing.py:

  • Tests for protocol conformance (int, float, numpy arrays, etc.)
  • Tests for all arithmetic operations
  • Tests for reverse arithmetic operations
  • Tests for unary operations
  • Tests for comparison operations
  • Tests for mixed-type arithmetic
  • Tests for numpy broadcasting
  • Tests for type aliases
  • Documentation of complex number exclusion (they don't support // and %)

Design Decisions:

  • Complex numbers excluded: Complex numbers don't support floor division and modulo, so they don't conform to this protocol
  • __hash__ omitted: Allows numpy arrays (unhashable) to conform to the protocol
  • object for parameters: Enables duck typing (e.g., int + float works)

Verification:

  • All 200 tests pass (including 17 new typing tests)
  • Ruff linting passes with ALL rules enabled
  • mypy strict mode passes
  • pyright type checking passes
  • Protocol works with int, float, numpy scalars, and numpy arrays
  • Runtime isinstance() checks work correctly
  • Code review feedback addressed
  • Pre-commit checks pass
Original prompt

This section details on the original issue you should resolve

<issue_title>Improve typing of the NUMERIC class</issue_title>
<issue_description>The NUMERIC class doesn't seems to do the job completely and
has a lot of warnings and errors with the ruff linter. This can be improved</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Nov 2, 2025
Copilot AI and others added 2 commits November 2, 2025 13:19
Co-authored-by: williambdean <57733339+williambdean@users.noreply.github.com>
…umber exclusion

Co-authored-by: williambdean <57733339+williambdean@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve typing for NUMERIC class and resolve linter errors Improve NUMERIC protocol typing with proper variance and missing operators Nov 2, 2025
Copilot AI requested a review from williambdean November 2, 2025 13:24
@codecov
Copy link
Copy Markdown

codecov bot commented Nov 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.03%. Comparing base (45a453a) to head (0d36a13).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #273      +/-   ##
==========================================
+ Coverage   95.00%   95.03%   +0.02%     
==========================================
  Files           9        9              
  Lines        1082     1087       +5     
==========================================
+ Hits         1028     1033       +5     
  Misses         54       54              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@williambdean
Copy link
Copy Markdown
Owner

@copilot this is not passing pre-commit. Please run pre-commit run --all-files and fix any issues.

Co-authored-by: williambdean <57733339+williambdean@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Nov 20, 2025

@copilot this is not passing pre-commit. Please run pre-commit run --all-files and fix any issues.

Fixed in commit 013a55a. The pre-commit hook removed an unused pytest import from tests/test_typing.py. All pre-commit checks now pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve typing of the NUMERIC class

2 participants