ruff/crates/red_knot_python_semantic/resources/mdtest/shadowing/function.md
InSync 11cfe2ea8a
[red-knot] Enforce specifying paths for mdtest code blocks in a separate preceding line (#15890)
## Summary

Resolves #15695, rework of #15704.

This change modifies the Mdtests framework so that:

* Paths must now be specified in a separate preceding line:

	`````markdown
	`a.py`:

	```py
	x = 1
	```
	`````

If the path of a file conflicts with its `lang`, an error will be
thrown.

* Configs are no longer accepted. The pattern still take them into
account, however, to avoid "Unterminated code block" errors.
* Unnamed files are now assigned unique, `lang`-respecting paths
automatically.

Additionally, all legacy usages have been updated.

## Test Plan

Unit tests and Markdown tests.

---------

Co-authored-by: Carl Meyer <carl@astral.sh>
2025-02-04 08:27:17 +01:00

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

a.py:

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

Implicit error

a.py:

def f(): ...

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

Explicit shadowing

a.py:

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]