Skip to content

TypeForm rejects a union when types are nested and metaclass defines __get__ #11741

Description

Describe the bug
I defined a metaclass with __get__ returning cls unchanged, nested classes constructed from this metaclass, and a union of the nested classes as a TypeForm. The bug: this TypeForm then rejects appropriate unions.

Code or Screenshots

from typing_extensions import TypeForm

class Meta(type):
    def __get__[T: Meta](
        cls: T,
        instance: object | None,
        owner: type | None = None,
    ) -> T:
        return cls

class Owner:
    class A(metaclass=Meta):
        pass

    class B(metaclass=Meta):
        pass

T1: TypeForm[Owner.A | Owner.B] = Owner.A # Accepted
T2: TypeForm[Owner.A | Owner.B] = Owner.B # Accepted
T3: TypeForm[Owner.A | Owner.B] = Owner.A | Owner.B # Rejected:

# Type "UnionType" is not assignable to declared type "TypeForm[A | B]"
# "UnionType" is not assignable to "TypeForm[A | B]"

Module-scoped classes doesn't have this bug.

from typing_extensions import TypeForm

class Meta(type):
    def __get__[T: Meta](
        cls: T,
        instance: object | None,
        owner: type | None = None,
    ) -> T:
        return cls

class A(metaclass=Meta):
    pass

class B(metaclass=Meta):
    pass


T1: TypeForm[A | B] = A # Accepted
T2: TypeForm[A | B] = B # Accepted
T3: TypeForm[A | B] = A | B # Accepted

Removing get also makes the bug disappear.

from typing_extensions import TypeForm

class Meta(type):
    pass

class Owner:
    class A(metaclass=Meta):
        pass

    class B(metaclass=Meta):
        pass


T1: TypeForm[Owner.A | Owner.B] = Owner.A # Accepted
T2: TypeForm[Owner.A | Owner.B] = Owner.B # Accepted
T3: TypeForm[Owner.A | Owner.B] = Owner.A | Owner.B # Accepted

VS Code extension or command-line

  • Pyright v1.1.414 both CLI and Pylance
  • Python 3.14.7
  • typing_extensions 4.16.0

Activity

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions