Skip to content

Fix pyrefly type errors and modernize type hints in LR scheduler classes#3500

Merged
vfdev-5 merged 6 commits intomasterfrom
copilot/fix-ci-typing-issue
Jan 26, 2026
Merged

Fix pyrefly type errors and modernize type hints in LR scheduler classes#3500
vfdev-5 merged 6 commits intomasterfrom
copilot/fix-ci-typing-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 26, 2026

Typing Fixes for pyrefly errors ✅

Original Issues (All Fixed)

  • Fix _ExponentialLR.base_lrs assignment type issue (lr_finder.py:543)
  • Fix _ExponentialLR.get_lr() return type to match parent class (lr_finder.py:545)
  • Fix _ExponentialLR.get_lr() list comprehension return type (lr_finder.py:548)
  • Fix _CosineAnnealingWarmRestarts.get_lr() return type (param_scheduler.py:900)
  • Fix LRScheduler.get_param() return type for single element (param_scheduler.py:998)
  • Fix LRScheduler.get_param() return type for multiple elements (param_scheduler.py:1000)

Additional Improvements

  • Import LRScheduler directly from torch.optim.lr_scheduler (no try/except)
  • Use PyTorchLRScheduler alias to avoid naming conflict with ignite's LRScheduler
  • 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

  • Pyrefly check: All 6 original errors fixed, 0 errors in our files
  • Syntax validation: All files compile successfully
  • Code review: No issues found
  • 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.

Original prompt

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>


💡 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.

@github-actions github-actions bot added the module: handlers Core Handlers module label Jan 26, 2026
Copilot AI changed the title [WIP] Fix CI typing issue in ignite/handlers/param_scheduler.py Fix pyrefly type errors in LR scheduler classes Jan 26, 2026
Copilot AI requested a review from vfdev-5 January 26, 2026 16:03
Copilot AI requested a review from vfdev-5 January 26, 2026 16:30
@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Jan 26, 2026

@copilot ignite/handlers/lr_finder.py:13-17 just import from torch.optim.lr_scheduler import LRScheduler without try/except and replace in class _ExponentialLR(_LRScheduler):

Also change the old typing to the new typing everywhere in the file. For example: Dict[str, List[Any]] -> dict[str, list[Any]]

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 26, 2026

@copilot ignite/handlers/lr_finder.py:13-17 just import from torch.optim.lr_scheduler import LRScheduler without try/except and replace in class _ExponentialLR(_LRScheduler):

Also change the old ...

Done in commit 8d4f2ab.

  • Removed the try/except block and changed to direct import: from torch.optim.lr_scheduler import LRScheduler as PyTorchLRScheduler
  • Updated class definition: class _ExponentialLR(PyTorchLRScheduler)
  • Modernized all type hints throughout the file (Union→|, Optional→|None, List→list, Dict→dict)
  • Removed unused typing imports

@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Jan 26, 2026

@copilot Great!
Now do the typing replacement in ignite/handlers/param_scheduler.py as well

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 26, 2026

@copilot Great!
Now do the typing replacement in ignite/handlers/param_scheduler.py as well

Done in commit e068938. Modernized all type hints in param_scheduler.py to Python 3.10+ syntax:

  • Removed unused typing imports (Dict, List, Optional, Union, Tuple)
  • Converted 50+ instances: Union→|, Optional→|None, List→list, Dict→dict, Tuple→tuple throughout the entire file

Copilot AI changed the title Fix pyrefly type errors in LR scheduler classes Fix pyrefly type errors and modernize type hints in LR scheduler classes Jan 26, 2026
@vfdev-5 vfdev-5 marked this pull request as ready for review January 26, 2026 16:52
@vfdev-5 vfdev-5 added this pull request to the merge queue Jan 26, 2026
Merged via the queue into master with commit 44e4a50 Jan 26, 2026
27 of 29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: handlers Core Handlers module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CI typing issue with ignite/handlers/param_scheduler.py and ignite/handlers/lr_finder.py

2 participants