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

Mypy 1.11.0 crashes on typing.NotRequired with --html-report #17604

Closed
sanderr opened this issue Jul 30, 2024 · 3 comments · Fixed by #17640
Closed

Mypy 1.11.0 crashes on typing.NotRequired with --html-report #17604

sanderr opened this issue Jul 30, 2024 · 3 comments · Fixed by #17640

Comments

@sanderr
Copy link

sanderr commented Jul 30, 2024

Crash Report

Mypy crashes on a Python file that makes use of typing.NotRequired when used with the --html-report option.

Traceback

To Reproduce

I took an example from the typing docs on TypedDict:

from typing import NotRequired, TypedDict


class Point2D(TypedDict):
    x: int
    y: int
    label: NotRequired[str]

Running mypy on it with --html-report causes a crash:

(sandbox-312) sander@bedevere:~/documents/projects/python-sandbox$ mypy --html-report mypy --show-traceback reproduce.py
reproduce.py: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 1.11.0
Traceback (most recent call last):
  File "/home/sander/.virtualenvs/sandbox-312/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "mypy/build.py", line 2069, in wrap_context
TypeError: mypy.types.ProperType object expected; got mypy.types.RequiredType
reproduce.py: : note: use --pdb to drop into pdb
Generated HTML report (via XSLT): /home/sander/documents/projects/python-sandbox/mypy/index.html
(sandbox-312) sander@bedevere:~/documents/projects/python-sandbox$ cat mypy/index.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="mypy-html.css">
</head>
<body>
<h1>Mypy Type Check Coverage Summary</h1>
<table class="summary">
<caption>Summary from index</caption>
<thead><tr class="summary">
<th class="summary">File</th>
<th class="summary">Imprecision</th>
<th class="summary">Lines</th>
</tr></thead>
<tfoot><tr class="summary summary-quality-0">
<th class="summary summary-filename">Total</th>
<th class="summary summary-precision">0.00% imprecise</th>
<th class="summary summary-lines">0 LOC</th>
</tr></tfoot>
<tbody></tbody>
</table>
</body>
</html>
(sandbox-312) sander@bedevere:~/documents/projects/python-sandbox$ mypy --version
mypy 1.11.0 (compiled: yes)

When I change the NotRequired[str] to plain str, mypy succeeds. The issue also doesn't occur without the --html-report option.

I failed to reproduce the issue from a source installation, even from a checked out v1.11.0 tag. But I get it consistently with the 1.11.0 version published to PyPi.

Your Environment

  • Mypy version used: 1.11.0 (from PyPi, could not reproduce from source installation, even from checked out v1.11.0 tag)
  • Mypy command-line flags: --html-report mypy --show-traceback reproduce.py
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11 and 3.12
  • Operating system and version: Arch Linux
@sanderr sanderr added the crash label Jul 30, 2024
@nernst
Copy link

nernst commented Jul 30, 2024

I've hit a very similar crash in 1.11 involving Required/NotRequired in a TypedDict as well. In my case, html report is not required:

from typing import NotRequired, Required, TypedDict
from bson.objectid import ObjectId  # type: ignore

class Data(TypedDict, total=False):
    _id: Required[ObjectId]  # or NotRequired, either crashes
    appId: str

I have the type ignore because I don't have stubs for bson. If I remove the import of ObjectId and change the type on _id to be str, the code analyses fine. Traceback looks a little more interesting in my case:

mypy-repro.py:8: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 1.11.0
Traceback (most recent call last):
  File "mypy/checker.py", line 591, in accept
  File "mypy/nodes.py", line 1351, in accept
  File "mypy/checker.py", line 2948, in visit_assignment_stmt
  File "mypy/messages.py", line 1768, in unimported_type_becomes_any
  File "mypy/messages.py", line 2805, in format_type
  File "mypy/messages.py", line 2822, in format_type_bare
  File "mypy/messages.py", line 2540, in format_type_inner
  File "mypy/types.py", line 3111, in get_proper_type
TypeError: mypy.types.ProperType object expected; got mypy.types.RequiredType
mypy-repro.py:8: : note: use --pdb to drop into pdb

@JelleZijlstra
Copy link
Member

@nernst could you report a separate bug and leave this one limited to the html-report issue?

@JelleZijlstra JelleZijlstra changed the title Mypy 1.11.0 crashes on typing.NotRequired Mypy 1.11.0 crashes on typing.NotRequired with --html-report Jul 30, 2024
@andersk
Copy link
Contributor

andersk commented Jul 30, 2024

git bisect says this was introduced by e5b3b56 (Fix daemon crash on invalid type in TypedDict, #17495).

Note that to reproduce without mypyc, you need to manually insert this assertion that would be enforced automatically by mypyc:

--- a/mypy/types.py
+++ b/mypy/types.py
@@ -3108,7 +3108,8 @@ def get_proper_type(typ: Type | None) -> ProperType | None:
     while isinstance(typ, TypeAliasType):
         typ = typ._expand_once()
     # TODO: store the name of original type alias on this type, so we can show it in errors.
-    return cast(ProperType, typ)
+    assert isinstance(typ, ProperType)
+    return typ
 
 
 @overload

(So far this seems similar to @nernst’s #17608.)

andersk added a commit to andersk/mypy that referenced this issue Jul 30, 2024
Fixes python#17604; fixes python#17608.  (To reproduce the crash without mypyc,
replace `cast(ProperType, typ)` with an assertion in
`get_proper_type`.)

Signed-off-by: Anders Kaseorg <[email protected]>
andersk added a commit to andersk/mypy that referenced this issue Jul 30, 2024
Fixes python#17604; fixes python#17608.  (To reproduce the crash without mypyc,
replace `cast(ProperType, typ)` with an assertion in
`get_proper_type`.)

Signed-off-by: Anders Kaseorg <[email protected]>
md384 pushed a commit to md384/mypy that referenced this issue Aug 14, 2024
Fixes python#17604
Fixes python#17608

Fix is trivial, rectify an obvious omission in my original PR.

(cherry picked from commit b56f357)
hauntsaninja pushed a commit that referenced this issue Aug 24, 2024
Fixes #17604
Fixes #17608

Fix is trivial, rectify an obvious omission in my original PR.

(cherry picked from commit b56f357)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants