mypy
mypy copied to clipboard
Mypy crashes with pydantic plugin enabled: Must not defer during final iteration
Crash Report
When I run mypy on our codebase, it works fine. When I enable pydantic plugin, it crashes.
Pyproject:
[tool.mypy]
plugins = [
"pydantic.mypy"
]
python_version = "3.10"
follow_imports = "silent"
# Start off with these
warn_unused_configs = false
warn_redundant_casts = false
warn_unused_ignores = false
# Getting these passing should be easy
strict_equality = false
strict_concatenate = false
# Strongly recommend enabling this one as soon as you can
check_untyped_defs = false
# These shouldn't be too much additional work, but may be tricky to
# get passing if you use a lot of untyped libraries
disallow_subclassing_any = false
disallow_untyped_decorators = false
disallow_any_generics = false
# These next few are various gradations of forcing use of type annotations
disallow_untyped_calls = false
disallow_incomplete_defs = false
disallow_untyped_defs = false
# This one isn't too hard to get passing, but return on investment is lower
no_implicit_reexport = false
# This one can be tricky to get passing if you use a lot of untyped libraries
warn_return_any = false
Traceback
version: 1.6.0+dev.7141d6bcff9e26e774e88712015ca6bbe8307c9e
Traceback (most recent call last):
File "/Users/foobar/Library/Caches/pypoetry/virtualenvs/foobar-py3.10/bin/mypy", line 8, in <module>
sys.exit(console_entry())
File "/private/tmp/mypy/mypy/__main__.py", line 15, in console_entry
main()
File "/private/tmp/mypy/mypy/main.py", line 99, in main
res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
File "/private/tmp/mypy/mypy/main.py", line 178, in run_build
res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
File "/private/tmp/mypy/mypy/build.py", line 189, in build
result = _build(
File "/private/tmp/mypy/mypy/build.py", line 262, in _build
graph = dispatch(sources, manager, stdout)
File "/private/tmp/mypy/mypy/build.py", line 2938, in dispatch
process_graph(graph, manager)
File "/private/tmp/mypy/mypy/build.py", line 3336, in process_graph
process_stale_scc(graph, scc, manager)
File "/private/tmp/mypy/mypy/build.py", line 3431, in process_stale_scc
mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
File "/private/tmp/mypy/mypy/semanal_main.py", line 94, in semantic_analysis_for_scc
process_functions(graph, scc, patches)
File "/private/tmp/mypy/mypy/semanal_main.py", line 252, in process_functions
process_top_level_function(
File "/private/tmp/mypy/mypy/semanal_main.py", line 291, in process_top_level_function
deferred, incomplete, progress = semantic_analyze_target(
File "/private/tmp/mypy/mypy/semanal_main.py", line 349, in semantic_analyze_target
analyzer.refresh_partial(
File "/private/tmp/mypy/mypy/semanal.py", line 603, in refresh_partial
self.accept(node)
File "/private/tmp/mypy/mypy/semanal.py", line 6477, in accept
node.accept(self)
File "/private/tmp/mypy/mypy/nodes.py", line 790, in accept
return visitor.visit_func_def(self)
File "/private/tmp/mypy/mypy/semanal.py", line 833, in visit_func_def
self.analyze_func_def(defn)
File "/private/tmp/mypy/mypy/semanal.py", line 868, in analyze_func_def
self.defer(defn)
File "/private/tmp/mypy/mypy/semanal.py", line 6149, in defer
assert not self.final_iteration, "Must not defer during final iteration"
AssertionError: Must not defer during final iteration
To Reproduce
from typing import *
from pydantic import BaseModel
AnyBaseFoo = TypeVar("AnyBaseFoo", bound="BaseFoo")
class BaseFoo:
pass
class Foo(BaseModel, Generic[AnyBaseFoo]):
pass
Your Environment
- Mypy version used:
1.6.0+dev.7141d6bcff9e26e774e88712015ca6bbe8307c9e - Mypy command-line flags:
mypy foobar/baz --show-traceback - Python version used: 3.10.10
- Operating system and version: Darwin mac 22.6.0, rosetta was used
A workaround has been found:
from typing import *
from pydantic import BaseModel
class BaseFoo:
pass
AnyBaseFoo = TypeVar("AnyBaseFoo", bound="BaseFoo")
class Foo(BaseModel, Generic[AnyBaseFoo]):
pass
Declaring the TypeVar anywhere after its bound avoids the crash.
A workaround has been found:
from typing import * from pydantic import BaseModel class BaseFoo: pass AnyBaseFoo = TypeVar("AnyBaseFoo", bound="BaseFoo") class Foo(BaseModel, Generic[AnyBaseFoo]): passDeclaring the
TypeVaranywhere after its bound avoids the crash.
This isn't a great/complete answer IMO because you sometimes need the TypeVar in class methods and such defined on the class.