Skip to content

Function returning Awaitable[R] returns T | None on Python 3.13 but T on Python 3.12 #2131

@yamaaaaaa31

Description

@yamaaaaaa31

Description

When using a function decorated with @overload as a method decorator, ty incorrectly infers the return type as T | None on Python 3.13, but correctly infers T on Python 3.12.

Reproduction

# log_fn.py
import asyncio
from collections.abc import Awaitable, Callable
from functools import wraps
from typing import Any, ParamSpec, TypeVar, overload

P = ParamSpec("P")
R = TypeVar("R")

@overload
def log_fn(fn: Callable[P, Awaitable[R]]) -> Callable[P, Awaitable[R]]: ...
@overload
def log_fn(fn: Callable[P, R]) -> Callable[P, R]: ...
def log_fn(fn: Any) -> Any:
    if asyncio.iscoroutinefunction(fn):
        @wraps(fn)
        async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
            return await fn(*args, **kwargs)
        return async_wrapper
    @wraps(fn)
    def wrapper(*args: Any, **kwargs: Any) -> Any:
        return fn(*args, **kwargs)
    return wrapper
# use_case.py
from dataclasses import dataclass
from injector import inject
from log_fn import log_fn

@dataclass
class Result:
    value: str

@inject
@dataclass
class UseCase:
    @log_fn
    async def run(self) -> Result:
        return Result(value="test")
# router.py
from use_case import UseCase, Result

async def main():
    result = await UseCase().run()
    print(result.value)  # Error on 3.13: possibly-missing-attribute

Commands

ty check --python-version 3.12 router.py  # All checks passed!
ty check --python-version 3.13 router.py  # Error: Attribute 'value' may be missing on object of type 'Result | None'

Expected

No error on Python 3.13 (same behavior as Python 3.12)

Actual

possibly-missing-attribute and invalid-argument-type errors on Python 3.13:

error[possibly-missing-attribute]: Attribute 'value' may be missing on object of type 'Result | None'

Environment

  • ty version: 0.0.4
  • OS: macOS

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggenericsBugs or features relating to ty's generics implementation

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions