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

Enable Final instance attributes for attrs #14232

Merged
merged 2 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 11 additions & 3 deletions mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

from typing import Iterable, List, cast
from typing_extensions import Final
from typing_extensions import Final, Literal

import mypy.plugin # To avoid circular imports.
from mypy.exprtotype import TypeTranslationError, expr_to_unanalyzed_type
Expand Down Expand Up @@ -756,20 +756,28 @@ def _add_init(
ctx: mypy.plugin.ClassDefContext,
attributes: list[Attribute],
adder: MethodAdder,
method_name: str,
method_name: Literal["__init__", "__attrs_init__"],
) -> None:
"""Generate an __init__ method for the attributes and add it to the class."""
# Convert attributes to arguments with kw_only arguments at the end of
# Convert attributes to arguments with kw_only arguments at the end of
# the argument list
pos_args = []
kw_only_args = []
sym_table = ctx.cls.info.names
for attribute in attributes:
if not attribute.init:
continue
if attribute.kw_only:
kw_only_args.append(attribute.argument(ctx))
else:
pos_args.append(attribute.argument(ctx))

# If the attribute is Final, present in `__init__` and has
# no default, make sure it doesn't error later.
if not attribute.has_default and attribute.name in sym_table:
sym_node = sym_table[attribute.name].node
if isinstance(sym_node, Var) and sym_node.is_final:
sym_node.final_set_in_init = True
args = pos_args + kw_only_args
if all(
# We use getattr rather than instance checks because the variable.type
Expand Down
33 changes: 33 additions & 0 deletions test-data/unit/check-attr.test
Original file line number Diff line number Diff line change
Expand Up @@ -1834,3 +1834,36 @@ class Sub(Base):
# This matches runtime semantics
reveal_type(Sub) # N: Revealed type is "def (*, name: builtins.str, first_name: builtins.str, last_name: builtins.str) -> __main__.Sub"
[builtins fixtures/property.pyi]

[case testFinalInstanceAttribute]
from attrs import define
from typing import Final

@define
class C:
a: Final[int]

reveal_type(C) # N: Revealed type is "def (a: builtins.int) -> __main__.C"

C(1).a = 2 # E: Cannot assign to final attribute "a"

[builtins fixtures/property.pyi]

[case testFinalInstanceAttributeInheritance]
from attrs import define
from typing import Final

@define
class C:
a: Final[int]

@define
class D(C):
b: Final[str]

reveal_type(D) # N: Revealed type is "def (a: builtins.int, b: builtins.str) -> __main__.D"

D(1, "").a = 2 # E: Cannot assign to final attribute "a"
D(1, "").b = "2" # E: Cannot assign to final attribute "b"

[builtins fixtures/property.pyi]