Skip to content

Commit 324db7f

Browse files
committed
Fix "not callable" issue for @dataclass(frozen=True) with Final attr
1 parent e2b821b commit 324db7f

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

mypy/checkmember.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,10 @@ def is_instance_var(var: Var) -> bool:
755755
var.name in var.info.names
756756
and var.info.names[var.name].node is var
757757
and not var.is_classvar
758-
# variables without annotations are treated as classvar
759-
and not var.is_inferred
758+
# variables without annotations are treated as classvar,
759+
# but not when they are annotated as `a: Final = 1`,
760+
# since it will be inferenced as `Literal[1]`
761+
and not (var.is_inferred and not var.is_final)
760762
)
761763

762764

test-data/unit/check-dataclasses.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2553,3 +2553,18 @@ class X(metaclass=DCMeta):
25532553
class Y(X):
25542554
a: int # E: Covariant override of a mutable attribute (base class "X" defined the type as "Optional[int]", expression has type "int")
25552555
[builtins fixtures/tuple.pyi]
2556+
2557+
2558+
[case testFrozenWithFinal]
2559+
from dataclasses import dataclass
2560+
from typing import Final
2561+
2562+
@dataclass(frozen=True)
2563+
class My:
2564+
a: Final = 1
2565+
b: Final[int] = 2
2566+
2567+
m = My()
2568+
reveal_type(m.a) # N: Revealed type is "Literal[1]?"
2569+
reveal_type(m.b) # N: Revealed type is "builtins.int"
2570+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)