Modernize type hints in accuracy.py to Python 3.10+ syntax#3497
Merged
Modernize type hints in accuracy.py to Python 3.10+ syntax#3497
Conversation
Co-authored-by: vfdev-5 <[email protected]>
Copilot
AI
changed the title
[WIP] Update typing hints to new version syntax
Modernize type hints in accuracy.py to Python 3.10+ syntax
Jan 19, 2026
vfdev-5
approved these changes
Jan 19, 2026
13 tasks
github-merge-queue bot
pushed a commit
that referenced
this pull request
Jan 26, 2026
…ses (#3500) ## Typing Fixes for pyrefly errors ✅ ### Original Issues (All Fixed) - [x] Fix `_ExponentialLR.base_lrs` assignment type issue (lr_finder.py:543) - [x] Fix `_ExponentialLR.get_lr()` return type to match parent class (lr_finder.py:545) - [x] Fix `_ExponentialLR.get_lr()` list comprehension return type (lr_finder.py:548) - [x] Fix `_CosineAnnealingWarmRestarts.get_lr()` return type (param_scheduler.py:900) - [x] Fix `LRScheduler.get_param()` return type for single element (param_scheduler.py:998) - [x] Fix `LRScheduler.get_param()` return type for multiple elements (param_scheduler.py:1000) ### Additional Improvements - [x] Import `LRScheduler` directly from torch.optim.lr_scheduler (no try/except) - [x] Use `PyTorchLRScheduler` alias to avoid naming conflict with ignite's LRScheduler - [x] **Modernize ALL type hints to Python 3.10+ syntax in both files**: - `Union[A, B]` → `A | B` - `Optional[A]` → `A | None` - `List[T]` → `list[T]` - `Dict[K, V]` → `dict[K, V]` - `Tuple[T, ...]` → `tuple[T, ...]` ### Changes Made 1. **lr_finder.py**: - Import: Removed try/except block, direct import as `from torch.optim.lr_scheduler import LRScheduler as PyTorchLRScheduler` - Class definition: Changed `class _ExponentialLR(_LRScheduler)` → `class _ExponentialLR(PyTorchLRScheduler)` - Removed unused typing imports: `Dict`, `List`, `Optional`, `Union` - Updated **all type hints throughout the entire file** to modern Python 3.10+ syntax 2. **param_scheduler.py**: - Removed unused typing imports: `Dict`, `List`, `Optional`, `Union`, `Tuple` - Updated **all type hints throughout the entire file** to modern Python 3.10+ syntax - Converted **50+ instances** of old typing syntax across the entire file - Line 872: `int | None` and `list[torch.Tensor | float]` for `_CosineAnnealingWarmRestarts.get_lr()` - Line 991: `torch.Tensor | float | list[torch.Tensor | float]` for `LRScheduler.get_param()` ### Verification - [x] Pyrefly check: All 6 original errors fixed, 0 errors in our files - [x] Syntax validation: All files compile successfully - [x] Code review: No issues found - [x] Security scan (CodeQL): No vulnerabilities detected ### Result All typing errors resolved with **comprehensive modernization of type hints to Python 3.10+ syntax throughout both files**, following project conventions established in PR #3497. <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>CI typing issue with ignite/handlers/param_scheduler.py</issue_title> > <issue_description>There is the following typing error revealed by pyrefly: > ``` > INFO Checking project configured at `/home/runner/work/ignite/ignite/pyrefly.toml` > ERROR `list[float]` is not assignable to attribute `base_lrs` with type `list[Tensor | float]` [bad-assignment] > --> ignite/handlers/lr_finder.py:543:25 > | > 543 | self.base_lrs = start_lrs > | ^^^^^^^^^ > | > ERROR Class member `_ExponentialLR.get_lr` overrides parent class `_LRScheduler` in an inconsistent manner [bad-override] > --> ignite/handlers/lr_finder.py:545:9 > | > 545 | def get_lr(self) -> List[float]: > | ^^^^^^ > | > `_ExponentialLR.get_lr` has type `BoundMethod[_ExponentialLR, (self: _ExponentialLR) -> list[float]]`, which is not assignable to `BoundMethod[_ExponentialLR, (self: _ExponentialLR) -> list[Tensor | float]]`, the type of `_LRScheduler.get_lr` > ERROR Returned type `list[Tensor | Unknown]` is not assignable to declared return type `list[float]` [bad-return] > --> ignite/handlers/lr_finder.py:548:16 > | > 548 | return [base_lr * (end_lr / base_lr) ** r for end_lr, base_lr in zip(self.end_lrs, self.base_lrs)] > | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > | > ERROR Returned type `list[Tensor | float]` is not assignable to declared return type `list[float]` [bad-return] > --> ignite/handlers/param_scheduler.py:900:16 > | > 900 | return [ > | ________________^ > 901 | | eta_min > 902 | | + (base_lr - eta_min) * (1 + math.cos(math.pi * self._lr_scheduler.T_cur / self._lr_scheduler.T_i)) / 2 > 903 | | for base_lr in self._lr_scheduler.base_lrs > 904 | | ] > | |_________^ > | > ERROR Returned type `Tensor | float` is not assignable to declared return type `float | list[float]` [bad-return] > --> ignite/handlers/param_scheduler.py:998:20 > | > 998 | return lr_list[0] > | ^^^^^^^^^^ > | > ERROR Returned type `list[Tensor | float] | list[float]` is not assignable to declared return type `float | list[float]` [bad-return] > --> ignite/handlers/param_scheduler.py:1000:20 > | > 1000 | return lr_list > | ^^^^^^^ > | > Error: `list[float]` is not assignable to attribute `base_lrs` with type `list[Tensor | float]` > Error: Class member `_ExponentialLR.get_lr` overrides parent class `_LRScheduler` in an inconsistent manner > `_ExponentialLR.get_lr` has type `BoundMethod[_ExponentialLR, (self: _ExponentialLR) -> list[float]]`, which is not assignable to `BoundMethod[_ExponentialLR, (self: _ExponentialLR) -> list[Tensor | float]]`, the type of `_LRScheduler.get_lr` > Error: Returned type `list[Tensor | Unknown]` is not assignable to declared return type `list[float]` > Error: Returned type `list[Tensor | float]` is not assignable to declared return type `list[float]` > Error: Returned type `Tensor | float` is not assignable to declared return type `float | list[float]` > Error: Returned type `list[Tensor | float] | list[float]` is not assignable to declared return type `float | list[float]` > INFO 6 errors (102 suppressed) > Error: Process completed with exit code 1. > ```</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> <!-- START COPILOT CODING AGENT SUFFIX --> - Fixes #3499 <!-- START COPILOT CODING AGENT TIPS --> --- 💡 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](https://gh.io/copilot-coding-agent-tips) in the docs. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: vfdev-5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace deprecated typing syntax with modern Python 3.10+ union syntax in
ignite/metrics/accuracy.py.Changes:
Union[A, B]→A | BOptional[A]→A | NoneTuple[int, ...]→tuple[int, ...]Union,Optional,TupleExample:
Part of broader effort to modernize type hints across the codebase. Python 3.9 is EOL; minimum supported version is 3.10.
Original prompt
💡 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.