New semantic analyzer: Explain that global variable must be declared at module top level
Open
Nobody has claimed this yet.
priority-1-normal
semantic-analyzer
topic-usability
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
The error message is poor for code like this where we don't initialize or declare a global variable at the module top level:
from typing import List
def foo() -> None:
global bar
bar = [] # type: List[str] # Name 'bar' already defined (possibly by an import)
bar # Name 'bar' is not defined
It would be better to be explicit and say something like "Global variable must be declared or initialized at module top level".
Follow-up to #6975.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the semantic analyzer handling global declarations and reproduce the Python snippet from the issue. Update the diagnostic so this case explains that the global variable must be declared or initialized at module top level, then verify that the reported message matches the requested wording.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100