Repository: python/mypy (Python)

Issue:
Several dataclass crashes involving Final attributes
<!--
  Use this form only if mypy reports an "INTERNAL ERROR" and/or gives a traceback.
  Please include the traceback and all other messages below (use `mypy --show-traceback`).
-->

**Crash Report**

I was just trying things out and found a crash with:

```python
from dataclasses import dataclass
from typing import Final

@dataclass
class FirstClass:
    FIRST_CONST: Final = 3

@dataclass  # mypy runs fine if this isn't a dataclass
class SecondClass:
    SECOND_CONST: Final = FirstClass.FIRST_CONST
```

Note: I don't think this code is very usable. I think I would want `ClassVar` here (or just a module-scoped variable), but thought the report might be interesting all the same.

**Traceback**

Traceback on current master:

```https://mypy.rtfd.io/en/latest/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.820+dev.88dde657fedecf17e15222ad4b685b0aeddcc423
Traceback (most recent call last):
  File "/home/mjh/.pyenv/versions/3.8.6/bin/mypy", line 33, in <module>
    sys.exit(load_entry_point('mypy', 'console_scripts', 'mypy')())
  File "/home/mjh/src/mypy/mypy/__main__.py", line 11, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "/home/mjh/src/mypy/mypy/main.py", line 98, in main
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/mjh/src/mypy/mypy/build.py", line 179, in build
    result = _build(
  File "/home/mjh/src/mypy/mypy/build.py", line 253, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/mjh/src/mypy/mypy/build.py", line 2658, in dispatch
    process_graph(graph, manager)
  File "/home/mjh/src/mypy/mypy/build.py", line 2982, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/mjh/src/mypy/mypy/build.py", line 3074, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "/home/mjh/src/mypy/mypy/semanal_main.py", line 78, in semantic_analysis_for_scc
    process_top_levels(graph, scc, patches)
  File "/home/mjh/src/mypy/mypy/semanal_main.py", line 199, in process_top_levels
    deferred, incomplete, progress = semantic_analyze_target(next_id, state,
  File "/home/mjh/src/mypy/mypy/semanal_main.py", line 326, in semantic_analyze_target
    analyzer.refresh_partial(refresh_node,
  File "/home/mjh/src/mypy/mypy/semanal.py", line 394, in refresh_partial
    self.refresh_top_level(node)
  File "/home/mjh/src/mypy/mypy/semanal.py", line 405, in refresh_top_level
    self.accept(d)
  File "/home/mjh/src/mypy/mypy/semanal.py", line 4835, in accept
    node.accept(self)
  File "/home/mjh/src/mypy/mypy/nodes.py", line 950, in accept
    return visitor.visit_class_def(self)
  File "/home/mjh/src/mypy/mypy/semanal.py", line 1048, in visit_class_def
    self.analyze_class(defn)
  File "/home/mjh/src/mypy/mypy/semanal.py", line 1125, in analyze_class
    self.analyze_class_body_common(defn)
  File "/home/mjh/src/mypy/mypy/semanal.py", line 1134, in analyze_class_body_common
    self.apply_class_plugin_hooks(defn)
  File "/home/mjh/src/mypy/mypy/semanal.py", line 1180, in apply_class_plugin_hooks
    hook(ClassDefContext(defn, decorator, self))
  File "/home/mjh/src/mypy/mypy/plugins/dataclasses.py", line 361, in dataclass_class_maker_callback
    transformer.transform()
  File "/home/mjh/src/mypy/mypy/plugins/dataclasses.py", line 106, in transform
    ctx.api.defer()
  File "/home/mjh/src/mypy/mypy/semanal.py", line 4554, in defer
    assert not self.final_iteration, 'Must not defer during final iteration'
AssertionError: Must not defer during final iteration
/home/mjh/tmp/testing_py/mypy_test.py:9: : note: use --pdb to drop into pdb
```

Traceback on pip package:

```
mypy_test.py:9: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.rtfd.io/en/latest/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.800
Traceback (most recent call last):
  File "mypy/semanal.py", line 4835, in accept
  File "mypy/nodes.py", line 950, in accept
  File "mypy/semanal.py", line 1048, in visit_class_def
  File "mypy/semanal.py", line 1125, in analyze_class
  File "mypy/semanal.py", line 1134, in analyze_class_body_common
  File "mypy/semanal.py", line 1180, in apply_class_plugin_hooks
  File "mypy/plugins/dataclasses.py", line 361, in dataclass_class_maker_callback
  File "mypy/plugins/dataclasses.py", line 106, in transform
  File "mypy/semanal.py", line 4554, in defer
AssertionError: Must not defer during final iteration
mypy_test.py:9: : note: use --pdb to drop into pdb
```

**To Reproduce**

Run `mypy` over a file containing the content above.

**Your Environment**

<!-- Include as many relevant details about the environment you experienced the bug in -->

- Mypy version used: 0.820+dev.88dde657fedecf17e15222ad4b685b0aeddcc423 and 0.800
- Mypy command-line flags: None
- Mypy configuration options from `mypy.ini` (and other config files): None
- Python version used: 3.8.6
- Operating system and version: Debian GNU/Linux 10 (buster)

<!--
You can freely edit this text, please remove all the lines
you believe are unnecessary.
-->



Hints:
The same problem occurs for this snippet:

```python
from dataclasses import dataclass
from typing import Final


@dataclass
class A:
    x: Final = "a"


@dataclass  # Removing this decorator makes it work
class B:
    y: Final = A()
```

```
$ mypy dataclass_final_test.py --show-traceback
dataclass_final_test.py:11: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.902
Traceback (most recent call last):
  File "mypy/semanal.py", line 4872, in accept
  File "mypy/nodes.py", line 950, in accept
  File "mypy/semanal.py", line 1056, in visit_class_def
  File "mypy/semanal.py", line 1134, in analyze_class
  File "mypy/semanal.py", line 1143, in analyze_class_body_common
  File "mypy/semanal.py", line 1189, in apply_class_plugin_hooks
  File "mypy/plugins/dataclasses.py", line 384, in dataclass_class_maker_callback
  File "mypy/plugins/dataclasses.py", line 109, in transform
  File "mypy/semanal.py", line 4583, in defer
AssertionError: Must not defer during final iteration
dataclass_final_test.py:11: : note: use --pdb to drop into pdb
```
Another occurrence of this problem with just a single dataclass:

```python
from dataclasses import dataclass
from typing import Final


def f():
    @dataclass
    class A:
        x: Final = "a"
```

```
$ mypy --show-traceback dataclass_final_test.py 
dataclass_final_test.py:7: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.910
Traceback (most recent call last):
  File "mypy/semanal.py", line 4872, in accept
  File "mypy/nodes.py", line 950, in accept
  File "mypy/semanal.py", line 1056, in visit_class_def
  File "mypy/semanal.py", line 1134, in analyze_class
  File "mypy/semanal.py", line 1143, in analyze_class_body_common
  File "mypy/semanal.py", line 1189, in apply_class_plugin_hooks
  File "mypy/plugins/dataclasses.py", line 384, in dataclass_class_maker_callback
  File "mypy/plugins/dataclasses.py", line 109, in transform
  File "mypy/semanal.py", line 4583, in defer
AssertionError: Must not defer during final iteration
dataclass_final_test.py:7: : note: use --pdb to drop into pdb
```
Another variant:

```python
from dataclasses import dataclass
from http import HTTPStatus
from typing import Final

@dataclass
class MyResponse:
    status_code: Final = HTTPStatus.NOT_FOUND
```

```
sketch_dataclasses.py:6: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.910
Traceback (most recent call last):
  File "mypy/semanal.py", line 4872, in accept
  File "mypy/nodes.py", line 950, in accept
  File "mypy/semanal.py", line 1056, in visit_class_def
  File "mypy/semanal.py", line 1134, in analyze_class
  File "mypy/semanal.py", line 1143, in analyze_class_body_common
  File "mypy/semanal.py", line 1189, in apply_class_plugin_hooks
  File "mypy/plugins/dataclasses.py", line 384, in dataclass_class_maker_callback
  File "mypy/plugins/dataclasses.py", line 109, in transform
  File "mypy/semanal.py", line 4583, in defer
AssertionError: Must not defer during final iteration
sketch_dataclasses.py:6: : note: use --pdb to drop into pdb
```
This still crashes on master, but the traceback is now different following #12762. Running mypy on the original repro now produces:

```
(venv) C:\Users\alexw\coding\mypy>mypy test.py
Traceback (most recent call last):
  File "C:\Users\alexw\coding\mypy\venv\Scripts\mypy-script.py", line 33, in <module>
    sys.exit(load_entry_point('mypy', 'console_scripts', 'mypy')())
  File "c:\users\alexw\coding\mypy\mypy\__main__.py", line 12, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "c:\users\alexw\coding\mypy\mypy\main.py", line 96, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "c:\users\alexw\coding\mypy\mypy\main.py", line 173, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "c:\users\alexw\coding\mypy\mypy\build.py", line 181, in build
    result = _build(
  File "c:\users\alexw\coding\mypy\mypy\build.py", line 257, in _build
    graph = dispatch(sources, manager, stdout)
  File "c:\users\alexw\coding\mypy\mypy\build.py", line 2755, in dispatch
    process_graph(graph, manager)
  File "c:\users\alexw\coding\mypy\mypy\build.py", line 3113, in process_graph
    process_stale_scc(graph, scc, manager)
  File "c:\users\alexw\coding\mypy\mypy\build.py", line 3205, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "c:\users\alexw\coding\mypy\mypy\semanal_main.py", line 88, in semantic_analysis_for_scc
    apply_class_plugin_hooks(graph, scc, errors)
  File "c:\users\alexw\coding\mypy\mypy\semanal_main.py", line 405, in apply_class_plugin_hooks
    assert num_passes < 10, "Internal error: too many class plugin hook passes"
AssertionError: Internal error: too many class plugin hook passes
```
Yet another repro for this crash was identified by @DetachHead in #13377, which I just closed as a duplicate of this issue:

```python
from dataclasses import dataclass, field
from typing import Final

@dataclass
class Foo:
    a: Final = field(default=1)
```

Patch:
diff --git a/mypy/plugin.py b/mypy/plugin.py
--- a/mypy/plugin.py
+++ b/mypy/plugin.py
@@ -407,6 +407,10 @@ def final_iteration(self) -> bool:
     def is_stub_file(self) -> bool:
         raise NotImplementedError
 
+    @abstractmethod
+    def analyze_simple_literal_type(self, rvalue: Expression, is_final: bool) -> Type | None:
+        raise NotImplementedError
+
 
 # A context for querying for configuration data about a module for
 # cache invalidation purposes.
diff --git a/mypy/plugins/dataclasses.py b/mypy/plugins/dataclasses.py
--- a/mypy/plugins/dataclasses.py
+++ b/mypy/plugins/dataclasses.py
@@ -368,7 +368,7 @@ def collect_attributes(self) -> list[DataclassAttribute] | None:
 
             if isinstance(node, TypeAlias):
                 ctx.api.fail(
-                    ("Type aliases inside dataclass definitions " "are not supported at runtime"),
+                    ("Type aliases inside dataclass definitions are not supported at runtime"),
                     node,
                 )
                 # Skip processing this node. This doesn't match the runtime behaviour,
@@ -426,6 +426,23 @@ def collect_attributes(self) -> list[DataclassAttribute] | None:
                 is_kw_only = bool(ctx.api.parse_bool(field_kw_only_param))
 
             known_attrs.add(lhs.name)
+
+            if sym.type is None and node.is_final and node.is_inferred:
+                # This is a special case, assignment like x: Final = 42 is classified
+                # annotated above, but mypy strips the `Final` turning it into x = 42.
+                # We do not support inferred types in dataclasses, so we can try inferring
+                # type for simple literals, and otherwise require an explicit type
+                # argument for Final[...].
+                typ = ctx.api.analyze_simple_literal_type(stmt.rvalue, is_final=True)
+                if typ:
+                    node.type = typ
+                else:
+                    ctx.api.fail(
+                        "Need type argument for Final[...] with non-literal default in dataclass",
+                        stmt,
+                    )
+                    node.type = AnyType(TypeOfAny.from_error)
+
             attrs.append(
                 DataclassAttribute(
                     name=lhs.name,


Test patch:
diff --git a/test-data/unit/check-dataclasses.test b/test-data/unit/check-dataclasses.test
--- a/test-data/unit/check-dataclasses.test
+++ b/test-data/unit/check-dataclasses.test
@@ -1796,3 +1796,21 @@ t: Two
 reveal_type(t.__match_args__)  # E: "Two" has no attribute "__match_args__" \
                                # N: Revealed type is "Any"
 [builtins fixtures/dataclasses.pyi]
+
+[case testFinalInDataclass]
+from dataclasses import dataclass
+from typing import Final
+
+@dataclass
+class FirstClass:
+    FIRST_CONST: Final = 3  # OK
+
+@dataclass
+class SecondClass:
+    SECOND_CONST: Final = FirstClass.FIRST_CONST  # E: Need type argument for Final[...] with non-literal default in dataclass
+
+reveal_type(FirstClass().FIRST_CONST)  # N: Revealed type is "Literal[3]?"
+FirstClass().FIRST_CONST = 42  # E: Cannot assign to final attribute "FIRST_CONST"
+reveal_type(SecondClass().SECOND_CONST)  # N: Revealed type is "Literal[3]?"
+SecondClass().SECOND_CONST = 42  # E: Cannot assign to final attribute "SECOND_CONST"
+[builtins fixtures/dataclasses.pyi]