import enum
def get_as[T](name: str, expected_type: type[T]) -> T:
if issubclass(expected_type, enum.Enum):
return expected_type[name]
raise NotImplementedError()
ERROR `type[Enum & T]` is not subscriptable [unsupported-operation]
--> repro.py:5:16
|
5 | return expected_type[name]
| ^^^^^^^^^^^^^^^^^^^
The error does not occur when the TypeVar is directly bounded e.g. the following works perfectly:
def get_as_enum[T: enum.Enum](name: str, expected_type: type[T]) -> T:
return expected_type[name]
Describe the Bug
Pyrefly fails to evaluate subscript operations on intersection types (such as
type[Enum & T]) resulting fromissubclass(...)type narrowing.Repro:
Pyrefly emits the following type error:
The error does not occur when the TypeVar is directly bounded e.g. the following works perfectly:
Sandbox Link
https://pyrefly.org/sandbox/?project=v2.pZVBa8JAEIX_ykAOejDJ3XOFeimFWjxIkTWZZEM1G5Ldpv33fTMx2lAqFEE86M7szJs33_6FzIi2fMzcick78pbpeeiVXoZTC_wIp-FjoEYJhSLqLbdMXy4AUTX1LSYBU1mDHxFER0btZA4u-AuMa-QV0RLES4pHyaABoEbAllLBBjYEd1wxXHkuE5OIKAanPYxqjrGvpFZ4kTLL2btkL5jzg8kwdDn5Cq_-QCW8ic6wAo0sV1UXmBDnmgGukAD-bPAEnLj21Ffeqgrrh9W1Im8N_mLqQqOYmnNSJmSCgF8II-2XLvYuxkLIeJTsVsi9IPYZXCr3bEUoVagNox7oPmcaDALRoRNkbeFh1-MlkCW4wJHrcBrRUbLfm263eZvLKikNcRP6yOA3bXupHeKEruZG_aww6MJB8TifHEc00icrfI1oOGNhcmwn18HcIxOenF-LAiIe50qHYXWvRe4l8W6zvF7wv6JvlfF7NaOLf8_2gcDY6TLg6cNUxH549Zis9023TNMx3LVlynWau6xLJxEpCYXMh6n0QZ2mI6co6ZD2HibMBiTMbhFhJkDAiZtL_g0
(Only applicable for extension issues) IDE Information
No response