The locals() function could be evaluated in the wrong context, e.g. in dict comprehension
See original GitHub issue0.6.8.4 (pip version) Python: 3.8.2 (default, Apr 27 2020, 15:53:34) Executable: /home/jack/oggreader/venv/bin/python OS: Linux Arch: x86_64
returning locals() from comprehension returns empty dict without warnings
from typing import Dict, Any
def foo() -> Dict[str,Any]:
bar: str = "asd"
return {x:y for x,y in locals().items()}
print(foo())
prints {}
copying the locals() to var solves the problem
from typing import Dict, Any
def foo() -> Dict[str,Any]:
bar: str = "asd"
copy = locals()
return {x:y for x,y in copy.items()}
print(foo())
prints {‘bar’: ‘asd’}
without comprehension everything works as expected
from typing import Dict, Any
def foo() -> Dict[str,Any]:
bar: str = "asd"
return locals()
print(foo())
prints {‘bar’: ‘asd’}
Issue Analytics
- State:
- Created 3 years ago
- Comments:15 (14 by maintainers)
Top Results From Across the Web
Python 3 Dictionary Comprehension Exec Error - Stack Overflow
At module scope (case1), locals is globals(). In function scope (case2), they are two different objects. "If exec gets two separate objects ...
Read more >Python eval(): Evaluate Expressions Dynamically - Real Python
This function can be handy when you're trying to dynamically evaluate Python expressions from any input that comes as a string or a...
Read more >List, Set, Dictionary Comprehensions in Python - Medium
The result will be a new list resulting from evaluating the expression in the context of the for and if clauses that follow...
Read more >6. Expressions — Python 3.11.1 documentation
A comprehension in an async def function may consist of either a for or async for clause following the leading expression, may contain...
Read more >starlark/spec.md at master - GitHub
A dictionary can also be constructed using a dictionary comprehension, ... It is a dynamic error to evaluate a reference to a local...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I made a pre-release with this contained. This has drawn more and more circles, esp. as the code turned out to now highlight weaknesses in the loop tracing, which had to be ironed out. However, all good now.
This is the last release 0.6.9 already for a while.