Skip to content

TypedDict totality ignored when instantiating a not type-hinted class variable typed dict #17174

Closed
@distrame

Description

@distrame

Bug Report

When instantiating a typeddict that was created as a class variable without any type hints and which has a not-required key, mypy thinks that all keys are required ignoring total=False and typing.NotRequired.

To Reproduce

import typing

class Dicts:
    class TF(typing.TypedDict, total=False):
        user_id: int

    class NR(typing.TypedDict):
        user_id: typing.NotRequired[int]

    TotalFalse = TF
    TotalFalse_Annotated: type[TF] = TF
    TotalFalse_TypeAlias: typing.TypeAlias = TF
    TotalFalse_ClassVar: typing.ClassVar[type[TF]] = TF
    TotalFalse_ClassVar_TypeAlias: typing.ClassVar[typing.TypeAlias] = TF

    NotRequired = NR
    NotRequired_Annotated: type[NR] = NR
    NotRequired_TypeAlias: typing.TypeAlias = NR
    NotRequired_ClassVar: typing.ClassVar[type[NR]] = NR
    NotRequired_ClassVar_TypeAlias: typing.ClassVar[typing.TypeAlias] = NR

dicts = Dicts()

dicts.TF()  # no error
dicts.TotalFalse()  # error: Missing named argument "user_id"  [call-arg]
dicts.TotalFalse_Annotated()  # no error
dicts.TotalFalse_TypeAlias()  # no error
dicts.TotalFalse_ClassVar()  # no error
dicts.TotalFalse_ClassVar_TypeAlias()  # no error

dicts.NR()  # no error
dicts.NotRequired()  # error: Missing named argument "user_id"  [call-arg]
dicts.NotRequired_Annotated()  # no error
dicts.NotRequired_TypeAlias()  # no error
dicts.NotRequired_ClassVar()  # no error
dicts.NotRequired_ClassVar_TypeAlias()  # no error

Expected Behavior

No errors

Actual Behavior

main.py:25: error: Missing named argument "user_id" [call-arg]
main.py:32: error: Missing named argument "user_id" [call-arg]

Your Environment

  • Mypy version used: 1.10.0
  • Python version used: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions