Skip to content

Variable used before definition #3598

Description

@azdanov

Environment data

  • VS Code version: 1.29.1 (1.29.1)
  • Extension version (available under the Extensions sidebar): 2018.11.0
  • OS and version: macOS Mojave 10.14.1 (18B75)
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.7.1 64-bit
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): venv
  • Relevant/affected Python packages and their versions: -

Expected behaviour

When running provided code snippet there should be no warning.

from typing import Dict

fallbackName: Dict[str, str] = {"firstName": "First", "lastName": "Last"}

NAME = " ".join(str(x) for x in fallbackName.values())

print(f"Hi, {NAME}!")

Actual behaviour

A warning is generated: 'fallbackName' used before definition

{
	"resource": "/Users/_____/Projects/python/sandbox/index.py",
	"owner": "_generated_diagnostic_collection_name_#0",
	"code": "use-before-def",
	"severity": 4,
	"message": "'fallbackName' used before definition",
	"source": "Python",
	"startLineNumber": 5,
	"startColumn": 33,
	"endLineNumber": 5,
	"endColumn": 45
}

Additional Info

This does not happen when type hinting is removed:

FALLBACK_NAME = {"first_name": "First", "last_name": "Last"}

NAME = " ".join(str(x) for x in FALLBACK_NAME.values())

print(f"Hi, {NAME}!")

Steps to reproduce:

  1. Create a new project, python3 -m venv new/venv.
  2. Insert code into new/index.py.
  3. Lint.

Logs

Output for Python in the Output panel (View→Output, change the drop-down the upper-right of the Output panel to Python)

Starting Microsoft Python language server.
Microsoft Python Language Server version 0.1.66.0
Initializing for /Users/_____/Projects/python/sandbox/venv/bin/python
##########Linting Output - prospector##########
{
  "summary": {
    "started": "2018-12-08 12:13:01.666245",
    "libraries": [],
    "strictness": "from profile",
    "profiles": ".prospector.yaml, full_pep8, doc_warnings, strictness_veryhigh, no_member_warnings",
    "tools": [
      "dodgy",
      "mccabe",
      "mypy",
      "pep257",
      "pep8",
      "profile-validator",
      "pyflakes",
      "pylint"
    ],
    "message_count": 0,
    "completed": "2018-12-08 12:13:02.171521",
    "time_taken": "0.51",
    "formatter": "json"
  },
  "messages": []
}

Output from Console under the Developer Tools panel (toggle Developer Tools on under Help)

 INFO no standard startup: not a new window
console.ts:134 [Extension Host] Python Extension: Display locator refreshing progress, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Notify locators are locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Detected refreshing of Interpreters, Arg 1: {}, Return Value: 
console.ts:134 [Extension Host] Python Extension: Display locator refreshing progress, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Notify locators are locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Detected refreshing of Interpreters, Arg 1: {}, Return Value: 
console.ts:134 [Extension Host] Python Extension: Display locator refreshing progress, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Notify locators are locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Detected refreshing of Interpreters, Arg 1: {}, Return Value: 
console.ts:134 [Extension Host] Python Extension: Display locator refreshing progress, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Notify locators are locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Detected refreshing of Interpreters, Arg 1: {}, Return Value: 
console.ts:134 [Extension Host] Python Extension: Create file systemwatcher with pattern */python
console.ts:134 [Extension Host] Python Extension: Create file systemwatcher with pattern */*/python
console.ts:134 [Extension Host] Python Extension: Display locator refreshing progress, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Notify locators are locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Detected refreshing of Interpreters, Arg 1: {}, Return Value: 
console.ts:134 [Extension Host] Python Extension: Display locator refreshing progress, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Notify locators are locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Detected refreshing of Interpreters, Arg 1: {}, Return Value: 
console.ts:134 [Extension Host] Python Extension: Display locator refreshing progress, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Notify locators are locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Detected refreshing of Interpreters, Arg 1: {}, Return Value: 
console.ts:134 [Extension Host] Python Extension: Register Intepreter Watcher, Arg 1: {"$mid":1,"fsPath":"/Users/_____/Projects/python/sandbox","external":"file:///Users/_____/Projects/python/sandbox","path":"/Users/_____/Projects/python/sandbox","scheme":"file"}, Return Value: 
console.ts:134 [Extension Host] Python Extension: Create file systemwatcher with pattern */python
console.ts:134 [Extension Host] Python Extension: Create file systemwatcher with pattern */*/python
console.ts:134 [Extension Host] Python Extension: Display locator refreshing progress, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Notify locators are locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Detected refreshing of Interpreters, Arg 1: {}, Return Value: 
console.ts:134 [Extension Host] Python Extension: Build the workspace interpreter watcher, Arg 1: {"$mid":1,"fsPath":"/Users/_____/Projects/python/sandbox","external":"file:///Users/_____/Projects/python/sandbox","path":"/Users/_____/Projects/python/sandbox","scheme":"file"}, Return Value: UNABLE TO DETERMINE VALUE
console.ts:134 [Extension Host] Python Extension: Register Intepreter Watcher, Arg 1: undefined, Return Value: 
console.ts:134 [Extension Host] Python Extension: Build the workspace interpreter watcher, Arg 1: undefined, Return Value: UNABLE TO DETERMINE VALUE
console.ts:134 [Extension Host] Python Extension: Get language server folder name, , Return Value: "languageServer.0.1.66"
6console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Hide locator refreshing progress, , Return Value: 
console.ts:134 [Extension Host] Python Extension: All locators have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Hide locator refreshing progress, , Return Value: 
console.ts:134 [Extension Host] Python Extension: All locators have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Hide locator refreshing progress, , Return Value: 
console.ts:134 [Extension Host] Python Extension: All locators have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Hide locator refreshing progress, , Return Value: 
console.ts:134 [Extension Host] Python Extension: All locators have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Hide locator refreshing progress, , Return Value: 
console.ts:134 [Extension Host] Python Extension: All locators have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Hide locator refreshing progress, , Return Value: 
console.ts:134 [Extension Host] Python Extension: All locators have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Hide locator refreshing progress, , Return Value: 
console.ts:134 [Extension Host] Python Extension: All locators have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Hide locator refreshing progress, , Return Value: 
console.ts:134 [Extension Host] Python Extension: All locators have completed locating, , Return Value: 
console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, , Return Value: 

Activity

  1. yehorb commented on Dec 8, 2018

    @yehorb

    I have a similar issue with my Python.
    VS Code version: 1.29.1 (1.29.1)
    Extension version (available under the Extensions sidebar): 2018.11.0
    OS and version: Ubuntu 18.04.1 LTS
    Python version (& distribution if applicable, e.g. Anaconda): Python 3.6.7 64-bit
    Type of virtual environment used (N/A | venv | virtualenv | conda | ...): conda
    Relevant/affected Python packages and their versions: not relevant

    Issues disappear if you reload window, but reappear if I make even the slightest edit to the code written. Sometimes they appear as I type (e.g. I type "return True" and issues are " 're' used before definition", " 'ret' used before definition" and so on).

    Issue appears to occur more in longer files.

    screenshot from 2018-12-09 00-47-41
    screenshot from 2018-12-09 00-47-53

  2. azdanov commented on Dec 9, 2018

    @azdanov
    Author

    Yehor Borkov (@yehorb) Could you create a minimal Github repo with a reproducible error? It will help a lot in fixing this issue.

    In my case, this happens because of typing.

  3. yehorb commented on Dec 10, 2018

    @yehorb
  4. azdanov commented on Dec 10, 2018

    @azdanov
    Author

    Yehor Borkov (@yehorb) Awesome, thanks! Now it's just waiting for a maintainer to triage this issue. 🤷‍♂️

  5. mcpower commented on Feb 7, 2019

    @mcpower

    I'm encountering a very similar problem with class variable type annotations.


    class TestClass:
        instance_var: str
    
        def __init__(self) -> None:
            self.instance_var = ""

    produces

    {
    	"owner": "_generated_diagnostic_collection_name_#1",
    	"code": "use-before-def",
    	"severity": 4,
    	"message": "'instance_var' used before definition",
    	"source": "Python",
    	"startLineNumber": 2,
    	"startColumn": 5,
    	"endLineNumber": 2,
    	"endColumn": 17
    }
    

    By adding a typed class variable, the error disappears (no problems are detected):

    from typing import ClassVar
    
    class TestClass:
        instance_var: str
        class_var: ClassVar[int] = 0
    
        def __init__(self) -> None:
            self.instance_var = ""

    Omitting the type annotation on the class variable will cause the previous error on the instance variable to show up again:

    class TestClass:
        instance_var: str
        class_var = 0
    
        def __init__(self) -> None:
            self.instance_var = ""

    produces

    {
    	"owner": "_generated_diagnostic_collection_name_#1",
    	"code": "use-before-def",
    	"severity": 4,
    	"message": "'instance_var' used before definition",
    	"source": "Python",
    	"startLineNumber": 2,
    	"startColumn": 5,
    	"endLineNumber": 2,
    	"endColumn": 17
    }
    

    • VS Code version: 1.31.0
    • Extension version (available under the Extensions sidebar): 2019.1.0
    • OS and version: Windows 10 1803
    • Python version (& distribution if applicable, e.g. Anaconda): CPython 3.7.0
    • Python Language Server version: 0.1.78
  6. mr-katsini commented on Feb 11, 2019

    @mr-katsini

    Having a similar issue... It's really frustrating.

    screenshot 2019-02-11 at 07 45 00

    Doing some very basic DRF stuff with django rest and these things keep popping up :(

  7. phwh commented on Feb 14, 2019

    @phwh

    I'm also having the same issue but with dataclasses. Every attribute gets unterlined with a use-before-def message.

    screenshot 2019-02-14 at 09 39 36

    screenshot 2019-02-14 at 09 39 47

    screenshot 2019-02-14 at 09 39 53

    Interestingly, assigning a default value makes the Language Server stop complaning:

    screenshot 2019-02-14 at 09 43 27

  8. brettcannon commented on Feb 15, 2019

    @brettcannon
    Member
  9. inthesmall commented on Mar 6, 2019

    @inthesmall

    If an even more minimal example would help:

    test: list = [1, 2]
    
    a = [2*i for i in test]

    screenshot 2019-03-06 at 22 19 56

    screenshot 2019-03-06 at 22 28 14

  10. brettcannon commented on Mar 7, 2019

    @brettcannon
    Member

    @millsyman please report on the upstream issue.

  11. locked and limited conversation to collaborators on Mar 7, 2019
  12. luabud commented on Sep 11, 2019

    @luabud
    Member

    Original issue is fixed upstream (microsoft/python-language-server#475)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-intellisenseLSP-related functionality: auto-complete, docstrings, navigation, refactoring, etc.bugIssue identified by VS Code Team member as probable bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions