[ty] Emit a diagnostic when frozen dataclass inherits a non-frozen dataclass and the other way around#21962
Conversation
…s and the other way around
Diagnostic diff on typing conformance testsChanges were detected when running ty on typing conformance tests--- old-output.txt 2025-12-13 20:51:37.689793991 +0000
+++ new-output.txt 2025-12-13 20:51:41.343814255 +0000
@@ -282,6 +282,8 @@
dataclasses_final.py:38:1: error[invalid-assignment] Cannot assign to final attribute `final_with_default` on type `<class 'D'>`
dataclasses_frozen.py:16:1: error[invalid-assignment] Property `a` defined in `DC1` is read-only
dataclasses_frozen.py:17:1: error[invalid-assignment] Property `b` defined in `DC1` is read-only
+dataclasses_frozen.py:23:7: error[invalid-frozen-dataclass-subclass] Non-frozen dataclass `DC2` cannot inherit from frozen dataclass `DC1`
+dataclasses_frozen.py:33:7: error[invalid-frozen-dataclass-subclass] Frozen dataclass `DC4` cannot inherit from non-frozen dataclass `DC3`
dataclasses_kwonly.py:23:11: error[too-many-positional-arguments] Too many positional arguments: expected 1, got 2
dataclasses_kwonly.py:38:11: error[too-many-positional-arguments] Too many positional arguments: expected 1, got 2
dataclasses_kwonly.py:53:11: error[too-many-positional-arguments] Too many positional arguments: expected 1, got 2
@@ -342,6 +344,7 @@
dataclasses_transform_func.py:70:8: error[missing-argument] No arguments provided for required parameters `id`, `name`
dataclasses_transform_func.py:70:18: error[too-many-positional-arguments] Too many positional arguments: expected 0, got 2
dataclasses_transform_func.py:76:36: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `T@create_model_frozen`
+dataclasses_transform_func.py:89:7: error[invalid-frozen-dataclass-subclass] Non-frozen dataclass `Customer3Subclass` cannot inherit from frozen dataclass `Customer3`
dataclasses_transform_func.py:96:1: error[invalid-assignment] Property `id` defined in `Customer3` is read-only
dataclasses_transform_meta.py:63:1: error[invalid-assignment] Property `id` defined in `Customer1` is read-only
dataclasses_transform_meta.py:66:8: error[missing-argument] No arguments provided for required parameters `id`, `name`
@@ -1022,4 +1025,4 @@
typeddicts_usage.py:28:17: error[missing-typed-dict-key] Missing required key 'name' in TypedDict `Movie` constructor
typeddicts_usage.py:28:18: error[invalid-key] Unknown key "title" for TypedDict `Movie`: Unknown key "title"
typeddicts_usage.py:40:24: error[invalid-type-form] The special form `typing.TypedDict` is not allowed in type expressions
-Found 1024 diagnostics
+Found 1027 diagnostics
|
|
AlexWaygood
left a comment
There was a problem hiding this comment.
Nice, we should definitely emit these diagnostics! These always raise exceptions at runtime, so it's a great thing for a type checker to catch.
But let's improve the documentation and add some tests.
|
The typing conformance suite results are great! They show us now emitting diagnostics on several places where we are meant to, but where we previously didn't. The mypy_primer results show us now emitting three false-positive diagnostics on The fact that we now emit a few false positives on |
|
Maybe these should just be one error code? |
Summary
Introduce a new diagnostic that is raised in the following situations:
Test Plan
Tests have been added.
The diagnostics include example codes to test.