8000 [PEP 747] Recognize TypeForm[T] type and values (#9773) by davidfstr · Pull Request #18690 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

[PEP 747] Recognize TypeForm[T] type and values (#9773) #18690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ca4c79f
[PEP 747] Recognize TypeForm[T] type and values (#9773)
davidfstr Sep 29, 2024
9dcfc23
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 16, 2025
5cb1da5
Eliminate use of Type Parameter Syntax, which only works on Python >=…
davidfstr Feb 19, 2025
3ece208
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 19, 2025
799bc48
Remove INPUTS directory
davidfstr Feb 19, 2025
6bda848
WIP: Don't declare attributes on Expression to avoid confusing mypyc
davidfstr Feb 20, 2025
9df0e6b
SQ -> WIP: Don't declare attributes on Expression to avoid confusing …
davidfstr Feb 21, 2025
a2c318b
SQ -> WIP: Don't declare attributes on Expression to avoid confusing …
davidfstr Feb 21, 2025
f398374
Recognize non-string type expressions everywhere lazily. Optimize eag…
davidfstr Feb 25, 2025
bd5911f
NOMERGE: Disable test that is already failing on master branch
davidfstr Mar 4, 2025
3801bcc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 4, 2025
c4db784
Fix mypyc errors: Replace EllipsisType with NotParsed type
davidfstr Mar 4, 2025
29fe65a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 4, 2025
4134250
mypy_primer: Enable TypeForm feature when checking effects on open so…
davidfstr Mar 5, 2025
5fb5bd8
Revert "mypy_primer: Enable TypeForm feature when checking effects on…
davidfstr Mar 8, 2025
54cd64d
NOMERGE: mypy_primer: Enable --enable-incomplete-feature=TypeForm whe…
davidfstr Mar 8, 2025
075980b
Ignore warnings like: <type_comment>:1: SyntaxWarning: invalid escape…
davidfstr Mar 14, 2025
6797cac
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 14, 2025
4162a35
Rerun CI
davidfstr Mar 17, 2025
d1aafcd
Improve warning message when string annotation used in TypeForm context
davidfstr Mar 18, 2025
1d9620d
Print error code for the MAYBE_UNRECOGNIZED_STR_TYPEFORM note
davidfstr Mar 26, 2025
76cae61
Document the [maybe-unrecognized-str-typeform] error code
davidfstr Mar 26, 2025
170a5e7
Fix doc generation warning
davidfstr Mar 26, 2025
84c06a6
Merge branch 'master' into f/typeform3
davidfstr Apr 28, 2025
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
Prev Previous commit
Next Next commit
Recognize non-string type expressions everywhere lazily. Optimize eag…
…er parsing to bail earlier.

Also:
* No longer add "as_type" attribute to NameExpr and MemberExpr.
  Retain that attribute on StrExpr, IndexExpr, and OpExpr.
* Recognize dotted type expressions like "typing.List".
  • Loading branch information
davidfstr committed Mar 4, 2025
commit f3983744fda9c4a33f577f2d8f7583efa1c5b2fa
107 changes: 106 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
from mypy.scope import Scope
from mypy.semanal import is_trivial_body, refers_to_fullname, set_callable_name
from mypy.semanal_enum import ENUM_BASES, ENUM_SPECIAL_PROPS
from mypy.semanal_shared import SemanticAnalyzerCoreInterface
from mypy.sharedparse import BINARY_MAGIC_METHODS
from mypy.state import state
from mypy.subtypes import (
Expand Down Expand Up @@ -307,6 +308,8 @@ class TypeChecker(NodeVisitor[None], CheckerPluginInterface):

tscope: Scope
scope: CheckerScope
# Innermost enclosing type
type: TypeInfo | None
# Stack of function return types
return_types: list[Type]
# Flags; true for dynamically typed functions
Expand Down Expand Up @@ -378,6 +381,7 @@ def __init__(
self.scope = CheckerScope(tree)
self.binder = ConditionalTypeBinder()
self.globals = tree.names
self.type = None
self.return_types = []
self.dynamic_funcs = []
self.partial_types = []
Expand Down Expand Up @@ -2556,7 +2560,9 @@ def visit_class_def(self, defn: ClassDef) -> None:
for base in typ.mro[1:]:
if base.is_final:
self.fail(message_registry.CANNOT_INHERIT_FROM_FINAL.format(base.name), defn)
with self.tscope.class_scope(defn.info), self.enter_partial_types(is_class=True):
with self.tscope.class_scope(defn.info), \
self.enter_partial_types(is_class=True), \
self.enter_class(defn.info):
old_binder = self.binder
self.binder = ConditionalTypeBinder()
with self.binder.top_frame_context():
Expand Down Expand Up @@ -2624,6 +2630,15 @@ def visit_class_def(self, defn: ClassDef) -> None:
self.check_enum(defn)
infer_class_variances(defn.info)

@contextmanager
def enter_class(self, type: TypeInfo) -> Iterator[None]:
original_type = self.type
self.type = type
try:
yield
finally:
self.type = original_type

def check_final_deletable(self, typ: TypeInfo) -> None:
# These checks are only for mypyc. Only perform some checks that are easier
# to implement here than in mypyc.
Expand Down Expand Up @@ -7923,6 +7938,96 @@ def visit_global_decl(self, o: GlobalDecl, /) -> None:
return None


class TypeCheckerAsSemanticAnalyzer(SemanticAnalyzerCoreInterface):
"""
Adapts TypeChecker to the SemanticAnalyzerCoreInterface,
allowing most type expressions to be parsed during the TypeChecker pass.

See ExpressionChecker.try_parse_as_type_expression() to understand how this
class is used.
"""
_chk: TypeChecker
_names: dict[str, SymbolTableNode]
did_fail: bool

def __init__(self, chk: TypeChecker, names: dict[str, SymbolTableNode]) -> None:
self._chk = chk
self._names = names
self.did_fail = False

def lookup_qualified(
self, name: str, ctx: Context, suppress_errors: bool = False
) -> SymbolTableNode | None:
sym = self._names.get(name)
# All names being looked up should have been previously gathered,
# even if the related SymbolTableNode does not refer to a valid SymbolNode
assert sym is not None, name
return sym

def lookup_fully_qualified(self, fullname: str, /) -> SymbolTableNode:
ret = self.lookup_fully_qualified_or_none(fullname)
assert ret is not None, fullname
return ret

def lookup_fully_qualified_or_none(self, fullname: str, /) -> SymbolTableNode | None:
try:
return self._chk.lookup_qualified(fullname)
except KeyError:
return None

def fail(
self,
msg: str,
ctx: Context,
serious: bool = False,
*,
blocker: bool = False,
code: ErrorCode | None = None,
) -> None:
self.did_fail = True

def note(self, msg: str, ctx: Context, *, code: ErrorCode | None = None) -> None:
pass

def incomplete_feature_enabled(self, feature: str, ctx: Context) -> bool:
if feature not in self._chk.options.enable_incomplete_feature:
self.fail('__ignored__', ctx)
return False
return True

def record_incomplete_ref(self) -> None:
pass

def defer(self, debug_context: Context | None = None, force_progress: bool = False) -> None:
pass

def is_incomplete_namespace(self, fullname: str) -> bool:
return False

@property
def final_iteration(self) -> bool:
return True

def is_future_flag_set(self, flag: str) -> bool:
return self._chk.tree.is_future_flag_set(flag)

@property
def is_stub_file(self) -> bool:
return self._chk.tree.is_stub

def is_func_scope(self) -> bool:
# Return arbitrary value.
#
# This method is currently only used to decide whether to pair
# a fail() message with a note() message or not. Both of those
# message types are ignored.
return False

@property
def type(self) -> TypeInfo | None:
return self._chk.type


class CollectArgTypeVarTypes(TypeTraverserVisitor):
"""Collects the non-nested argument types in a set."""

Expand Down
85 changes: 79 additions & 6 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
freshen_all_functions_type_vars,
freshen_function_type_vars,
)
from mypy.exprtotype import expr_to_unanalyzed_type, TypeTranslationError
from mypy.infer import ArgumentInferContext, infer_function_type_arguments, infer_type_arguments
from mypy.literals import literal
from mypy.maptype import map_instance_to_supertype
Expand Down Expand Up @@ -66,6 +67,7 @@
FloatExpr,
FuncDef,
GeneratorExpr,
get_member_expr_fullname,
IndexExpr,
IntExpr,
LambdaExpr,
Expand All @@ -91,6 +93,7 @@
StrExpr,
SuperExpr,
SymbolNode,
SymbolTableNode,
TempNode,
TupleExpr,
TypeAlias,
Expand All @@ -102,6 +105,7 @@
TypeVarExpr,
TypeVarTupleExpr,
UnaryExpr,
UNBOUND_IMPORTED,
Var,
YieldExpr,
YieldFromExpr,
Expand All @@ -123,14 +127,16 @@
is_subtype,
non_method_protocol_members,
)
from mypy.traverser import has_await_expression
from mypy.traverser import all_name_and_member_expressions, has_await_expression, has_str_expression
from mypy.tvar_scope import TypeVarLikeScope
from mypy.typeanal import (
check_for_explicit_any,
fix_instance,
has_any_from_unimported_type,
instantiate_type_alias,
make_optional_type,
set_any_tvars,
TypeAnalyser,
validate_instance,
)
from mypy.typeops import (
Expand Down Expand Up @@ -5950,13 +5956,12 @@ def accept(
elif (
isinstance(p_type_context, TypeType)
and p_type_context.is_type_form
and isinstance(node, MaybeTypeExpression)
and node.as_type is not None
and (node_as_type := self.try_parse_as_type_expression(node)) is not None
):
typ = TypeType.make_normalized(
node.as_type,
line=node.as_type.line,
column=node.as_type.column,
node_as_type,
line=node_as_type.line,
column=node_as_type.column,
is_type_form=True,
)
else:
Expand Down Expand Up @@ -6313,6 +6318,74 @@ def has_abstract_type(self, caller_type: ProperType, callee_type: ProperType) ->
and not self.chk.allow_abstract_call
)

def try_parse_as_type_expression(self, maybe_type_expr: Expression) -> Type | None:
"""Try to parse a value Expression as a type expression.
If success then return the type that it spells.
If fails then return None.

A value expression that is parsable as a type expression may be used
where a TypeForm is expected to represent the spelled type.

Unlike SemanticAnalyzer.try_parse_as_type_expression()
(used in the earlier SemanticAnalyzer pass), this function can only
recognize type expressions which contain no string annotations."""
if not isinstance(maybe_type_expr, MaybeTypeExpression):
return None

# Check whether has already been parsed as a type expression
# by SemanticAnalyzer.try_parse_as_type_expression(),
# perhaps containing a string annotation
if (isinstance(maybe_type_expr, (StrExpr, IndexExpr, OpExpr))
and maybe_type_expr.as_type is not Ellipsis):
return maybe_type_expr.as_type

# If is potentially a type expression containing a string annotation,
# don't try to parse it because there isn't enough information
# available to the TypeChecker pass to resolve string annotations
if has_str_expression(maybe_type_expr):
self.chk.note(
'TypeForm containing a string annotation cannot be recognized here. '
'Try assigning the TypeForm to a variable and use the variable here instead.',
maybe_type_expr,
)
return None

# Collect symbols targeted by NameExprs and MemberExprs,
# to be looked up by TypeAnalyser when binding the
# UnboundTypes corresponding to those expressions.
(name_exprs, member_exprs) = all_name_and_member_expressions(maybe_type_expr)
sym_for_name = {
e.name:
SymbolTableNode(UNBOUND_IMPORTED, e.node)
for e in name_exprs
} | {
F438 e_name:
SymbolTableNode(UNBOUND_IMPORTED, e.node)
for e in member_exprs
if (e_name := get_member_expr_fullname(e)) is not None
}

chk_sem = mypy.checker.TypeCheckerAsSemanticAnalyzer(self.chk, sym_for_name)
tpan = TypeAnalyser(
chk_sem,
TypeVarLikeScope(), # empty scope
self.plugin,
self.chk.options,
self.chk.tree,
self.chk.is_typeshed_stub,
)

try:
typ1 = expr_to_unanalyzed_type(
maybe_type_expr, self.chk.options, self.chk.is_typeshed_stub,
)
typ2 = typ1.accept(tpan)
if chk_sem.did_fail:
return None
return typ2
except TypeTranslationError:
return None


def has_any_type(t: Type, ignore_in_type_obj: bool = False) -> bool:
"""Whether t contains an Any type"""
Expand Down
33 changes: 18 additions & 15 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import os
from abc import abstractmethod
import builtins
from collections import defaultdict
from collections.abc import Iterator, Sequence
from enum import Enum, unique
Expand All @@ -20,6 +21,9 @@
if TYPE_CHECKING:
from mypy.patterns import Pattern

EllipsisType = builtins.ellipsis
else:
EllipsisType = Any

class Context:
"""Base type for objects that are valid as error message locations."""
Expand Down Expand Up @@ -1723,12 +1727,13 @@ class StrExpr(Expression):
value: str # '' by default
# If this value expression can also be parsed as a valid type expression,
# represents the type denoted by the type expression.
as_type: mypy.types.Type | None
# Ellipsis means "not parsed" and None means "is not a type expression".
as_type: EllipsisType | mypy.types.Type | None

def __init__(self, value: str) -> None:
super().__init__()
self.value = value
self.as_type = None
self.as_type = Ellipsis

def accept(self, visitor: ExpressionVisitor[T]) -> T:
return visitor.visit_str_expr(self)
Expand Down Expand Up @@ -1879,20 +1884,15 @@ class NameExpr(RefExpr):
This refers to a local name, global name or a module.
"""

__slots__ = ("name", "is_special_form", "as_type")
__slots__ = ("name", "is_special_form")

__match_args__ = ("name", "node")

# If this value expression can also be parsed as a valid type expression,
# represents the type denoted by the type expression.
as_type: mypy.types.Type | None

def __init__(self, name: str) -> None:
super().__init__()
self.name = name # Name referred to
# Is this a l.h.s. of a special form assignment like typed dict or type variable?
self.is_special_form = False
self.as_type = None

def accept(self, visitor: ExpressionVisitor[T]) -> T:
return visitor.visit_name_expr(self)
Expand Down Expand Up @@ -2045,15 +2045,16 @@ class IndexExpr(Expression):
analyzed: TypeApplication | TypeAliasExpr | None
# If this value expression can also be parsed as a valid type expression,
# represents the type denoted by the type expression.
as_type: mypy.types.Type | None
# Ellipsis means "not parsed" and None means "is not a type expression".
as_type: EllipsisType | mypy.types.Type | None

def __init__(self, base: Expression, index: Expression) -> None:
super().__init__()
self.base = base
self.index = index
self.method_type = None
self.analyzed = None
self.as_type = None
self.as_type = Ellipsis

def accept(self, visitor: ExpressionVisitor[T]) -> T:
return visitor.visit_index_expr(self)
Expand Down Expand Up @@ -2129,7 +2130,8 @@ class OpExpr(Expression):
analyzed: TypeAliasExpr | None
# If this value expression can also be parsed as a valid type expression,
# represents the type denoted by the type expression.
as_type: mypy.types.Type | None
# Ellipsis means "not parsed" and None means "is not a type expression".
as_type: EllipsisType | mypy.types.Type | None

def __init__(
self, op: str, left: Expression, right: Expression, analyzed: TypeAliasExpr | None = None
Expand All @@ -2142,16 +2144,17 @@ def __init__(
self.right_always = False
self.right_unreachable = False
self.analyzed = analyzed
self.as_type = None
self.as_type = Ellipsis

def accept(self, visitor: ExpressionVisitor[T]) -> T:
return visitor.visit_op_expr(self)


# Expression subtypes that could represent the root of a valid type expression.
# Always contains an "as_type" attribute.
# TODO: Make this into a Protocol if mypyc is OK with that.
MaybeTypeExpression = (IndexExpr, NameExpr, OpExpr, StrExpr)
#
# May have an "as_type" attribute to hold the type for a type expression parsed
# during the SemanticAnalyzer pass.
MaybeTypeExpression = (IndexExpr, MemberExpr, NameExpr, OpExpr, StrExpr)


class ComparisonExpr(Expression):
Expand Down
Loading
0