Skip to content

invalid-argument-type on union with pre PEP 695 type parameter #1564

@edgarrmondragon

Description

@edgarrmondragon

Summary

The following is accepted by mypy and was also working in previous versions of ty:

from typing import Any, Generic, TypeAlias, TypeVar, Union

_JsonValue: TypeAlias = str | int | float | bool | list | dict | None

T = TypeVar("T", bound=_JsonValue)


class JSONTypeHelper(Generic[T]):
    def __init__(self) -> None: ...


class StringType(JSONTypeHelper[str]):
    def __init__(self) -> None: ...


W = TypeVar("W", bound=JSONTypeHelper)


class ArrayType(JSONTypeHelper[list], Generic[W]):
    def __init__(self, wrapped_type: W | type[W], **kwargs: Any) -> None: ...


array = ArrayType(StringType)
# Argument to bound method `__init__` is incorrect: Expected `ArrayType[W@ArrayType]`, found `ArrayType[<class 'StringType'>]` (invalid-argument-type) [Ln 23, Col 9]

https://play.ty.dev/bddec81e-101a-4c8b-818b-5012af6dec2e

Curiously, using PEP 695 seems to make ty happy, though unfortunately I need to support Python 3.10+:

from typing import Any, TypeAlias, Union

_JsonValue: TypeAlias = str | int | float | bool | list | dict | None


class JSONTypeHelper[T: _JsonValue]():
    def __init__(self) -> None: ...


class StringType(JSONTypeHelper[str]):
    def __init__(self) -> None: ...


class ArrayType[W: JSONTypeHelper](JSONTypeHelper[list]):
    def __init__(self, wrapped_type: W | type[W], **kwargs: Any) -> None: ...


array = ArrayType(StringType)

https://play.ty.dev/5369dde9-a05c-4788-86c2-8d7291f6d85f

Version

ty 0.0.1-alpha.26 (b225fd8 2025-11-10)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions