Skip to content

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

Closed

Description

@sirosen

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

Activity

  1. erictraut commented on Feb 4, 2022

    @erictraut

    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.

  2. sirosen commented on Feb 4, 2022

    @sirosen
    Author

    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.

  3. hauntsaninja commented on Feb 4, 2022

    @hauntsaninja
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions