Skip to content

TypeError/SystemError for dataclasses that uses default_factory and is subclassed #1204

Closed
python/mypy
#21785

Description

@DanielNoord
from importlib.metadata import version
from dataclasses import dataclass, field


@dataclass
class _Test:
    x: dict = field(default_factory=dict)


class Test(_Test):
    pass


print("Mypy version:", version("mypy"))
print(Test())

With mypyc test.py && python -c "import test" leads to:

❯ mypyc test.py && python -c "import test"
test.py:7: warning: Unsupported default attribute value
running build_ext
building 'test' extension
clang -fno-strict-overflow -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -O3 -Wall -I/private/tmp/mypyc/.venv/lib/python3.14/site-packages/mypyc/lib-rt -I/private/tmp/mypyc/.venv/include -I/opt/homebrew/opt/[email protected]/Frameworks/Python.framework/Versions/3.14/include/python3.14 -c build/__native.c -o build/temp.macosx-26.0-arm64-cpython-314/build/__native.o -O3 -g1 -Werror -Wno-unused-function -Wno-unused-label -Wno-unreachable-code -Wno-unused-variable -Wno-unused-command-line-argument -Wno-unknown-warning-option -Wno-unused-but-set-variable -Wno-ignored-optimization-argument -Wno-array-bounds -Wno-stringop-overread -Wno-stringop-overflow -Wno-cpp -Wno-array-bounds -Wno-stringop-overread -Wno-stringop-overflow
clang -bundle -undefined dynamic_lookup build/temp.macosx-26.0-arm64-cpython-314/build/__native.o -o build/lib.macosx-26.0-arm64-cpython-314/test.cpython-314-darwin.so
copying build/lib.macosx-26.0-arm64-cpython-314/test.cpython-314-darwin.so -> 
Mypy version: 2.3.0
TypeError: dict object expected; got dataclasses.Field

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import test
  File "test.py", line 15, in <module>
    print(Test())
  File "<string>", line 3, in __init__
SystemError: <class 'dict'> returned a result with an exception set

Ran into this in the wild while trying to compile isort with mypyc (PyCQA/isort#2586).

Code runs fine without mypyc compilation.

Activity

  1. DanielNoord commented on Jul 28, 2026

    @DanielNoord
    Author

    Submitted a fix in python/mypy#21785. Using that fix does seem to compile isort correctly, or at least, it avoids the previous crash :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions