mypyc
mypyc copied to clipboard
Compiled nested functions don't retain their __name__ attribute
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)
This may be related to #718.
Are you sure? I can still reproduce the issuelocally with current master of python/mypy@76406642694b0900a54d79e2800b2c364101d47c.
@ichard26 Sorry I made a mistake when testing it.