Skip to content

False positive: comparison-overlap with dataclasses.Field.default_factory and dataclasses.MISSING #12124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sirosen opened this issue Feb 3, 2022 · 3 comments
Labels
bug mypy got something wrong

Comments

@sirosen
Copy link

sirosen commented Feb 3, 2022

Bug Report

In code which is checking if a field has a default, a check to see if Field.default_factory is MISSING will be misreported as a comparison-overlap error if mypy --strict is used.

To Reproduce

In foo.py:

from dataclasses import MISSING, Field
from typing import Any

def is_required(x: Field[Any]) -> bool:
    return x.default is MISSING and x.default_factory is MISSING

Expected Behavior

This check should pass. One of the valid values for default_factory is MISSING.

Actual Behavior

With the above reproduction,

$ mypy --strict foo.py
foo.py:5: error: Non-overlapping identity check (left operand type: "_DefaultFactory[Any]", right operand type: "_MISSING_TYPE")
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.931 and master (0.940+dev.84b977876aa76bfebf5fe49d11ebd202e4a60a33)
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): None
@sirosen sirosen added the bug mypy got something wrong label Feb 3, 2022
@erictraut
Copy link

This looks like a bug in the typeshed stub dataclasses.pyi. The type annotations for Field.default and Field.default_factory should probably be unions that include the type _MISSING_TYPE.

@sirosen
Copy link
Author

sirosen commented Feb 4, 2022

Good point! I didn't think to look at typeshed -- I thought mypy was doing something weird.
I'll put in a PR to update it. Assuming that's right and my patch is accepted, I'll close this when that's merged.

@hauntsaninja
Copy link
Collaborator

There isn't any action for mypy, so closing. Current plan is to use the literal trick in typeshed: python/typeshed#7127

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants