pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

Interaction with deferred evaluation of types

Open legendre6891 opened this issue 9 months ago • 2 comments

Describe the Bug

Right now, this code typechecks correctly, but will error out at runtime.

from typing import Optional, Union
from dataclasses import dataclass

@dataclass
class Leaf:
    value: int

@dataclass
class Node:
    left: Optional[Tree]
    right: Optional[Tree]

type Tree = Union[Leaf, Node]

The issue is that the annotations left: Optional[Tree] and right: Optional[Tree] are invalid until PEP649 (deferred annotations) is introduced in Python 3.14.

So this code is incorrect in Python ≤3.13 and correct in ≥3.14. Right now it passes pyrefly -- is this the expected behavior?

Sandbox Link

https://pyrefly.org/sandbox/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoDyCMSYKAhgDYA0UAqiqSgFCiRQAm5M5AxpeQDOggKaDM2XPi49+Qwc2YABGXwHDmc4VAAyI8sABczKKagA3KgFcRhzChiKV3NfM3rxAOTAdbJs5QiwDB2RCRkVADaACogIiIAuv6mIEhoABYhhMRMUbHxSczwCCJQ+aUAvPSMZJF6BrTevglAA

(Only applicable for extension issues) IDE Information

No response

legendre6891 avatar May 25 '25 21:05 legendre6891

Thanks for raising this. We don't currently have any gating for what type forms are supported in which versions, aside from version restrictions in Ruff's parser and typeshed, but it may be possible to add in the future.

Right now we're mostly targeting 3.12, but we plan to support additional features from 3.13 and 3.14 later this year.

yangdanny97 avatar May 29 '25 02:05 yangdanny97

Thanks Danny - that makes sense.

To be sure, the bug being discussed is a bug for 3.12 (and 3.13) and will cease being a bug for 3.14.

legendre6891 avatar May 29 '25 10:05 legendre6891