ruff/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_34.py
Brent Westbrook fe516e24f5
[pyflakes] Visit forward annotations in TypeAliasType as types (F401) (#15829)
## Summary

Fixes https://github.com/astral-sh/ruff/issues/15812 by visiting the
second argument as a type definition.

## Test Plan

New F401 tests based on the report.

---------

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2025-01-30 18:06:38 -05:00

67 lines
1.5 KiB
Python

"""Regression tests for https://github.com/astral-sh/ruff/issues/15812"""
def f():
from typing import Union
from typing_extensions import TypeAliasType
Json = TypeAliasType(
"Json",
"Union[dict[str, Json], list[Json], str, int, float, bool, None]",
)
def f():
from typing import Union
from typing_extensions import TypeAliasType, TypeVar
T = TypeVar("T")
V = TypeVar("V")
Json = TypeAliasType(
"Json",
"Union[dict[str, Json], list[Json], str, int, float, bool, T, V, None]",
type_params=(T, V),
)
def f():
from typing import Union
from typing_extensions import TypeAliasType
Json = TypeAliasType(
value="Union[dict[str, Json], list[Json], str, int, float, bool, None]",
name="Json",
)
# strictly speaking it's a false positive to emit F401 for both of these, but
# we can't really be expected to understand that the strings here are type
# expressions (and type checkers probably wouldn't understand them as type
# expressions either!)
def f():
from typing import Union
from typing_extensions import TypeAliasType
args = [
"Json",
"Union[dict[str, Json], list[Json], str, int, float, bool, None]",
]
Json = TypeAliasType(*args)
def f():
from typing import Union
from typing_extensions import TypeAliasType
kwargs = {
"name": "Json",
"value": "Union[dict[str, Json], list[Json], str, int, float, bool, None]",
}
Json = TypeAliasType(**kwargs)