Repository: python/mypy

Issue:
dataclasses.InitVar crashes on new analyzer
Currently this test case crashes on the new semantic analyzer:
```
[case testDataclassesInitVars]
from dataclasses import InitVar, dataclass

defer: Yes

@dataclass
class Application:
  name: str
  database_name: InitVar[str]

class Yes: ...
[builtins fixtures/list.pyi]
```


Discussion:

Resolving patch:
diff --git a/mypy/plugins/dataclasses.py b/mypy/plugins/dataclasses.py
--- a/mypy/plugins/dataclasses.py
+++ b/mypy/plugins/dataclasses.py
@@ -1,3 +1,5 @@
+"""Plugin that provides support for dataclasses."""
+
 from collections import OrderedDict
 from typing import Dict, List, Set, Tuple
 
@@ -166,16 +168,26 @@ def transform(self) -> None:
         if decorator_arguments['frozen']:
             self._freeze(attributes)
 
-        # Remove init-only vars from the class.
-        for attr in attributes:
-            if attr.is_init_var:
-                del info.names[attr.name]
+        self.reset_init_only_vars(info, attributes)
 
         info.metadata['dataclass'] = {
             'attributes': OrderedDict((attr.name, attr.serialize()) for attr in attributes),
             'frozen': decorator_arguments['frozen'],
         }
 
+    def reset_init_only_vars(self, info: TypeInfo, attributes: List[DataclassAttribute]) -> None:
+        """Remove init-only vars from the class and reset init var declarations."""
+        for attr in attributes:
+            if attr.is_init_var:
+                del info.names[attr.name]
+                for stmt in info.defn.defs.body:
+                    if isinstance(stmt, AssignmentStmt) and stmt.unanalyzed_type:
+                        lvalue = stmt.lvalues[0]
+                        if isinstance(lvalue, NameExpr) and lvalue.name == attr.name:
+                            # Reset node so that another semantic analysis pass will
+                            # recreate a symbol node for this attribute.
+                            lvalue.node = None
+
     def collect_attributes(self) -> List[DataclassAttribute]:
         """Collect all attributes declared in the dataclass and its parents.
 


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
@@ -520,6 +520,26 @@ app.database_name  # E: "SpecializedApplication" has no attribute "database_name
 
 [builtins fixtures/list.pyi]
 
+[case testDataclassesInitVarsAndDefer]
+# flags: --new-semantic-analyzer
+from dataclasses import InitVar, dataclass
+
+defer: Yes
+
+@dataclass
+class Application:
+  name: str
+  database_name: InitVar[str]
+
+reveal_type(Application)  # E: Revealed type is 'def (name: builtins.str, database_name: builtins.str) -> __main__.Application'
+app = Application("example", 42)  # E: Argument 2 to "Application" has incompatible type "int"; expected "str"
+app = Application("example", "apps")
+app.name
+app.database_name  # E: "Application" has no attribute "database_name"
+
+class Yes: ...
+[builtins fixtures/list.pyi]
+
 [case testDataclassFactory]
 from typing import Type, TypeVar
 from dataclasses import dataclass