python / python/mypy

Strange behavior with dataclass InitVar

Open
#8,439 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug priority-1-normal topic-dataclasses topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Wondering what is going on here for the cases 3-6. Running it with --strict, mypy version is 0.761.

import dataclasses
from dataclasses import dataclass, InitVar
from functools import partial

dataclass_alias = dataclass
dataclass_partial = partial(dataclass, frozen=True)
dataclass_decorator = dataclass(frozen=True)

# 1. ok
@dataclass(frozen=True)
class X1:
    value: InitVar[bool] = None


# 2. ok
@dataclasses.dataclass(frozen=True)
class X2:
    default: InitVar[bool] = None


# 3. error: Incompatible types in assignment (expression has type "None", variable has type "InitVar[bool]")
@dataclass_alias(frozen=True)
class X3:
    default: InitVar[bool] = None


# 4. error: Incompatible types in assignment (expression has type "None", variable has type "InitVar[bool]")
@dataclass_partial
class X4:
    default: InitVar[bool] = None

# 5a. error: Too many arguments for "object"
# 5b. error: Incompatible types in assignment (expression has type "None", variable has type "InitVar[bool]")
@dataclass_partial()
class X5:
    default: InitVar[bool] = None


# 6. error: Incompatible types in assignment (expression has type "None", variable has type "InitVar[bool]")
@dataclass_decorator
class X6:
    default: InitVar[bool] = None

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied cases with mypy 0.761 and compare how each dataclass decorator form is analyzed. Trace the handling of InitVar and decorator aliases or partials; done means the cases have a documented, consistent result and appropriate diagnostics or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.