Skip to content

Modernize to Python 3.9: type hints with future annotations, exception handling, and code quality - #1189

Merged
bact merged 3 commits into
devfrom
copilot/update-python-type-hints
Jan 10, 2026
Merged

Modernize to Python 3.9: type hints with future annotations, exception handling, and code quality#1189
bact merged 3 commits into
devfrom
copilot/update-python-type-hints

Conversation

Copilot AI commented Jan 10, 2026

Copy link
Copy Markdown
Contributor

What does this changes

Modernizes codebase to Python 3.9 standards using from __future__ import annotations for type hints, improves exception handling, and removes obsolete Python 2 artifacts.

Type hints (191 files):

  • Added from __future__ import annotations to enable PEP 604 syntax (X | None, X | Y) in Python 3.9
  • Replaced typing module generics with builtins: Listlist, Dictdict, Optional[X]X | None, Union[X, Y]X | Y
  • Removed 197 unnecessary typing imports; retained only Literal, Protocol, TypeVar, etc.

Code quality:

  • Removed # -*- coding: utf-8 -*- headers (188 files) - obsolete in Python 3
  • Fixed None comparisons: != Noneis not None (2 instances)
  • Replaced generic Exception with RuntimeError and added exception chaining (2 instances)
  • Fixed circular imports from ruff sort by restoring critical import order in corpus/__init__.py and util/__init__.py

Configuration:

  • Updated pyproject.toml ruff target to py39

Example:

Before:

from typing import Optional, List, Union

def process(data: Optional[str], items: List[int]) -> Union[Dict[str, int], None]:
    pass

After:

from __future__ import annotations

def process(data: str | None, items: list[int]) -> dict[str, int] | None:
    pass

What was wrong

Codebase used outdated Python 3.7-style type hints with explicit typing imports, retained Python 2 coding headers, and had generic exception handling without proper chaining.

How this fixes it

PEP 563's from __future__ import annotations stores type hints as strings, enabling PEP 604 pipe syntax in Python 3.9 without runtime evaluation. Automated with ruff UP rules, then manually fixed import order for circular dependency resolution.

Your checklist for this pull request

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

Use consistent Python 3.9 type hints and use appropriate Exceptions

The codebase now requires at least Python 3.9, so let's use Python 3.9 features when possible. Do not use any language feature that do not available in Python 3.9.

Update type hints:

  • Update the type hints to Python 3.9 style for the whole codebase
  • Recheck the docstring to use matching Python 3.9 hinting style (few now use | for Union which is not yet available in Python 3.9)
  • Use future imports annotation when possible and appropriate
  • After code update, remove unnecessary typing imports
  • Move the typing imports into its own block
  • Recheck that the codebase does not use any features that not available in Python 3.9

Update exceptions:

  • Fix too generic exceptions
  • Use "from exception" when appropriate

Use appropriate None comparison:

  • Replace != and == with "is not" and "is" as appropriate

Code formatting:

  • Format the code and sort imports with Ruff
  • Remove # -- coding: utf-8 -- from headers
  • When insert imports, place it below the """ docstring for file, to avoid Pylint warnings

💡 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 and others added 2 commits January 10, 2026 22:29
…e annotations, update type hints

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

Copy link
Copy Markdown

Copilot AI changed the title [WIP] Update codebase for Python 3.9 type hints and exceptions Modernize to Python 3.9: type hints with future annotations, exception handling, and code quality Jan 10, 2026
Copilot AI requested a review from bact January 10, 2026 22:38
@bact bact added the refactoring a technical improvement which does not add any new features or change existing features. label Jan 10, 2026
@bact
bact marked this pull request as ready for review January 10, 2026 22:42
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 52.878% (+0.09%) from 52.793%
when pulling 01a0a1f on copilot/update-python-type-hints
into 6439d0d on dev.

@bact
bact merged commit da8c3b6 into dev Jan 10, 2026
43 of 44 checks passed
@bact
bact deleted the copilot/update-python-type-hints branch January 10, 2026 22:47
@bact bact mentioned this pull request Jan 11, 2026
@bact bact added this to the 5.3 milestone Jan 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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