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
15 changes: 15 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2810,6 +2810,21 @@ def check_arg(
# Only non-abstract non-protocol class can be given where Type[...] is expected...
elif self.has_abstract_type_part(caller_type, callee_type):
self.msg.concrete_only_call(callee_type, context)
# Semantic analysis tried and failed to parse this as a type expression.
elif (
isinstance(context, StrExpr)
and context.as_type is None
and isinstance(caller_type, Instance)
and caller_type.type.fullname == "builtins.str"
and isinstance(callee_type, TypeType)
and callee_type.is_type_form
):
self.chk.fail(
"TypeForm containing a string annotation cannot be recognized here. "
"Surround with TypeForm(...) to recognize.",
context,
code=codes.MAYBE_UNRECOGNIZED_STR_TYPEFORM,
)
elif not is_subtype(caller_type, callee_type, options=self.chk.options):
error = self.msg.incompatible_argument(
n,
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-typeform.test
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ is_type('int | None')
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]

[case testUnrecognizedStringTypeFormArgument]
from typing import TypeVar
from typing_extensions import TypeForm
T = TypeVar("T")
def foo(x: TypeForm[T]) -> None: pass
foo("Unresolvable") # E: TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize.
foo(42) # E: Argument 1 to "foo" has incompatible type "int"; expected "TypeForm[Never]"
def foo_or_str(x: TypeForm[T] | str) -> None: pass
foo_or_str("Unresolvable")
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]


-- Type Expression Location: Return Statement

Expand Down
Loading