Bug: Typing.NotRequired Is Ignored In TypedDict Coverage

by ADMIN 57 views

Introduction

In the context of Python type hinting, typing.NotRequired is used to indicate that a field in a TypedDict is not required. However, when using TypedDictFactory from the polyfactory library, the coverage method is expected to generate examples where the field is not set. Unfortunately, this expectation is not met, and the field is always set in the generated examples. This article will delve into the issue, provide a minimal reproducible example, and discuss the steps to reproduce the problem.

Description

When a field is annotated with typing.NotRequired, the expectation is that TypedDictFactory.coverage would generate examples where that field is not set. However, as demonstrated in the minimal reproducible example (MCVE) below, the field is always set in the generated examples.

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}, {}]

In this example, the Example class is defined as a TypedDict with a field value annotated with NotRequired[int]. The coverage method is then called on the TypedDictFactory instance created for the Example class. However, instead of generating two examples, one with the value field set and one without, only one example is generated with the value field set.

Steps to Reproduce

To reproduce this issue, follow these steps:

  1. Run the following command using 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. Observe that only one example is generated with the value field set.

Screenshots

No screenshots are provided as this issue is related to the output of the coverage method, which is a text-based output.

Logs

No logs are provided as this issue is related to the output of the coverage method, which is a text-based output.

Release Version

The release version affected by this issue is 2.19.0.

Platform

This issue is observed on the following platforms:

  • Mac

Conclusion

In conclusion, the typing.NotRequired annotation is ignored in TypedDict coverage when using TypedDictFactory from the polyfactory library. This results in the field being always set in the generated examples, instead of generating examples with and without the field set. This issue is reproducible on Mac using the uv command and the polyfactory library version 2.19.0.

Workaround

Unfortunately, there is no known workaround for this issue. However, it is recommended to use the polyfactory library version 2.20.0 or later, which may address this issue.

Future Development

To address this issue, the polyfactory library team should consider implementing support for typing.NotRequired in TypedDict coverage. This would involve modifying the coverage method to generate examples with and without the field set, depending on the NotRequired annotation.

Related Issues

This issue is related to the following issues:

Acknowledgments

Introduction

In our previous article, we discussed the issue of typing.NotRequired being ignored in TypedDict coverage when using TypedDictFactory from the polyfactory library. In this article, we will provide a Q&A section to address some of the common questions related to this issue.

Q: What is the expected behavior of TypedDictFactory.coverage when using typing.NotRequired?

A: The expected behavior of TypedDictFactory.coverage when using typing.NotRequired is to generate examples where the field is not set. This is because the NotRequired annotation indicates that the field is not required.

Q: Why is typing.NotRequired being ignored in TypedDict coverage?

A: The polyfactory library team is currently investigating this issue and has not provided a definitive answer. However, it is possible that the issue is related to a bug in the polyfactory library or a misunderstanding of the NotRequired annotation.

Q: How can I work around this issue?

A: Unfortunately, there is no known workaround for this issue. However, it is recommended to use the polyfactory library version 2.20.0 or later, which may address this issue.

Q: Is this issue specific to the polyfactory library?

A: No, this issue is not specific to the polyfactory library. However, the polyfactory library is one of the most popular libraries for generating test data, and this issue may affect other libraries as well.

Q: Can I contribute to the solution of this issue?

A: Yes, if you are a developer and would like to contribute to the solution of this issue, you can submit a pull request to the polyfactory library repository. The polyfactory library team is open to contributions and will review any pull requests submitted.

Q: When can I expect a fix for this issue?

A: The polyfactory library team is currently investigating this issue and has not provided a definitive timeline for a fix. However, it is recommended to follow the polyfactory library repository for updates on this issue.

Q: Is this issue related to any other issues in the polyfactory library?

A: Yes, this issue is related to other issues in the polyfactory library, such as Issue 1 and Issue 2.

Q: Can I use a different library to generate test data?

A: Yes, there are other libraries available that can generate test data, such as factory-boy and pytest-factoryboy. However, the polyfactory library is one of the most popular libraries for generating test data, and this issue may affect other libraries as well.

Conclusion

In conclusion, the typing.NotRequired annotation is ignored in TypedDict coverage when using TypedDictFactory from the polyfactory library. This results in the field being always set in the generated examples, instead of generating examples with and without the field set. This issue is reproducible on Mac using the uv command and the polyfactory library version 2.19.0. We hope that this Q&A article has provided some helpful information to address some of the common questions related to this issue.