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

1.11 crash with TypeDict+(Not)Required and type: ignore #17608

Closed
nernst opened this issue Jul 30, 2024 · 1 comment · Fixed by #17640
Closed

1.11 crash with TypeDict+(Not)Required and type: ignore #17608

nernst opened this issue Jul 30, 2024 · 1 comment · Fixed by #17640

Comments

@nernst
Copy link

nernst commented Jul 30, 2024

Crash Report

I upgraded to mypy 1.11 from 1.08, and previously accepted code started causing crashes.

Traceback

$ mypy mypy-repro.py --show-traceback
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

To Reproduce

mypy-repro.py:

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

class Data(TypedDict, total=False):
    # NOTE: not complete
    _id: Required[ObjectId]
    appId: str

(Write what you did to reproduce the crash. Full source code is
appreciated. We also very much appreciate it if you try to narrow the
source down to a small stand-alone example.)

Your Environment

In this repro, I'm using pymongo ^3.13,<4 because I cannot currently upgrade any further due to server dependencies, so bson is untyped.

  • Mypy version used: 1.11.0
  • Mypy command-line flags: mpypy mypy-repro.py
  • Mypy configuration options from pyproject.toml:
...
[tool.mypy]
python_version = "3.12"

# set mypy_path and explicit_package_bases, otherwise mypy gets confused about package names
# due to the use of namespace packages.
mypy_path = "src"
explicit_package_bases = true
enable_incomplete_feature = ["NewGenericSyntax"]

pretty = true
disallow_any_unimported = true
disallow_any_expr = false
disallow_any_explicit = false
disallow_any_generics = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
disallow_untyped_decorators = true
implicit_optional = true
strict_optional = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_no_return = true
warn_return_any = true
warn_unreachable = true
warn_unused_configs = true
exclude = ["(dist|docs)/.*/.*\\.py$"]
  • Python version used: Python 3.12.2
  • Operating system and version: Debian GNU/Linux 12 (bookworm)
@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

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#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
3 participants