Skip to content

Mixin class factory crash #12112

@DanielSank

Description

@DanielSank

Crash Report

I run mypy against a file and it crashes.

Traceback

(pyle) danielsank@danielsank1:~/Desktop/test$ mypy --show-traceback code.py 
code.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.940+dev.c0e49abcf27ff59e761f3ec06a24ee94e700464d
Traceback (most recent call last):
  File "/home/danielsank/.virtualenvs/pyle/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/__main__.py", line 12, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/main.py", line 96, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/main.py", line 173, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/build.py", line 180, in build
    result = _build(
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/build.py", line 256, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/build.py", line 2715, in dispatch
    process_graph(graph, manager)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/build.py", line 3046, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/build.py", line 3138, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal_main.py", line 79, in semantic_analysis_for_scc
    process_functions(graph, scc, patches)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal_main.py", line 233, in process_functions
    process_top_level_function(analyzer,
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal_main.py", line 274, in process_top_level_function
    deferred, incomplete, progress = semantic_analyze_target(target, state, node, active_type,
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal_main.py", line 326, in semantic_analyze_target
    analyzer.refresh_partial(refresh_node,
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal.py", line 415, in refresh_partial
    self.accept(node)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal.py", line 5171, in accept
    node.accept(self)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/nodes.py", line 730, in accept
    return visitor.visit_func_def(self)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal.py", line 628, in visit_func_def
    self.analyze_func_def(defn)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal.py", line 671, in analyze_func_def
    self.analyze_function_body(defn)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal.py", line 1001, in analyze_function_body
    defn.body.accept(self)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/nodes.py", line 1077, in accept
    return visitor.visit_block(self)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal.py", line 3566, in visit_block
    self.accept(s)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal.py", line 5171, in accept
    node.accept(self)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/nodes.py", line 1008, in accept
    return visitor.visit_class_def(self)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal.py", line 1111, in visit_class_def
    self.analyze_class(defn)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal.py", line 1189, in analyze_class
    self.analyze_class_body_common(defn)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal.py", line 1197, in analyze_class_body_common
    defn.defs.accept(self)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/nodes.py", line 1077, in accept
    return visitor.visit_block(self)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal.py", line 3566, in visit_block
    self.accept(s)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal.py", line 5171, in accept
    node.accept(self)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/nodes.py", line 730, in accept
    return visitor.visit_func_def(self)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal.py", line 628, in visit_func_def
    self.analyze_func_def(defn)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal.py", line 660, in analyze_func_def
    self.defer(defn)
  File "/home/danielsank/.virtualenvs/pyle/lib/python3.8/site-packages/mypy/semanal.py", line 4871, in defer
    assert not self.final_iteration, 'Must not defer during final iteration'
AssertionError: Must not defer during final iteration
code.py:14: : note: use --pdb to drop into pdb

To Reproduce

The offending code:

from typing import cast, Dict, Generic, Type, TypeVar

def make_mixin(key: str):
    T = TypeVar('T', bound="Mixin")
    # T represents an instance of a Mixin subclass.


    class Mixin(Generic[T]):

        KEY: str = key

        def to_str(self) -> str:
            return "a string"

        @classmethod
        def from_str(cls: Type[T], s: str) -> T:
            print(s)
            instance = cast(T, cls())
            return instance

        @classmethod
        def from_dict(cls: Type[T], d: Dict[str, str]) -> T:
            s = d[cls.KEY]
            return cls.from_str(s)

On the other hand, this code does not cause a crash

from typing import cast, Dict, Generic, Type, TypeVar



T = TypeVar('T', bound="Mixin")
# T represents an instance of a Mixin subclass.


class Mixin(Generic[T]):

    KEY: str = 'foo'

    def to_str(self) -> str:
        return "a string"

    @classmethod
    def from_str(cls: Type[T], s: str) -> T:
        print(s)
        instance = cast(T, cls())
        return instance

    @classmethod
    def from_dict(cls: Type[T], d: Dict[str, str]) -> T:
        s = d[cls.KEY]
        return cls.from_str(s)

Your Environment

  • Mypy version used: mypy 0.940+dev.c0e49abcf27ff59e761f3ec06a24ee94e700464d
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.8.8
  • Operating system and version: Debian derivative

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions