microsoft / microsoft/pyright

Type forms not allowed in any order in TypedDict functional syntax

Open
#11,167 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

**Describe the bug**
pyright is erroring if given `NotRequired` inside `ReadOnly` in the TypedDict functional syntax.

PEP-705 states that "ReadOnly[] can be used with Required[], NotRequired[] and Annotated[], in any nesting order"

**Code or Screenshots**

```python
from typing import NotRequired, ReadOnly, TypedDict

# Errors with: "NotRequired" is not allowed in this context (reportInvalidTypeForm)
Foo = TypedDict("Foo", {"has-hyphens": ReadOnly[NotRequired[int]]})
```
[playground](https://pyright-play.net/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoByYMASgKYCOArkiGQCYA0U5AhvQPIoA2czAKogYARJAGMYAWABQMgGJgwUALxRBCEeJgAKAEQKwu5gG9dAC1YBnALRnEZsiku6AXCzLsuvANpFSlGjp6b1QYAF0wgF8AShkgA)

Switching to class-based syntax, or switching the order of `ReadOnly` and `NotRequired`, makes the error go away.

Contributor guide

Open the contributing guide

Research direction

Reproduce the error in the linked pyright playground using the TypedDict functional syntax example, then trace the validation for nested ReadOnly and NotRequired forms and inspect related regression tests. Done means both nesting orders are accepted without reportInvalidTypeForm while class-based syntax remains unaffected.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.