Skip to content

ty should error on Union syntax in function definition without annotations import in Python 3.9 #449

@metaist

Description

@metaist

Summary

Splitting this off from #437. Although UnionType was introduced in Python 3.10, it is possible to use the union syntax in function definitions (but not in type aliases or elsewhere) in Python 3.9 if you include from __future__ import annotations

mypy catches this; ty does not.

https://play.ty.dev/5c47f2d5-ef8c-4d2f-b227-f93178b29e57

def f(x: int|str) -> None: ...
$ uvx --python 3.9 mypy --strict test.py
test.py:1: error: X | Y syntax for unions requires Python 3.10  [syntax]
Found 1 error in 1 file (checked 1 source file)

$ uvx ty check --python-version 3.9 test.py
All checks passed!

https://play.ty.dev/23b020e7-4d07-49d7-9671-9918244dad1c

from __future__ import annotations
def f(x: int|str) -> None: ...
$ uvx --python 3.9 mypy --strict test.py
Success: no issues found in 1 source file

$ uvx ty check --python-version 3.9 test.py
All checks passed!

Version

ty 0.0.1-alpha.5 (4ad13f2 2025-05-17)

Metadata

Metadata

Assignees

No one assigned

    Labels

    diagnosticsRelated to reporting of diagnostics.runtime semanticsAccurate modeling of how Python's semantics work at runtime

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions