Skip to content

Subscripting intersection types false positive ("type[Enum & T] is not subscriptable) #4436

Description

@QEDady

Describe the Bug

Pyrefly fails to evaluate subscript operations on intersection types (such as type[Enum & T]) resulting from issubclass(...) type narrowing.
Repro:

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()

Pyrefly emits the following type error:

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]

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

Metadata

Metadata

Labels

googleissues from googlenarrowingIssues with narrowing - root cause is usually narrowing, flow handling, or bothtypechecking

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions