Skip to content

refactor Optional[Union[...]] w | for utils.py;#3482

Merged
vfdev-5 merged 3 commits intopytorch:masterfrom
maifeeulasad:refactor-3481-1-maifee
Jan 15, 2026
Merged

refactor Optional[Union[...]] w | for utils.py;#3482
vfdev-5 merged 3 commits intopytorch:masterfrom
maifeeulasad:refactor-3481-1-maifee

Conversation

@maifeeulasad
Copy link
Copy Markdown
Contributor

@maifeeulasad maifeeulasad commented Jan 14, 2026

Related to #3481

Description:

  • Removed un-used from typing imports
  • Optional[Union[str, torch.device]]str | torch.device | None
  • Optional[Union[int, str]]int | str | None
  • Optional[str]str | None
  • Optional[TextIO]TextIO | None
  • Optional[int]int | None
  • Tuple -> tuple, List -> list, Dict -> dict, Type -> type

Tested w python -m pytest tests/ignite/test_utils.py -x --tb=line --disable-warnings -q

NB: The Union[Any, "_CollectionItem"] was kept as-is since Any doesn't work with the | operator syntax.

Check list:

  • New tests are added (if a new feature is added)
  • New doc strings: description and/or example code are in RST format
  • Documentation is updated (if required)

@maifeeulasad
Copy link
Copy Markdown
Contributor Author

After 0f1d178:

  • Added from __future__ import annotations, explained later
  • Removed Union from imports
  • Replaced all Union[A, B] with A | B
  • Replaced all Optional[A] with A | None

from __future__ import annotations explaination

We needed from __future__ import annotations because of PEP 563 - Postponed Evaluation of Annotations.

Without it, Python evaluates type annotations at runtime when the module is imported. This caused the error:

def wrap(object: Dict | List, key: int | str, value: Any) -> Any | "_CollectionItem":
                                                             ^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unsupported operand type(s) for |: '_AnyMeta' and 'str'

The problem: Python tried to execute Any | "_CollectionItem" at import time, but Any (from typing) doesn't support the | operator with strings.

With from __future__ import annotations:

  • All annotations are stored as strings and not evaluated at import time
  • They're only evaluated when explicitly needed (by type checkers, IDEs, or get_type_hints())
  • This allows:
    • Forward references (like "_CollectionItem" before the class is defined)
    • Complex type expressions that might not be valid at runtime
    • Faster import times (annotations aren't evaluated)

This becomes the default behavior in Python 3.11+, but for Python 3.10 we need the __future__ import to enable it.

@maifeeulasad maifeeulasad requested a review from vfdev-5 January 14, 2026 23:11
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.

Thanks for working on the PR!

@maifeeulasad
Copy link
Copy Markdown
Contributor Author

@vfdev-5 thanks for giving me the opportunity to work on this one! And reviewing my work so many times.

@maifeeulasad maifeeulasad requested a review from vfdev-5 January 15, 2026 08:27
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, thanks @maifeeulasad !

@vfdev-5 vfdev-5 enabled auto-merge January 15, 2026 08:50
@vfdev-5 vfdev-5 added this pull request to the merge queue Jan 15, 2026
Merged via the queue into pytorch:master with commit 090587b Jan 15, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: utils Utils module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants