Skip to content

New semantic analyzer: unbound base class results in crash #7129

Closed

Description

@JukkaL

This test case fails with assertion failure (Must not defer during final iteration) when using the new semantic analyzer:

[case testNewAnalyzerCyclicWithUnboundTypeVar]
from typing import Generic

class C(Generic[D]):
    pass

D = D
[out]
...

Activity

  1. self-assigned this
    on Jul 2, 2019
  2. JukkaL commented on Jul 2, 2019

    @JukkaL
    CollaboratorAuthor

    Another similar test case that causes a crash:

    [case testNewAnalyzerCyclicWithMetaclass]
    from typing import Generic
    
    class C(metaclass=D): # E: Cannot resolve name "D" (possible cyclic definition)
        pass
    
    D = D # E: Cannot resolve name "D" (possible cyclic definition)
    
  3. pbasista commented on Sep 27, 2019

    @pbasista

    A similar issue also occurs on the following Python 2 code:

    from typing import Generic, TypeVar
    import attr
    
    
    TYPE_VARIABLE = TypeVar("TYPE_VARIABLE", bound="A")
    
    
    class A(object):
        pass
    
    
    @attr.s
    class B(Generic[TYPE_VARIABLE], object):
        pass

    It crashes with the following traceback:

    test_mypy.py:13: error: INTERNAL ERROR -- Please try using mypy master on Github:
    https://mypy.rtfd.io/en/latest/common_issues.html#using-a-development-mypy-build
    Please report a bug at https://github.com/python/mypy/issues
    version: 0.740+dev.df6284201d1a9803831728d76a9763e7b6fb79c4
    Traceback (most recent call last):
      File "/home/peter/bin/mypy", line 11, in <module>
        load_entry_point('mypy', 'console_scripts', 'mypy')()
      File "/home/peter/code/mypy/mypy/__main__.py", line 8, in console_entry
        main(None, sys.stdout, sys.stderr)
      File "/home/peter/code/mypy/mypy/main.py", line 88, in main
        res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
      File "/home/peter/code/mypy/mypy/build.py", line 164, in build
        result = _build(sources, options, alt_lib_path, flush_errors, fscache, stdout, stderr)
      File "/home/peter/code/mypy/mypy/build.py", line 229, in _build
        graph = dispatch(sources, manager, stdout)
      File "/home/peter/code/mypy/mypy/build.py", line 2566, in dispatch
        process_graph(graph, manager)
      File "/home/peter/code/mypy/mypy/build.py", line 2875, in process_graph
        process_stale_scc(graph, scc, manager)
      File "/home/peter/code/mypy/mypy/build.py", line 2968, in process_stale_scc
        mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
      File "/home/peter/code/mypy/mypy/semanal_main.py", line 78, in semantic_analysis_for_scc
        process_functions(graph, scc, patches)
      File "/home/peter/code/mypy/mypy/semanal_main.py", line 238, in process_functions
        patches)
      File "/home/peter/code/mypy/mypy/semanal_main.py", line 274, in process_top_level_function
        final_iteration, patches)
      File "/home/peter/code/mypy/mypy/semanal_main.py", line 330, in semantic_analyze_target
        active_type=active_type)
      File "/home/peter/code/mypy/mypy/semanal.py", line 381, in refresh_partial
        self.accept(node)
      File "/home/peter/code/mypy/mypy/semanal.py", line 4625, in accept
        node.accept(self)
      File "/home/peter/code/mypy/mypy/nodes.py", line 668, in accept
        return visitor.visit_func_def(self)
      File "/home/peter/code/mypy/mypy/semanal.py", line 557, in visit_func_def
        self.analyze_func_def(defn)
      File "/home/peter/code/mypy/mypy/semanal.py", line 589, in analyze_func_def
        self.defer(defn)
      File "/home/peter/code/mypy/mypy/semanal.py", line 4359, in defer
        assert not self.final_iteration, 'Must not defer during final iteration'
    AssertionError: Must not defer during final iteration
    test_mypy.py:13: : note: use --pdb to drop into pdb
    

    It is interesting that the crash goes away when either:

    1. @attr.s decorator on class B is removed or
    2. the order of the declarations of TYPE_VARIABLE and class A is switched.

    Since the new semantic analyzer is the only analyzer available since mypy 0.730, it is not possible to use newer versions of mypy for checking code which contains patterns mentioned in this issue.

  4. ilevkivskyi commented on Sep 27, 2019

    @ilevkivskyi
    Member

    @pbasista I think your example is actually a different crash, see #7535

  5. hauntsaninja commented on Sep 6, 2021

    @hauntsaninja
    Collaborator

    I can't repro either of the original crashes, so closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions