python / python/mypy

Mypyc gcc compilation fails with specific duplicate variable definition

Abierto
#19,557 2 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

crash topic-mypyc
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Crash Report

Using mypyc to compile simple.py (slimmed down example of real use-case wherein I accidentally set typed variable twice in different ways) leads to compilation failure with GCC instead of mypy noticing something is amiss.

simple.py

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from mypy_extensions import u8


def next_down(
    depth: u8 = 5,
) -> u8:
    next_down = depth - 1

    ##### KEY TO FAILURE #####
    next_down = None if depth is None else depth - 1
    ##########################
    return next_down


if __name__ == "__main__":
    print(next_down())
mypyc simple.py

Traceback

/home/<username>/Desktop/mypyc-crash/.venv/lib/python3.13/site-packages/setuptools/config/_apply_pyprojecttoml.py:82: SetuptoolsDeprecationWarning: `project.license` as a TOML table is deprecated
!!

        ********************************************************************************
        Please use a simple string containing a SPDX expression for `project.license`. You can also use `project.license-files`. (Both options available on setuptools>=77.0.0).

        By 2026-Feb-18, you need to update your project and remove deprecated calls
        or your builds will no longer be supported.

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  corresp(dist, value, root_dir)
running build_ext
building 'package_name.simple__mypyc' extension
x86_64-linux-gnu-gcc -fno-strict-overflow -Wsign-compare -DNDEBUG -g -O2 -Wall -fPIC -I/home/<username>/Desktop/mypyc-crash/.venv/lib/python3.13/site-packages/mypyc/lib-rt -Ibuild -I/home/<username>/Desktop/mypyc-crash/.venv/include -I/usr/include/python3.13 -c build/package_name/__native_simple.c -o build/temp.linux-x86_64-cpython-313/build/package_name/__native_simple.o -O3 -g1 -Werror -Wno-unused-function -Wno-unused-label -Wno-unreachable-code -Wno-unused-variable -Wno-unused-command-line-argument -Wno-unknown-warning-option -Wno-unused-but-set-variable -Wno-ignored-optimization-argument -Wno-cpp
In file included from /home/<username>/Desktop/mypyc-crash/.venv/lib/python3.13/site-packages/mypyc/lib-rt/init.c:2,
                 from build/package_name/__native_simple.c:1:
In function ‘CPyLong_AsUInt8’,
    inlined from ‘CPyDef_next_down’ at build/package_name/__native_simple.c:95:16:
/home/<username>/Desktop/mypyc-crash/.venv/lib/python3.13/site-packages/mypyc/lib-rt/CPy.h:396:16: error: array subscript ‘PyLongObject {aka struct _longobject}[0]’ is partly outside array bounds of ‘PyObject[1]’ {aka ‘struct _object[1]’} [-Werror=array-bounds=]
  396 |         size_t tag = CPY_LONG_TAG(lobj);
      |                ^~~
In file included from /usr/include/python3.13/Python.h:72,
                 from /home/<username>/Desktop/mypyc-crash/.venv/lib/python3.13/site-packages/mypyc/lib-rt/init.c:1:
/usr/include/python3.13/object.h: In function ‘CPyDef_next_down’:
/usr/include/python3.13/object.h:1101:22: note: object ‘_Py_NoneStruct’ of size 16
 1101 | PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
      |                      ^~~~~~~~~~~~~~
In function ‘CPyLong_AsUInt8’,
    inlined from ‘CPyDef_next_down’ at build/package_name/__native_simple.c:95:16:
/home/<username>/Desktop/mypyc-crash/.venv/lib/python3.13/site-packages/mypyc/lib-rt/CPy.h:399:19: error: array subscript ‘PyLongObject {aka struct _longobject}[0]’ is partly outside array bounds of ‘PyObject[1]’ {aka ‘struct _object[1]’} [-Werror=array-bounds=]
  399 |             digit x = CPY_LONG_DIGIT(lobj, 0);
      |                   ^
/usr/include/python3.13/object.h: In function ‘CPyDef_next_down’:
/usr/include/python3.13/object.h:1101:22: note: object ‘_Py_NoneStruct’ of size 16
 1101 | PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
      |                      ^~~~~~~~~~~~~~
At top level:
cc1: note: unrecognized command-line option ‘-Wno-ignored-optimization-argument’ may have been intended to silence earlier diagnostics
cc1: note: unrecognized command-line option ‘-Wno-unknown-warning-option’ may have been intended to silence earlier diagnostics
cc1: note: unrecognized command-line option ‘-Wno-unused-command-line-argument’ may have been intended to silence earlier diagnostics
cc1: all warnings being treated as errors
error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1

To Reproduce

See above

Your Environment

  • Mypy version used: mypy 1.17.1 (compiled: yes)
  • Mypy command-line flags: See above
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: Python 3.13.3 (main, Jun 16 2025, 18:15:32) [GCC 14.2.0] on linux
  • Operating system and version:
> uname -a
Linux ideapad 6.14.0-24-generic #24-Ubuntu SMP PREEMPT_DYNAMIC Sun Jun 15 11:18:07 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
  • GCC version
> x86_64-linux-gnu-gcc --version
x86_64-linux-gnu-gcc (Ubuntu 14.2.0-19ubuntu2) 14.2.0
...

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza reproduciendo el ejemplo de simple.py con mypyc e inspecciona build/package_name/__native_simple.c alrededor de CPyDef_next_down. Lee la lógica de conversión relacionada en CPy.h, especialmente CPyLong_AsUInt8, y compara cómo la asignación repetida produce el código generado que falla. La tarea estará terminada cuando esta entrada ya no provoque un fallo de compilación de GCC y haya una cobertura de regresión o diagnósticos adecuados.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
c, python
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.