Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6212,7 +6212,13 @@ def accept(
column=node_as_type.column,
is_type_form=True,
)
if is_subtype(typ1, p_type_context):
# Type variables in the TypeForm branch can prevent the subtype
# check from recognizing a type expression. Erase them only for
# this check; normal argument checking and inference still use
# the original context.
if is_subtype(typ1, p_type_context) or is_subtype(
typ1, erasetype.erase_typevars(p_type_context)
):
typ = typ1 # r-value type, when interpreted as a type expression
else:
typ2 = node.accept(self)
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/check-typeform.test
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,15 @@ typx2: TypeForm = no_such_module.NoSuchType # E: Name "no_such_module" is not d

-- Type Expression Context: Union[TypeForm, <non-TypeForm>]

[case testCanPassTypeExpressionToTypeFormInUnionParameter]
from typing import TypeVar
from typing_extensions import Annotated, TypeForm
T = TypeVar('T')
def f(typx: TypeForm[T] | None) -> T: ...
reveal_type(f(Annotated[int, 'meta'])) # N: Revealed type is "builtins.int"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]

[case testAcceptsTypeFormLiteralAssignedToUnionOfTypeFormAndNonStr]
from typing_extensions import TypeForm
typx_or_int1: TypeForm[int | None] | int = int | None # No error; interpret as TypeForm
Expand Down
Loading