Skip to content

Incorrect type narrowing on union of TypedDict #12098

Open
@Dreamsorcerer

Description

@Dreamsorcerer
from typing import Any, TypedDict

class _SessionData(TypedDict):
    created: int
    session: dict[str, Any]


class _EmptyDict(TypedDict):
    """Empty dict for typing."""


SessionData = _SessionData | _EmptyDict

data: SessionData

reveal_type(data)
a = data if data else None
reveal_type(a)
aiohttp_session/__init__.py:56: note: Revealed type is "Union[TypedDict('aiohttp_session._SessionData', {'created': builtins.int, 'session': builtins.dict[builtins.str, Any]}), TypedDict('aiohttp_session._EmptyDict', {})]"
aiohttp_session/__init__.py:58: note: Revealed type is "Union[TypedDict('aiohttp_session._EmptyDict', {}), None]"

I also get the same result if I make it not data. The _SessionData type just disappears for no reason and it keeps saying the empty dict will be the result regardless of the boolean check.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions