polyfactory icon indicating copy to clipboard operation
polyfactory copied to clipboard

Bug: typing.NotRequired is ignored in TypedDict coverage

Open vrslev opened this issue 10 months ago • 0 comments

Description

When a field is annotated with typing.NotRequired, expectation is that TypedDictFactory.coverage would generate examples where that field is not set.

URL to code causing the issue

No response

MCVE

from typing import NotRequired, TypedDict

from polyfactory.factories import TypedDictFactory


class Example(TypedDict):
    value: NotRequired[int]


print(list(TypedDictFactory.create_factory(Example).coverage()))
# Prints: [{'value': 7853}]
# Expected: [{'value': 7853}, {}]

Steps to reproduce

  1. Run with uv:
echo "\
from typing import NotRequired, TypedDict

from polyfactory.factories import TypedDictFactory


class Example(TypedDict):
    value: NotRequired[int]


print(list(TypedDictFactory.create_factory(Example).coverage()))
" | uv run --with polyfactory --python 3.13 -
  1. See that it provides only one example: with value field set.

Screenshots

"In the format of: ![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"

Logs


Release Version

2.19.0

Platform

  • [ ] Linux
  • [x] Mac
  • [ ] Windows
  • [ ] Other (Please specify in the description above)

vrslev avatar Mar 11 '25 10:03 vrslev