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 crashes when importing types_aiobotocore_s3 #16498

Closed
Apakottur opened this issue Nov 15, 2023 · 6 comments · Fixed by #16510
Closed

Mypy crashes when importing types_aiobotocore_s3 #16498

Apakottur opened this issue Nov 15, 2023 · 6 comments · Fixed by #16510
Labels
crash topic-disallow-any The disallow-any-* family of flags topic-typed-dict

Comments

@Apakottur
Copy link

Crash report

Mypy 1.7.0 crashes when importing the most recent version of types_aiobotocore_s3 (2.7.0).

To reproduce:

I have the following file:

# test.py
import types_aiobotocore_s3

I'm getting a Mypy crash when running the following command:

mypy --show-traceback --disallow-any-unimported test.py

Getting same crash on mypy master branch.

Traceback

/opt/virtualenvs/cmd-N7MVwD3X-py3.11/lib/python3.11/site-packages/types_aiobotocore_s3/type_defs.pyi:565: 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.7.0
Traceback (most recent call last):
  File "mypy/semanal.py", line 6525, in accept
  File "mypy/nodes.py", line 1308, in accept
  File "mypy/semanal.py", line 2855, in visit_assignment_stmt
  File "mypy/semanal.py", line 3152, in analyze_typeddict_assign
  File "mypy/semanal_typeddict.py", line 359, in check_typeddict
  File "mypy/semanal_typeddict.py", line 460, in parse_typeddict_args
  File "mypy/messages.py", line 1745, in unimported_type_becomes_any
  File "mypy/messages.py", line 2714, in format_type
  File "mypy/messages.py", line 2731, in format_type_bare
  File "mypy/messages.py", line 2486, in format_type_inner
  File "mypy/types.py", line 3074, in get_proper_type
TypeError: mypy.types.ProperType object expected; got mypy.types.RequiredType
/opt/virtualenvs/cmd-N7MVwD3X-py3.11/lib/python3.11/site-packages/types_aiobotocore_s3/type_defs.pyi:565: : note: use --pdb to drop into pdb

Your Environment

  • Mypy version used: 1.7.0
  • Mypy command-line flags: --show-traceback --disallow-any-unimported
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11.6
  • Operating system and version: Docker image python:3.11.6-slim-bullseye
@AlexWaygood
Copy link
Member

Hmm, I can't reproduce locally on py311 or py312

@miikka
Copy link

miikka commented Nov 15, 2023

I'm running into same issue with Python 3.11.6 and mypy 1.4.1 in a real project, but I was able to reproduce the issue using the instructions above with Python 3.12.0 and mypy 1.7.0. Note that --disallow-any-unimported must be enabled; it works if it's disabled.

@AlexWaygood AlexWaygood added the topic-disallow-any The disallow-any-* family of flags label Nov 15, 2023
@JelleZijlstra
Copy link
Member

Reproduces for me in a clean venv.

I reduced it to a single file:

from typing import NotRequired, TypedDict
from thismoduledoesntexist import TransferConfig

B = TypedDict(
    "B",
    {
        "Config": NotRequired[TransferConfig],
    },
)
% mypy --show-traceback --disallow-any-unimported /tmp/type_defs.pyi
/tmp/type_defs.pyi:2: error: Cannot find implementation or library stub for module named "thismoduledoesntexist"  [import-not-found]
/tmp/type_defs.pyi:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
/tmp/type_defs.pyi:4: 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.7.0
Traceback (most recent call last):
  File "mypy/semanal.py", line 6525, in accept
  File "mypy/nodes.py", line 1308, in accept
  File "mypy/semanal.py", line 2855, in visit_assignment_stmt
  File "mypy/semanal.py", line 3152, in analyze_typeddict_assign
  File "mypy/semanal_typeddict.py", line 359, in check_typeddict
  File "mypy/semanal_typeddict.py", line 460, in parse_typeddict_args
  File "mypy/messages.py", line 1745, in unimported_type_becomes_any
  File "mypy/messages.py", line 2714, in format_type
  File "mypy/messages.py", line 2731, in format_type_bare
  File "mypy/messages.py", line 2486, in format_type_inner
  File "mypy/types.py", line 3074, in get_proper_type
TypeError: mypy.types.ProperType object expected; got mypy.types.RequiredType
/tmp/type_defs.pyi:4: : note: use --pdb to drop into pdb

The problem seems to be a NotRequired containing an unimported type inside an old-stype TypedDict (class-style TypedDicts are fine).

@JelleZijlstra
Copy link
Member

Also reproduces as far back as mypy 1.4.1 (as @miikka wrote). I haven't tried to see how far back it reproduces, but it's not a recent regression.

@AlexWaygood
Copy link
Member

AlexWaygood commented Nov 15, 2023

Oh, I can reproduce with compiled mypy, but not uncompiled mypy (I was using uncompiled mypy). Because it's one of those runtime checks inserted by mypyc that's failing. Fun.

@AlexWaygood
Copy link
Member

FWIW, here's how to get it to repro with uncompiled mypy: apply this diff to master:

diff --git a/mypy/types.py b/mypy/types.py
index e7738bd7d..3eabce15d 100644
--- a/mypy/types.py
+++ b/mypy/types.py
@@ -3076,8 +3076,9 @@ def get_proper_type(typ: Type | None) -> ProperType | None:
         typ = typ.type_guard
     while isinstance(typ, TypeAliasType):
         typ = typ._expand_once()
+    assert isinstance(typ, ProperType), type(typ)
     # TODO: store the name of original type alias on this type, so we can show it in errors.
-    return cast(ProperType, typ)
+    return typ

@ilevkivskyi ilevkivskyi changed the title Mypy 1.7.0 crashes when importing types_aiobotocore_s3 Mypy crashes when importing types_aiobotocore_s3 Nov 16, 2023
ilevkivskyi added a commit that referenced this issue Nov 17, 2023
Fixes #16498

Fix is trivial: no operations can be done with `Required` types before
unwrapping (because they are not real types).

---------

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash topic-disallow-any The disallow-any-* family of flags topic-typed-dict
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants