ruff/crates/red_knot_python_semantic/resources/mdtest/shadowing/function.md
Mike Perlov 3b24fe5c07
[red-knot] improve function/bound method type display (#17294)
## Summary

* Partial #17238
* Flyby from discord discussion - `todo_type!` now statically checks for
no parens in the message to avoid issues between debug & release build
tests

## Test Plan

many mdtests are changing
2025-04-14 15:56:18 -07:00

948 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: def f() -> Unknown

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

reveal_type(f)  # revealed: def f(x: int) -> int

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

def f(): ...

reveal_type(f)  # revealed: def f() -> Unknown