Skip to content
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

Crash when using total_ordering and a Generic class #11728

Closed
tjstum opened this issue Dec 13, 2021 · 1 comment · Fixed by #12767
Closed

Crash when using total_ordering and a Generic class #11728

tjstum opened this issue Dec 13, 2021 · 1 comment · Fixed by #12767
Labels

Comments

@tjstum
Copy link

tjstum commented Dec 13, 2021

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
@tjstum tjstum added the crash label Dec 13, 2021
@AlexWaygood
Copy link
Member

AlexWaygood commented Mar 26, 2022

Same assertion is hit in #12113, #10140, #10672, #12252 and #12112

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

Successfully merging a pull request may close this issue.

2 participants