Skip to content

Revert PEP 604 union syntax to Union[]/Optional[] for Python 3.9 compatibility#1228

Merged
bact merged 14 commits intodevfrom
copilot/revert-pep604-union-types
Jan 27, 2026
Merged

Revert PEP 604 union syntax to Union[]/Optional[] for Python 3.9 compatibility#1228
bact merged 14 commits intodevfrom
copilot/revert-pep604-union-types

Conversation

Copy link
Contributor

Copilot AI commented Jan 26, 2026

What does this changes

Converts all PEP 604 union type syntax (X | Y) to traditional Union[X, Y] and Optional[X] across 46 files.

What was wrong

PEP 604 syntax (str | None, list[str] | str) requires Python 3.10+. While from __future__ import annotations allows the syntax to parse in 3.9, it stores annotations as strings and can cause runtime failures in type checking or introspection. The project's requires-python = ">=3.9" demands full compatibility.

How this fixes it

  • X | NoneOptional[X]
  • X | YUnion[X, Y]
  • X | Y | ZUnion[X, Y, Z]

Added typing imports (Union, Optional) to affected modules, merged with existing imports alphabetically. Preserved from __future__ import annotations for forward compatibility.

Example transformation:

# Before
def dependency_parsing(
    text: str,
    model: str | None = None,
) -> list[list[str]] | str:

# After
from typing import List, Optional, Union

def dependency_parsing(
    text: str,
    model: Optional[str] = None,
) -> Union[List[List[str]], str]:

Affected modules: tokenize, parse, translate, spell, summarize, tag, transliterate, util, corpus, and 10+ others.

Your checklist for this pull request

  • Passed code styles and structures
  • Passed code linting checks and unit test
Original prompt

Revert all the PEP 604 (Union Types as X | Y) to Union[] (or Optional[], for | None), to keep Python 3.9 support.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 10 commits January 26, 2026 22:01
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
…bility

- Add Union and Optional imports from typing module
- Convert X | None to Optional[X]
- Convert X | Y to Union[X, Y]
- Keep imports sorted alphabetically
- Maintain __future__ annotations import

Co-authored-by: bact <128572+bact@users.noreply.github.com>
…compatibility

Co-authored-by: bact <128572+bact@users.noreply.github.com>
Copilot AI changed the title [WIP] Revert PEP 604 union types to Union[] for Python 3.9 support Revert PEP 604 union syntax to Union[]/Optional[] for Python 3.9 compatibility Jan 26, 2026
Copilot AI requested a review from bact January 26, 2026 22:30
bact added 3 commits January 27, 2026 08:14
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
This reverts commit 62ff5c8.
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
@sonarqubecloud
Copy link

@coveralls
Copy link

coveralls commented Jan 27, 2026

Coverage Status

coverage: 53.556% (-0.06%) from 53.615%
when pulling 273c396 on copilot/revert-pep604-union-types
into 9e6520b on dev.

@bact bact marked this pull request as ready for review January 27, 2026 08:25
@bact bact added bug bugs in the library refactoring a technical improvement which does not add any new features or change existing features. labels Jan 27, 2026
@bact bact merged commit e4c6f6b into dev Jan 27, 2026
29 of 32 checks passed
@bact bact deleted the copilot/revert-pep604-union-types branch January 27, 2026 08:26
@bact bact added this to the 5.3 milestone Jan 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug bugs in the library refactoring a technical improvement which does not add any new features or change existing features.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants