question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The locals() function could be evaluated in the wrong context, e.g. in dict comprehension

See original GitHub issue

0.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:closed
  • Created 3 years ago
  • Comments:15 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
kayhayencommented, Aug 25, 2020

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.

0reactions
kayhayencommented, Oct 2, 2020

This is the last release 0.6.9 already for a while.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found