Skip to content

Crash when using total_ordering and a Generic class #11728

@tjstum

Description

@tjstum

Crash Report

When using a test snippet that uses functools.total_ordering and a Generic class whose TypeVar is bound by some other class, there is an internal crash in mypy. The reproduction cases are below the traceback.

This may be related to #8539, but in this case, there is no abstract class involved

Traceback

$ mypy --show-traceback test_mypy.py
test_mypy.py:9: 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.920+dev.01a2d98bc480ea9e1575f2adab47c0cfbca1c8d0
Traceback (most recent call last):
  File "/usr/scratch/stum/mypy-repro/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/usr/scratch/stum/mypy-repro/lib/python3.8/site-packages/mypy/__main__.py", line 12, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "/usr/scratch/stum/mypy-repro/lib/python3.8/site-packages/mypy/main.py", line 96, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/usr/scratch/stum/mypy-repro/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 "/usr/scratch/stum/mypy-repro/lib/python3.8/site-packages/mypy/build.py", line 180, in build
    result = _build(
  File "/usr/scratch/stum/mypy-repro/lib/python3.8/site-packages/mypy/build.py", line 256, in _build
    graph = dispatch(sources, manager, stdout)
  File "/usr/scratch/stum/mypy-repro/lib/python3.8/site-packages/mypy/build.py", line 2720, in dispatch
    process_graph(graph, manager)
  File "/usr/scratch/stum/mypy-repro/lib/python3.8/site-packages/mypy/build.py", line 3051, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/usr/scratch/stum/mypy-repro/lib/python3.8/site-packages/mypy/build.py", line 3143, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "/usr/scratch/stum/mypy-repro/lib/python3.8/site-packages/mypy/semanal_main.py", line 79, in semantic_analysis_for_scc
    process_functions(graph, scc, patches)
  File "/usr/scratch/stum/mypy-repro/lib/python3.8/site-packages/mypy/semanal_main.py", line 233, in process_functions
    process_top_level_function(analyzer,
  File "/usr/scratch/stum/mypy-repro/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 "/usr/scratch/stum/mypy-repro/lib/python3.8/site-packages/mypy/semanal_main.py", line 326, in semantic_analyze_target
    analyzer.refresh_partial(refresh_node,
  File "/usr/scratch/stum/mypy-repro/lib/python3.8/site-packages/mypy/semanal.py", line 408, in refresh_partial
    self.accept(node)
  File "/usr/scratch/stum/mypy-repro/lib/python3.8/site-packages/mypy/semanal.py", line 5092, in accept
    node.accept(self)
  File "/usr/scratch/stum/mypy-repro/lib/python3.8/site-packages/mypy/nodes.py", line 727, in accept
    return visitor.visit_func_def(self)
  File "/usr/scratch/stum/mypy-repro/lib/python3.8/site-packages/mypy/semanal.py", line 621, in visit_func_def
    self.analyze_func_def(defn)
  File "/usr/scratch/stum/mypy-repro/lib/python3.8/site-packages/mypy/semanal.py", line 653, in analyze_func_def
    self.defer(defn)
  File "/usr/scratch/stum/mypy-repro/lib/python3.8/site-packages/mypy/semanal.py", line 4776, in defer
    assert not self.final_iteration, 'Must not defer during final iteration'
AssertionError: Must not defer during final iteration
test_mypy.py:9: : note: use --pdb to drop into pdb

To Reproduce

Here is basically the smallest example I could come up with:

from typing import Generic, Any, TypeVar
import functools

T = TypeVar("T", bound="C")

@functools.total_ordering
class D(Generic[T]):
    def __lt__(self, other: Any) -> bool:
        ...


class C:
    pass

A workaround (to suppress the crash) is to use a total_ordering that is essentially a no-op, that skips the mypy total_ordering plugin. Some ways that I've done this are:

import functools
total_ordering = functools.total_ordering

@total_ordering
class D(Generic[T]): ...

or

if TYPE_CHECKING:
    _T = TypeVar("_T")
    def total_ordering(cls: Type[_T]) -> Type[_T]:
        return cls
else:
    from functools import total_ordering

@total_ordering
class D(Generic[T]): ...

Your Environment

  • Mypy version used: 01a2d98
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): No mypy.ini
  • Python version used: Python 3.8
  • Operating system and version: Debian 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions