Skip to content

False-positive errors when using type[T] (or Type[T]) in a PEP 604 type alias #12392

Closed
Listed in
@AlexWaygood

Description

@AlexWaygood
Member

Bug Report

The following two snippets (one using builtins.type, the other using typing.Type) both work fine at runtime on 3.10+. They should be accepted as valid type alias definitions on Python 3.10+ (and should be accepted in stub files regardless of Python version). However, mypy raises false-positive errors for both. The errors do not go away if you use typing.TypeAlias:

from typing import Type, TypeAlias

A = type[int] | str  # error: Value of type "Type[type]" is not indexable
B = Type[int] | str  # error: Unsupported left operand type for | ("object")
C: TypeAlias = type[int] | str  # error: Value of type "Type[type]" is not indexable
D: TypeAlias = Type[int] | str  # error: Unsupported left operand type for | ("object")

The order of the types in the union makes no difference when using type[T]: the following two variants still produce errors:

E = str | type[int]  # error: Value of type "Type[type]" is not indexable
F: TypeAlias = str | type[int]  # error: Value of type "Type[type]" is not indexable

However, the order of the types in the union does matter if you're using typing.Type[T]:

B = Type[int] | str  # error: Unsupported left operand type for | ("object")
D: TypeAlias = Type[int] | str  # error: Unsupported left operand type for | ("object")
G = str | Type[int]  # no error

Interestingly, the bug can only be reproduced if type or Type is the outermost object in the type alias. Neither of the following two snippets causes mypy errors:

E = list[type[int] | str]
F = type[int | str]

Mypy is also happy with type[T] or Type[T] being used in old-style typing.Union type aliases:

from typing import Union
G = Union[type[int], str]

To Reproduce

Mypy playground link here.

Expected Behavior

No error should be reported.

Cc. @JelleZijlstra

Activity

changed the title [-]False-positive errors when using `type[T]` (or Type[T]`) in a PEP 604 type alias[/-] [+]False-positive errors when using `type[T]` (or `Type[T]`) in a PEP 604 type alias[/+] on Mar 20, 2022
wrobell

wrobell commented on Apr 12, 2022

@wrobell

I have encountered the following

import typing as tp
from collections.abc import Coroutine

T = tp.TypeVar('T')

F: tp.TypeAlias = tp.Callable[[], T] | tp.Callable[[], Coroutine[None, None, T]]  # Unsupported left operand type for | ("object")
G: tp.TypeAlias = tp.Callable[[], T | Coroutine[None, None, T]]  # ok

It looks like the same issue?

AlexWaygood

AlexWaygood commented on Apr 12, 2022

@AlexWaygood
MemberAuthor

@wrobell, I have another issue open for PEP-604 false-positives with respect to Callable: #12393. But I agree that these problems are linked.

mthuurne

mthuurne commented on May 31, 2022

@mthuurne
Contributor

This issue seems to exist for any runtime evaluation of type[X], not just in type aliases:

def f() -> type:
    return type[int]  # error: Value of type "Type[type]" is not indexable

This still happens when using the --python-version 3.10 option.

12 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    affects-typeshedAnything that blocks a typeshed changebugmypy got something wrongfalse-positivemypy gave an error on correct codetopic-pep-604PEP 604 (union | operator)topic-type-aliasTypeAlias and other type alias issues

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @wrobell@mthuurne@ppentchev@JelleZijlstra@ilevkivskyi

      Issue actions

        False-positive errors when using `type[T]` (or `Type[T]`) in a PEP 604 type alias · Issue #12392 · python/mypy