Skip to content

NotRequired[...] built at runtime for introspection reported as invalid-annotation #5429

Open

Description

@MarcoGorelli

Describe the Bug

from typing import Any, get_origin

from typing_extensions import NotRequired


def mark_optional(hints: dict[str, Any]) -> dict[str, Any]:
    return {key: NotRequired[hint] for key, hint in hints.items()}


assert get_origin(mark_optional({"a": int})["a"]) is NotRequired
$ pyrefly check t.py
ERROR `NotRequired` is not allowed in this context [invalid-annotation]
 --> t.py:7:18
  |
7 |     return {key: NotRequired[hint] for key, hint in hints.items()}
  |                  ^^^^^^^^^^^^^^^^^
 INFO 1 error

other type checkers are fine with it

In this example, NotRequired is used for runtime checks. An example of where this pattern is used is in huggingface-hub:

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.pZRBCoMwEEWvEtqFCtbsPUQp3apIqEGl1BRNS4t49_6ZqCgFoXSZZPIzk3nzf7XMUtvctHVZow-jObioXL-sbqjw2SKOxp7HMijY0X1T7TU3o0_6FTqGKS1gfTSlIT2SMTXrrakZDskenxIv5RPSyQSBgaNQ0JLoYPmIGfGDwSXhvHRRiL9OqU93CtDhuh2ChBfIiKdqfu-793txchti_Ea4HqApH_BW6EZps4etalFZe-9iKafrpi2lbmRhLp1c3ZBcjXqqmh17LSdcth1k_4HOc8x5W8h5RBxHHFTzRhB3APssjoNNvD4

(Only applicable for extension issues) IDE Information

No response

Activity

  1. added 2 commits that reference this issue on Oct 29, 2026
    f228845
    5ced393
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions