ruff/crates/red_knot_python_semantic/resources/mdtest/shadowing/function.md
David Peter d296f602e7
[red-knot] Merge Markdown code blocks inside a single section (#15950)
## Summary

Allow for literate style in Markdown tests and merge multiple (unnamed)
code blocks into a single embedded file.

closes #15941

## Test Plan

- Interactively made sure that error-lines were reported correctly in
  multi-snippet sections.
2025-02-05 22:26:15 +01:00

922 B

Function shadowing

Parameter

Parameter x of type str is shadowed and reassigned with a new int value inside the function. No diagnostics should be generated.

def f(x: str):
    x: int = int(x)

Implicit error

def f(): ...

f = 1  # error: "Implicit shadowing of function `f`; annotate to make it explicit if this is intentional"

Explicit shadowing

def f(): ...

f: int = 1

Explicit shadowing involving def statements

Since a def statement is a declaration, one def can shadow another def, or shadow a previous non-def declaration, without error.

f = 1
reveal_type(f)  # revealed: Literal[1]

def f(): ...

reveal_type(f)  # revealed: Literal[f]

def f(x: int) -> int:
    raise NotImplementedError

reveal_type(f)  # revealed: Literal[f]

f: int = 1
reveal_type(f)  # revealed: Literal[1]

def f(): ...

reveal_type(f)  # revealed: Literal[f]