Skip to content

mypy crashes on callable frozen dataclass #12084

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

Closed
alexei opened this issue Jan 27, 2022 · 2 comments
Closed

mypy crashes on callable frozen dataclass #12084

alexei opened this issue Jan 27, 2022 · 2 comments

Comments

@alexei
Copy link

alexei commented Jan 27, 2022

Crash Report

mypy crashes on callable frozen dataclass

Traceback

➜  mypy-bug poetry run mypy mypy_bug/case_3.py --show-traceback
mypy_bug/case_3.py:14: 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.931
Traceback (most recent call last):
  File "mypy/semanal.py", line 5142, in accept
  File "mypy/nodes.py", line 1005, in accept
  File "mypy/semanal.py", line 1105, in visit_class_def
  File "mypy/semanal.py", line 1184, in analyze_class
  File "mypy/semanal.py", line 1193, in analyze_class_body_common
  File "mypy/semanal.py", line 1239, in apply_class_plugin_hooks
  File "mypy/plugins/dataclasses.py", line 489, in dataclass_class_maker_callback
  File "mypy/plugins/dataclasses.py", line 194, in transform
  File "mypy/plugins/dataclasses.py", line 431, in _freeze
AssertionError: 
mypy_bug/case_3.py:14: : note: use --pdb to drop into pdb

To Reproduce

from dataclasses import dataclass
from typing import Any, Callable

# here both `Parent` and `Child` are frozen dataclasses

@dataclass(frozen=True)
class Parent:
    __call__: Callable[..., None]

    def __call__(self, *args: Any, **kwargs: Any) -> None:  # type: ignore
        raise NotImplementedError


@dataclass(frozen=True)
class Child(Parent):
    def __call__(self, value: int) -> None:
        ...

Your Environment

  • Mypy version used: 0.931
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10.0
  • Operating system and version: macOS Catalina (10.15)

I'm using "Guido's hack" from #9560 (comment) which does work in other circumstances:

from typing import Any, Callable

# here both `Parent` and `Child` are "regular" classes

class Parent:
    __call__: Callable[..., None]

    def __call__(self, *args: Any, **kwargs: Any) -> None:  # type: ignore
        raise NotImplementedError


class Child(Parent):
    def __call__(self, value: int) -> None:
        ...
from dataclasses import dataclass
from typing import Any, Callable

# here `Parent` is a frozen dataclass, but `Child` is not

@dataclass(frozen=True)
class Parent:
    __call__: Callable[..., None]

    def __call__(self, *args: Any, **kwargs: Any) -> None:  # type: ignore
        raise NotImplementedError


class Child(Parent):
    def __call__(self, value: int) -> None:
        ...
from dataclasses import dataclass
from typing import Any, Callable

# here both `Parent` and `Child` are dataclasses, but not frozen

@dataclass
class Parent:
    __call__: Callable[..., None]

    def __call__(self, *args: Any, **kwargs: Any) -> None:  # type: ignore
        raise NotImplementedError


@dataclass
class Child(Parent):
    def __call__(self, value: int) -> None:
        ...
@JukkaL
Copy link
Collaborator

JukkaL commented Jun 15, 2022

This no longer crashes. It seems to have been fixed in 0.950.

@JukkaL JukkaL closed this as completed Jun 15, 2022
@alexei
Copy link
Author

alexei commented Jun 15, 2022

👍

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

Successfully merging a pull request may close this issue.

4 participants