python / python/mypy

[mypyc] singledispatch made outside the main namespace

Open
#13,613 0 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

Mypyc cannot compile if there's a singledispatch function defined outside the main namespace, for example in the body of a function or class.

To Reproduce

  1. Make a file with the following contents
from __future__ import annotations
from functools import singledispatch, _SingleDispatchCallable
from typing import Any


def outer() -> _SingleDispatchCallable:

    @singledispatch
    def inner(obj: object) -> Any:
        return 2

    return inner
  1. Compile with mypyc

Actual Behavior

Traceback (most recent call last):
  File "mypyc/irbuild/builder.py", line 176, in accept
  File "mypy/nodes.py", line 831, in accept
  File "mypyc/irbuild/visitor.py", line 123, in visit_decorator
  File "mypyc/irbuild/function.py", line 86, in transform_decorator
  File "mypyc/irbuild/function.py", line 312, in gen_func_item
  File "mypyc/irbuild/function.py", line 351, in gen_func_ir
src/.../mwe.py:8: KeyError: <mypy.nodes.FuncDef object at 0x10a7bfb80>

The error occurs at the location of the @singledispatch.

Your Environment

  • Mypy version used: '0.950'
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: Python 3.10.4
  • Operating system and version: macOS Monterey 12.5.1

https://github.com/python/mypy/issues/13613#tasklist-block-c661a68b-595f-4b41-98a7-bf6bcaf6e3d9

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 failure with the provided nested singledispatch example and mypyc. Start in mypyc/irbuild/visitor.py at visit_decorator, then follow transform_decorator, gen_func_item, and gen_func_ir in mypyc/irbuild/function.py. Done means mypyc compiles the example without the reported KeyError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.