Skip to content
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

Fix "not callable" issue for @dataclass(frozen=True) with Final attr #18572

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,10 @@ def is_instance_var(var: Var) -> bool:
var.name in var.info.names
and var.info.names[var.name].node is var
and not var.is_classvar
# variables without annotations are treated as classvar
and not var.is_inferred
# variables without annotations are treated as classvar,
# but not when they are annotated as `a: Final = 1`,
# since it will be inferenced as `Literal[1]`
and not (var.is_inferred and not var.is_final)
)


Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -2553,3 +2553,18 @@ class X(metaclass=DCMeta):
class Y(X):
a: int # E: Covariant override of a mutable attribute (base class "X" defined the type as "Optional[int]", expression has type "int")
[builtins fixtures/tuple.pyi]


[case testFrozenWithFinal]
from dataclasses import dataclass
from typing import Final

@dataclass(frozen=True)
class My:
a: Final = 1
b: Final[int] = 2

m = My()
reveal_type(m.a) # N: Revealed type is "Literal[1]?"
reveal_type(m.b) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]