Skip to content

"from __future__ import annotations" breaks the parsing of NotRequired in check_type #424

@kilroy42

Description

@kilroy42

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Typeguard version

4.1.5

Python version

3.11 / 3.12

What happened?

The following program should output the dictionary and not raise an Exception.

from __future__ import annotations

from typing import TypedDict, NotRequired
from typeguard import check_type

class TestType(TypedDict):
    needed: str
    notNeeded: NotRequired[str]

print(check_type({'needed': ''}, TestType))

But the following happens:

typeguard.TypeCheckError: dict is missing required key(s): "notNeeded"

BUT if you remove the first line (from __future__ import annotations), it works as expected.

How can we reproduce the bug?

Run the code once unmodified. Then run the code with the first line removed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions