Skip to content
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

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

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 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
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
59 changes: 59 additions & 0 deletions docs/source/error_code_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,65 @@ type must be a subtype of the original type::
def g(x: object) -> TypeIs[str]: # OK
...

.. _code-maybe-unrecognized-str-typeform:

String appears in a context which expects a TypeForm [maybe-unrecognized-str-typeform]
--------------------------------------------------------------------------------------

TypeForm literals may contain string annotations:

.. code-block:: python

typx1: TypeForm = str | None
typx2: TypeForm = 'str | None' # OK
typx3: TypeForm = 'str' | None # OK

However TypeForm literals containing a string annotation can only be recognized
by mypy in the following locations:

.. code-block:: python

typx_var: TypeForm = 'str | None' # assignment r-value

def func(typx_param: TypeForm) -> TypeForm:
return 'str | None' # returned expression

func('str | None') # callable's argument

If you try to use a string annotation in some other location
which expects a TypeForm, the string value will always be treated as a ``str``
even if a ``TypeForm`` would be more appropriate and this note code
will be generated:

.. code-block:: python

# Note: TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize. [maybe-unrecognized-str-typeform]
# Error: List item 0 has incompatible type "str"; expected "TypeForm[Any]" [list-item]
list_of_typx: list[TypeForm] = ['str | None', float]

Fix the note by surrounding the entire type with ``TypeForm(...)``:

.. code-block:: python

list_of_typx: list[TypeForm] = [TypeForm('str | None'), float] # OK

Similarly, if you try to use a string literal in a location which expects a
TypeForm, this note code will be generated:

.. code-block:: python

dict_of_typx = {'str_or_none': TypeForm(str | None)}
# Note: TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize. [maybe-unrecognized-str-typeform]
list_of_typx: list[TypeForm] = [dict_of_typx['str_or_none']]

Fix the note by adding ``# type: ignore[maybe-unrecognized-str-typeform]``
to the line with the string literal:

.. code-block:: python

dict_of_typx = {'str_or_none': TypeForm(str | None)}
list_of_typx: list[TypeForm] = [dict_of_typx['str_or_none']] # type: ignore[maybe-unrecognized-str-typeform]

.. _code-misc:

Miscellaneous checks [misc]
Expand Down
114 changes: 112 additions & 2 deletions 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,11 @@ 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 +2632,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 @@ -7786,7 +7803,9 @@ def add_any_attribute_to_type(self, typ: Type, name: str) -> Type:
fallback = typ.fallback.copy_with_extra_attr(name, any_type)
return typ.copy_modified(fallback=fallback)
if isinstance(typ, TypeType) and isinstance(typ.item, Instance):
return TypeType.make_normalized(self.add_any_attribute_to_type(typ.item, name))
return TypeType.make_normalized(
self.add_any_attribute_to_type(typ.item, name), is_type_form=typ.is_type_form
)
if isinstance(typ, TypeVarType):
return typ.copy_modified(
upper_bound=self.add_any_attribute_to_type(typ.upper_bound, name),
Expand Down Expand Up @@ -7921,6 +7940,97 @@ 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
Loading