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
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
Document the [maybe-unrecognized-str-typeform] error code
  • Loading branch information
davidfstr committed Mar 26, 2025
commit 76cae61b2d88952af0f6f305593ecbfa5c14de29
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
Loading
0