Skip to content

Variable used before definition #3598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
azdanov opened this issue Dec 8, 2018 · 11 comments
Closed

Variable used before definition #3598

azdanov opened this issue Dec 8, 2018 · 11 comments
Labels
area-intellisense LSP-related functionality: auto-complete, docstrings, navigation, refactoring, etc. bug Issue identified by VS Code Team member as probable bug

Comments

@azdanov
Copy link

azdanov commented Dec 8, 2018

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 (ViewOutput, 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: 
@yehorb
Copy link

yehorb commented Dec 8, 2018

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

@azdanov
Copy link
Author

azdanov commented Dec 9, 2018

@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.

@yehorb
Copy link

yehorb commented Dec 10, 2018

Here:
https://github.com/yehorb/VSCode-use-before-def-bug

This is the output I get:
screenshot from 2018-12-10 12-59-08
screenshot from 2018-12-10 12-59-19
screenshot from 2018-12-10 12-59-32
screenshot from 2018-12-10 12-59-51
screenshot from 2018-12-10 13-00-12
screenshot from 2018-12-10 13-00-32
screenshot from 2018-12-10 13-00-38
screenshot from 2018-12-10 13-00-46

@azdanov
Copy link
Author

azdanov commented Dec 10, 2018

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

@brettcannon brettcannon added upstream-language server bug Issue identified by VS Code Team member as probable bug area-intellisense LSP-related functionality: auto-complete, docstrings, navigation, refactoring, etc. labels Dec 10, 2018
@mcpower
Copy link

mcpower commented Feb 7, 2019

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

@mr-katsini
Copy link

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 :(

@phwh
Copy link

phwh commented Feb 14, 2019

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

@brettcannon
Copy link
Member

@phwh reported in microsoft/python-language-server#399 .

@inthesmall
Copy link

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

@brettcannon
Copy link
Member

@millsyman please report on the upstream issue.

@microsoft microsoft locked and limited conversation to collaborators Mar 7, 2019
@luabud
Copy link
Member

luabud commented Sep 11, 2019

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

@luabud luabud closed this as completed Sep 11, 2019
@ghost ghost removed the needs upstream fix label Sep 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-intellisense LSP-related functionality: auto-complete, docstrings, navigation, refactoring, etc. bug Issue identified by VS Code Team member as probable bug
Projects
None yet
Development

No branches or pull requests

8 participants