Skip to content

Fix pyrefly type error and modernize type hints in CohenKappa#3495

Merged
vfdev-5 merged 5 commits intomasterfrom
copilot/fix-pyrefly-error-in-cohen-kappa
Jan 19, 2026
Merged

Fix pyrefly type error and modernize type hints in CohenKappa#3495
vfdev-5 merged 5 commits intomasterfrom
copilot/fix-pyrefly-error-in-cohen-kappa

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 19, 2026

Fix pyrefly type error and modernize type hints in CohenKappa

  • Update type annotation for weights parameter from Optional[str] to Literal["linear", "quadratic"] | None
  • Update device parameter type annotation from Union[str, torch.device] to str | torch.device
  • Modernize type hints to use Python 3.10+ syntax (| operator instead of Optional and Union)
  • Removed Optional and Union from typing imports
  • Add explicit type annotation for self.weights attribute to preserve Literal type
  • Fix typo in comment (initalize -> initialize)

Summary

Fixed the pyrefly type checking error by:

  1. Changing the weights parameter type from Optional[str] to Literal["linear", "quadratic"] | None
  2. Adding an explicit type annotation for self.weights to preserve the Literal type through assignment
  3. Modernizing all type hints to use the | syntax
  4. Fixed a spelling error in a comment

The type checker was inferring self.weights as str | None instead of Literal['linear', 'quadratic'] | None because Python doesn't automatically preserve Literal types through assignment. The explicit type annotation on line 72 ensures the type checker recognizes the correct type when self.weights is passed to sklearn's cohen_kappa_score.

Original prompt

This section details on the original issue you should resolve

<issue_title>CI - Fix pyrefly error in ignite/metrics/cohen_kappa.py</issue_title>
<issue_description>Fix pyrefly error:

 INFO Checking project configured at `/home/runner/work/ignite/ignite/pyrefly.toml`
ERROR Argument `str | None` is not assignable to parameter `weights` with type `Literal['linear', 'quadratic'] | None` in function `sklearn.metrics._classification.cohen_kappa_score` [bad-argument-type]
  --> ignite/metrics/cohen_kappa.py:87:58
   |
87 |         return cohen_kappa_score(y_true, y_pred, weights=self.weights)
   |                                                          ^^^^^^^^^^^^
   |
Error: Argument `str | None` is not assignable to parameter `weights` with type `Literal['linear', 'quadratic'] | None` in function `sklearn.metrics._classification.cohen_kappa_score`
```</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.

Copilot AI changed the title [WIP] Fix pyrefly error in ignite/metrics/cohen_kappa.py Fix pyrefly type error in CohenKappa weights parameter Jan 19, 2026
Copilot AI requested a review from vfdev-5 January 19, 2026 09:56
@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Jan 19, 2026

Great! Let's now also modernize the type hints : Optional[A] -> A | None, Union[A, B] -> A | B, Tuple -> tuple, List -> list.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 19, 2026

Great! Let's now also modernize the type hints : Optional[A] -> A | None, Union[A, B] -> A | B, Tuple -> tuple, List -> list.

Done in commit b897611. All type hints in the file have been modernized to use the | syntax.

Copilot AI changed the title Fix pyrefly type error in CohenKappa weights parameter Fix pyrefly type error and modernize type hints in CohenKappa Jan 19, 2026
Copilot AI requested a review from vfdev-5 January 19, 2026 10:16
Copy link
Copy Markdown
Collaborator

@vfdev-5 vfdev-5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@github-actions github-actions bot added the module: metrics Metrics module label Jan 19, 2026
@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Jan 19, 2026

@copilot type checking CI job reports :

 INFO Checking project configured at `/home/runner/work/ignite/ignite/pyrefly.toml`
ERROR Argument `str | None` is not assignable to parameter `weights` with type `Literal['linear', 'quadratic'] | None` in function `sklearn.metrics._classification.cohen_kappa_score` [bad-argument-type]
  --> ignite/metrics/cohen_kappa.py:87:58
   |
87 |         return cohen_kappa_score(y_true, y_pred, weights=self.weights)
   |                                                          ^^^^^^^^^^^^
   |
Error: Argument `str | None` is not assignable to parameter `weights` with type `Literal['linear', 'quadratic'] | None` in function `sklearn.metrics._classification.cohen_kappa_score`

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 19, 2026

@copilot type checking CI job reports :

 INFO Checking project configured at `/home/runner/work/ignite/ignite/pyrefly.toml`

...

Fixed in commit cfc8930. Added explicit type annotation for self.weights attribute to preserve the Literal type:

self.weights: Literal["linear", "quadratic"] | None = weights

The type checker was inferring self.weights as str | None because Python doesn't automatically preserve Literal types through assignment. The explicit annotation ensures the correct type is recognized when passed to sklearn's cohen_kappa_score.

Copilot AI requested a review from vfdev-5 January 19, 2026 10:30
@vfdev-5 vfdev-5 marked this pull request as ready for review January 19, 2026 10:30
@vfdev-5 vfdev-5 enabled auto-merge January 19, 2026 10:31
@vfdev-5 vfdev-5 added this pull request to the merge queue Jan 19, 2026
Merged via the queue into master with commit 69bda91 Jan 19, 2026
26 checks passed
@vfdev-5 vfdev-5 deleted the copilot/fix-pyrefly-error-in-cohen-kappa branch January 19, 2026 11:17
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.

CI - Fix pyrefly error in ignite/metrics/cohen_kappa.py

2 participants