py↗VSCODE PYTHON
AN INDEPENDENT GUIDE TO PYTHON IN VS CODESTARTING WITH PYLANCE · VOL. 01

LESS
GUESS.
MORE PYTHON.

Your editor knows more than you think.
Meet the small features that make
a big difference.

↘
++
A LITTLE CLARITY GOES A LONG WAY.
THE MOMENT IT CLICKS
def greet(name: str) -> str:
  return f"Hello, {name}"

greet(42)
!
Expected str. Received int.Catch it here. Before you run it.
ILLUSTRATIVE EXAMPLE / NOT A LIVE LANGUAGE SERVER
THE NOTEBOOK

Five notes on Python & Pylance.
All examples are shown as you read.

01 / Type checking

Catch the mismatch.

A function asks for a string. Your call supplies a number. See the difference before you run it.

ONE FUNCTION. TWO DIFFERENT CALLS.
def greet(name: str) -> str:
    return f"Hello, {name}"
RECEIVED · int
greet(42)

Does not match name: str

EXPECTED · str
greet("Python")

The argument matches.

Type annotations describe what a function expects. With type checking enabled, Pylance can flag an incompatible argument while you edit.

Give the function the value it asks for. Changing the argument to a string makes this call match its declared parameter type.

Type diagnostics depend on python.analysis.typeCheckingMode.

02 / Hover information

Know what’s behind the name.

A short name can carry a lot of context. A hover can bring that context into view.

readings.py / TRACE THE TYPE
readings = [18.2, 20.5, 19.8]
average = sum(readings) / len(readings)
averagefloat

Inferred from division.
No annotation needed here.

Pylance infers many types from expressions. Hover over average to see the type that follows from this calculation, even without an explicit annotation.

The division expression produces a float. The editor carries that information forward wherever the value is used.

Illustrative type information, shown expanded.

03 / IntelliSense

Discover the next move.

A completion list is more than a way to type faster. It shows what your value can do.

message.py / AVAILABLE STRING MEMBERS
message = "hello, python"
message.
upper()→ str
lower()→ str
strip()→ str
AFTER ACCEPTING upper()
message.upper()
# "HELLO, PYTHON"

message is a string, so member suggestions include string methods. Type a dot after the variable to explore the available members.

Accept upper() to complete the expression. The resulting string is uppercase; the original message value stays the same.

Suggestions depend on the value’s type and your environment.

04 / Go to definition

Follow the name home.

A function call is an invitation to ask: where does this come from?

01 / THE CALLmain.py
from readings import mean

result = mean([18.2, 20.5])
02 / THE SOURCEreadings.py
def mean(
    values: list[float],
) -> float:
    return sum(values) / len(values)

Go to Definition follows a symbol to the code that defines it. Start with the call in main.py, then read the function in readings.py.

The definition reveals the parameter and return types. Read what you need, then return to the call with a clearer picture.

F12 is the default Go to Definition shortcut in VS Code.

05 / Rename symbol

A new name. Every reference.

A clearer name should travel with the thing it describes.

ONE SYMBOL / DEFINITION + REFERENCE
BEFOREmean
AFTERaverage
def average(
    values: list[float],
) -> float:
    return sum(values) / len(values)

result = average([18.2, 20.5])

Rename Symbol updates the symbol and its references together. Here, mean becomes average in both the definition and the call.

The name changes; the calculation stays the same. Review the references as part of a real refactoring.

This small example shows the definition and one call.

Keep the curiosity.
Keep writing Python.

Back to the beginning ↑