Skip to content

AttributeError on __mypyc_env__ on Iterator that is yielded from twice and which calls a lambda #1210

Closed
python/mypy
#21826

Description

@DanielNoord
from typing import Iterator, Callable

def call_lambda(callable: Callable[[], None]) -> None:
    pass


def get_iterator() -> Iterator[str]:
    call_lambda(lambda: None)
    yield ""


def yield_twice() -> Iterator[str]:
    identified_imports = get_iterator()
    yield from identified_imports

    for identified_import in identified_imports:
        yield identified_import

list(yield_twice())

With mypyc test.py && python -c "import test" leads to:

❯ mypyc test.py && python -c "import test"
running build_ext
building 'test' extension
cc -fno-strict-overflow -Wsign-compare -Wunreachable-code -DNDEBUG -g -O3 -Wall -arch arm64 -mmacosx-version-min=11.0 -Wno-nullability-completeness -Wno-expansion-to-defined -Wno-undef-prefix -fPIC -I/private/tmp/mypy/mypyc/lib-rt -I/private/tmp/mypy/.venv/include -I/Users/daniel/.local/share/uv/python/cpython-3.14.0-macos-aarch64-none/include/python3.14 -c build/__native.c -o build/temp.macosx-11.0-arm64-cpython-314/build/__native.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-array-bounds -Wno-stringop-overread -Wno-stringop-overflow -Wno-cpp -Wno-array-bounds -Wno-stringop-overread -Wno-stringop-overflow
cc -bundle -undefined dynamic_lookup -arch arm64 -mmacosx-version-min=11.0 -LModules/_hacl -Wl,-headerpad,40 build/temp.macosx-11.0-arm64-cpython-314/build/__native.o -L/Users/daniel/.local/share/uv/python/cpython-3.14.0-macos-aarch64-none/lib -o build/lib.macosx-11.0-arm64-cpython-314/test.cpython-314-darwin.so
ld: warning: search path 'Modules/_hacl' not found
copying build/lib.macosx-11.0-arm64-cpython-314/test.cpython-314-darwin.so -> 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import test
  File "test.py", line 19, in <module>
    list(yield_twice())
  File "test.py", line 16, in yield_twice
    for identified_import in identified_imports:
  File "test.py", line 7, in get_iterator
    def get_iterator() -> Iterator[str]:
AttributeError: attribute '__mypyc_env__' of 'get_iterator_gen' undefined

This is another issue I ran into while trying to compile isort with mypyc (PyCQA/isort#2586).

Code runs fine without mypyc compilation.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions