Skip to content

Type alias and union wrong revealed type #19640

@zuckerruebe

Description

@zuckerruebe

Bug Report

Derived type is object when it should be CreateProject | LoadProject | MigrateProject.

To Reproduce

MyPy Playground: https://mypy-play.net/?mypy=latest&python=3.12&gist=1f2c2788b60a53d4389826ea2518dc1a

import typing as _tp
import dataclasses as _dc
import pathlib as _pl

class Cancelled:
    pass


CANCELLED = Cancelled()

type MaybeCancelled[T] = T | Cancelled


def isCancelled[
    T
](maybeCancelled: MaybeCancelled[T]) -> _tp.TypeGuard[Cancelled]:
    raise NotImplementedError()

def value[T](maybeCancelled: MaybeCancelled[T]) -> T:
    raise NotImplementedError()
    
@_dc.dataclass
class CreateProject:
    jsonFilePath: _pl.Path


@_dc.dataclass
class LoadProject:
    jsonFilePath: _pl.Path


@_dc.dataclass
class MigrateProject:
    oldJsonFilePath: _pl.Path
    newProjectFolderPath: _pl.Path
    
Project = CreateProject | LoadProject | MigrateProject

def getProject() -> MaybeCancelled[Project]:
    raise NotImplementedError()

def main() -> None:
    maybeCancelled = getProject()
    if  isCancelled(maybeCancelled):
        return
    project: Project = _tp.reveal_type(value(maybeCancelled))

Expected Behavior

The revealed type should be CreateProject | LoadProject | MigrateProject.

Actual Behavior

The revealed type is object.

Your Environment

  • Mypy version used: 1.17.1
  • Mypy command-line flags: none (?) (just used the mypy playground and hit Run)
  • Mypy configuration options from mypy.ini (and other config files): none (?) (just used the mypy playground and hit Run)
  • Python version used: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-join-v-unionUsing join vs. using unions

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions