python / python/mypy

Mypyc crashes on conditionally-defined class

Open
#15,716 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

crash topic-mypyc
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

To Reproduce

if True:
    class A:
        pass

(This example is silly, but a real use case would be defining a protocol inside a TYPE_CHECKING block.)

Actual Behavior

  File "mypy/mypy/nodes.py", line 1141, in accept
    return visitor.visit_class_def(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "mypy/mypyc/irbuild/visitor.py", line 167, in visit_class_def
    transform_class_def(self.builder, cdef)
  File "mypy/mypyc/irbuild/classdef.py", line 88, in transform_class_def
    ir = builder.mapper.type_to_ir[cdef.info]
         ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
asdf.py:2: KeyError: <TypeInfo asdf.A>
mypyc --version
mypy 1.6.0+dev.89ad125fa5a31a7c82f267e957d3c94da6b52f61
(compiled: no)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the conditional class example with mypyc, then read mypy/mypyc/irbuild/visitor.py and mypy/mypyc/irbuild/classdef.py around visit_class_def and transform_class_def. Trace why the conditionally defined TypeInfo is absent from type_to_ir; done means the example no longer raises the reported KeyError and the behavior is covered by regression testing.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.