Skip to content

Modernize type hints in accuracy.py to Python 3.10+ syntax#3497

Merged
vfdev-5 merged 2 commits intomasterfrom
copilot/replace-typing-hints-version
Jan 19, 2026
Merged

Modernize type hints in accuracy.py to Python 3.10+ syntax#3497
vfdev-5 merged 2 commits intomasterfrom
copilot/replace-typing-hints-version

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 19, 2026

Replace deprecated typing syntax with modern Python 3.10+ union syntax in ignite/metrics/accuracy.py.

Changes:

  • Union[A, B]A | B
  • Optional[A]A | None
  • Tuple[int, ...]tuple[int, ...]
  • Removed unused typing imports: Union, Optional, Tuple

Example:

# Before
from typing import Callable, Optional, Sequence, Tuple, Union

def __init__(
    self,
    device: Union[str, torch.device] = torch.device("cpu"),
):
    self._type: Optional[str] = None
    y_pred_shape: Tuple[int, ...] = y_pred.shape

# After
from typing import Callable, Sequence

def __init__(
    self,
    device: str | torch.device = torch.device("cpu"),
):
    self._type: str | None = None
    y_pred_shape: tuple[int, ...] = y_pred.shape

Part of broader effort to modernize type hints across the codebase. Python 3.9 is EOL; minimum supported version is 3.10.

Original prompt

This section details on the original issue you should resolve

<issue_title>Replace typing hints with new version</issue_title>
<issue_description>Typing improvements we can make the change: Optional[Union[int, float]] = None into int | float = None which should be OK starting from python 3.10

We should replace all:

  • Union[A, B] with A | B
  • Optional[A] with A | None
  • Tuple -> tuple, List -> list, etc
    in the function signature and also of the output type hint

python 3.9 is EOL, min supported python version is 3.10.

It is a large issue, multiple people can work on it.
If you plan to work on it. Make changes in a single file and submit a PR. Smaller PR == easy review == quicker merging.
</issue_description>

<agent_instructions>Make sure to send one PR per one changed file. Start with files from ignite/tree/master/ignite/metrics folder.
PR should not close this issue</agent_instructions>

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

@vfdev-5 It is a large issue, multiple people can work on it. I wont assign anyone on it. @vfdev-5 I updated the description, we should just replace all old type hints with the new one: Optional and Union in this issue.

💡 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 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
Copilot AI requested a review from vfdev-5 January 19, 2026 12:17
@github-actions github-actions bot added the module: metrics Metrics module label Jan 19, 2026
@vfdev-5 vfdev-5 marked this pull request as ready for review January 19, 2026 12:29
@vfdev-5 vfdev-5 enabled auto-merge January 19, 2026 12:29
@vfdev-5 vfdev-5 added this pull request to the merge queue Jan 19, 2026
Merged via the queue into master with commit 3c5bf86 Jan 19, 2026
26 checks passed
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: metrics Metrics module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants