Skip to content

Crash when overriding field as property/method on frozen derived class #8334

Description

@jjlee

I see this on python/mypy master (41b40aa) and Python 3.7.5.

Following on from #7535 (fixed recently by @msullivan), this triggers another crash (really just adding the frozen=True from my example on that bug report, though I also dropped the args to field since I realised I could):

from dataclasses import dataclass, field

@dataclass
class Base:
    foo: bool = field()

@dataclass(frozen=True)
class Derived(Base):
    @property
    def foo(self):
        pass

Traceback (seems slightly different from #6715 -- the assertion here is in method _freeze, not method collect_attributes):

$ mypy --show-traceback /home/me/minimal3.py
/home/me/minimal3.py:10: 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.770+dev.41b40aa4fc7cdeb32f5ee4fec97f5379ebbe795d
Traceback (most recent call last):
  File "/home/me/env/3/moving-target/bin/mypy", line 11, in <module>
    load_entry_point('mypy', 'console_scripts', 'mypy')()
  File "/home/me/dev/mypy/mypy/__main__.py", line 8, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "/home/me/dev/mypy/mypy/main.py", line 89, in main
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/me/dev/mypy/mypy/build.py", line 166, in build
    sources, options, alt_lib_path, flush_errors, fscache, stdout, stderr, extra_plugins
  File "/home/me/dev/mypy/mypy/build.py", line 234, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/me/dev/mypy/mypy/build.py", line 2631, in dispatch
    process_graph(graph, manager)
  File "/home/me/dev/mypy/mypy/build.py", line 2938, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/me/dev/mypy/mypy/build.py", line 3031, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "/home/me/dev/mypy/mypy/semanal_main.py", line 77, in semantic_analysis_for_scc
    process_top_levels(graph, scc, patches)
  File "/home/me/dev/mypy/mypy/semanal_main.py", line 202, in process_top_levels
    patches)
  File "/home/me/dev/mypy/mypy/semanal_main.py", line 330, in semantic_analyze_target
    active_type=active_type)
  File "/home/me/dev/mypy/mypy/semanal.py", line 363, in refresh_partial
    self.refresh_top_level(node)
  File "/home/me/dev/mypy/mypy/semanal.py", line 374, in refresh_top_level
    self.accept(d)
  File "/home/me/dev/mypy/mypy/semanal.py", line 4682, in accept
    node.accept(self)
  File "/home/me/dev/mypy/mypy/nodes.py", line 939, in accept
    return visitor.visit_class_def(self)
  File "/home/me/dev/mypy/mypy/semanal.py", line 1014, in visit_class_def
    self.analyze_class(defn)
  File "/home/me/dev/mypy/mypy/semanal.py", line 1091, in analyze_class
    self.analyze_class_body_common(defn)
  File "/home/me/dev/mypy/mypy/semanal.py", line 1100, in analyze_class_body_common
    self.apply_class_plugin_hooks(defn)
  File "/home/me/dev/mypy/mypy/semanal.py", line 1146, in apply_class_plugin_hooks
    hook(ClassDefContext(defn, decorator, self))
  File "/home/me/dev/mypy/mypy/plugins/dataclasses.py", line 369, in dataclass_class_maker_callback
    transformer.transform()
  File "/home/me/dev/mypy/mypy/plugins/dataclasses.py", line 182, in transform
    self._freeze(attributes)
  File "/home/me/dev/mypy/mypy/plugins/dataclasses.py", line 355, in _freeze
    assert isinstance(var, Var)
AssertionError: 
/home/me/minimal3.py:10: : note: use --pdb to drop into pdb

Activity

  1. added a commit that references this issue on Jan 26, 2020
  2. anis-campos commented on Jun 14, 2021

    @anis-campos

    Is there any workaround that would make this less of a crash but more of a warning ? warning that could therefore be ignore just like the others ones ?

    It's so sad no being able to properly override inherited properties with @property, it's becomes very difficult to impose immutability of the dataclasses without some cumbersome workaround via __post_init__ and object.setattr

  3. JukkaL commented on Aug 24, 2022

    @JukkaL
    Collaborator

    In #8976 there was a similar-looking crash when overriding a frozen attribute with a method:

    from dataclasses import dataclass
    
    
    @dataclass(frozen=True)
    class Foo:
        attr: int
    
    
    @dataclass(frozen=True)
    class Bar(Foo):
        def attr(self) -> int:
            return 1
  4. changed the title [-]Traceback with overridden field property on frozen derived class[/-] [+]Crash when overriding field as property/method on frozen derived class[/+] on Aug 24, 2022
  5. anis-campos commented on Aug 24, 2022

    @anis-campos

    Isn't this related to #13475 ? Well I guess not completely, as for a frozen dataclass, a read-only property should be enough

  6. added a commit that references this issue on Aug 27, 2022
    9361b9a
  7. added a commit that references this issue on Aug 27, 2022
    3f0ac21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions