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

Mixin class factory crash #12112

Closed
DanielSank opened this issue Feb 2, 2022 · 3 comments · Fixed by #13678
Closed

Mixin class factory crash #12112

DanielSank opened this issue Feb 2, 2022 · 3 comments · Fixed by #13678
Assignees
Labels

Comments

@DanielSank
Copy link

DanielSank commented Feb 2, 2022

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
@sobolevn
Copy link
Member

sobolevn commented Feb 3, 2022

Thanks, I will take a look today!

@sobolevn sobolevn self-assigned this Feb 3, 2022
@AlexWaygood
Copy link
Member

AlexWaygood commented Mar 26, 2022

Same assertion is hit in #12252, #12113, #11728 and #10140

@hauntsaninja
Copy link
Collaborator

A smaller example from #10140 (comment) :

from typing import TypeVar

def test() -> None:
    T = TypeVar('T', bound='Foo')

    class Foo:
        def bar(self, foo: T) -> None:
            pass

Michael0x2a added a commit to Michael0x2a/mypy that referenced this issue Sep 18, 2022
Fixes python#12112.

The reason why mypy was crashing with a "Must not defer during final
iteration" error in the following snippet:

    from typing import TypeVar

    def test() -> None:
        T = TypeVar('T', bound='Foo')

        class Foo:
            def bar(self, foo: T) -> None:
                pass

...was because mypy did not seem to be updating the types of the `bar`
callable on each pass: the `bind_function_type_variables` method in
`typeanal.py` always returned the _old_ type variables instead of using
the new updated ones we found by calling `self.lookup_qualified(...)`.

This in turn prevented us from making any forward progress when mypy
generated a CallableType containing a placedholder type variable. So,
we repeated the semanal passes until we hit the limit and crashed.

I opted to fix this by having the function always return the newly-bound
TypeVarLikeType instead. (Hopefully this is safe -- the way mypy likes
mutating types always makes it hard to reason about this sort of stuff).

Interestingly, my fix for this bug introduced a regression in one of
our existing tests:

    from typing import NamedTuple, TypeVar

    T = TypeVar("T")
    NT = NamedTuple("NT", [("key", int), ("value", T)])

    # Test thinks the revealed type should be:
    #     def [T] (key: builtins.int, value: T`-1) -> Tuple[builtins.int, T`-1, fallback=__main__.NT[T`-1]]
    #
    # ...but we started seeing:
    #     def [T, _NT <: Tuple[builtins.int, T`-1]] (key: builtins.int, value: T`-1) -> Tuple[builtins.int, T`-1, fallback=test.WTF[T`-1]]
    reveal_type(NT)

What seems to be happening here is that during the first pass, we add
two type vars to the `tvar_scope` inside `bind_function_type_variables`:
`T` with id -1 and `_NT` with id -2.

But in the second pass, we lose track of the `T` typevar definition and/or
introduce a fresh scope somewhere and infer `_NT` with id -1 instead?

So now mypy thinks there are two type variables associated with this
NamedTuple, which results in the screwed-up type definition.

I wasn't really sure how to fix this, but I thought it was weird that:

1. We were using negative IDs instead of positive ones. The former is
   meant to be for class definitions, which is what we're using here.
2. We weren't wrapping this whole thing in a new tvar scope frame, given
   we're nominally synthesizing a new class.

So I did that, and the tests started passing?

I wasn't able to repro this issue for TypedDicts, but opted to introduce
a new tvar scope frame there as well for consistency.
Michael0x2a added a commit that referenced this issue Sep 25, 2022
Fixes #12112.

The reason why mypy was crashing with a "Must not defer during final
iteration" error in the following snippet:

    from typing import TypeVar

    def test() -> None:
        T = TypeVar('T', bound='Foo')

        class Foo:
            def bar(self, foo: T) -> None:
                pass

...was because mypy did not seem to be updating the types of the `bar`
callable on each pass: the `bind_function_type_variables` method in
`typeanal.py` always returned the _old_ type variables instead of using
the new updated ones we found by calling `self.lookup_qualified(...)`.

This in turn prevented us from making any forward progress when mypy
generated a CallableType containing a placedholder type variable. So, we
repeated the semanal passes until we hit the limit and crashed.

I opted to fix this by having the function always return the newly-bound
TypeVarLikeType instead. (Hopefully this is safe -- the way mypy likes
mutating types always makes it hard to reason about this sort of stuff).

Interestingly, my fix for this bug introduced a regression in one of our
existing tests:

    from typing import NamedTuple, TypeVar

    T = TypeVar("T")
    NT = NamedTuple("NT", [("key", int), ("value", T)])

    # Test thinks the revealed type should be:
# def [T] (key: builtins.int, value: T`-1) -> Tuple[builtins.int, T`-1,
fallback=__main__.NT[T`-1]]
    #
    # ...but we started seeing:
# def [T, _NT <: Tuple[builtins.int, T`-1]] (key: builtins.int, value:
T`-1) -> Tuple[builtins.int, T`-1, fallback=test.WTF[T`-1]]
    reveal_type(NT)

What seems to be happening here is that during the first pass, we add
two type vars to the `tvar_scope` inside `bind_function_type_variables`:
`T` with id -1 and `_NT` with id -2.

But in the second pass, we lose track of the `T` typevar definition
and/or introduce a fresh scope somewhere and infer `_NT` with id -1
instead?

So now mypy thinks there are two type variables associated with this
NamedTuple, which results in the screwed-up type definition.

I wasn't really sure how to fix this, but I thought it was weird that:

1. We were using negative IDs instead of positive ones. (Class typevars
are supposed to use the latter).
2. We weren't wrapping this whole thing in a new tvar scope frame, given
we're nominally synthesizing a new class.

So I did that, and the tests started passing?

I wasn't able to repro this issue for TypedDicts, but opted to introduce
a new tvar scope frame there as well for consistency.
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.

4 participants