mypyc icon indicating copy to clipboard operation
mypyc copied to clipboard

Compiled nested functions don't retain their __name__ attribute

Open ichard26 opened this issue 4 years ago • 3 comments

from typing import Callable

def outer() -> Callable:
    def inner() -> None:
        pass

    return inner

print(f"outer.__name__: {outer.__name__}")
print(f"inner.__name__: {outer().__name__}")
❯ python nested.py
outer.__name__: outer
inner.__name__: inner

❯ mypyc nested.py > /dev/null && python -c "import nested"
outer.__name__: outer
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "nested.py", line 10, in <module>
    print(f"inner.__name__: {outer().__name__}")
AttributeError: 'inner_outer_obj' object has no attribute '__name__'

❯ mypy --version ; python --version ; lsb_release -d
mypy 0.910
Python 3.8.5
Description:	Ubuntu 20.04.2 LTS

I suppose this is because nested functions are implemented as an instance of a special class as I've so heard?

edit: I confirmed this also fails using mypyc from master (commit: python/mypy@68a67aedb072dc811424636140a87aa76f1b59bb)

ichard26 avatar Aug 05 '21 20:08 ichard26

This may be related to #718.

JukkaL avatar Aug 06 '21 10:08 JukkaL

Are you sure? I can still reproduce the issuelocally with current master of python/mypy@76406642694b0900a54d79e2800b2c364101d47c.

ichard26 avatar Nov 05 '21 03:11 ichard26

@ichard26 Sorry I made a mistake when testing it.

97littleleaf11 avatar Nov 05 '21 03:11 97littleleaf11