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

Crashes for created TypedDicts from unpacked dictionaries when creating reports #15891

Closed
md384 opened this issue Aug 16, 2023 · 0 comments · Fixed by #16116
Closed

Crashes for created TypedDicts from unpacked dictionaries when creating reports #15891

md384 opened this issue Aug 16, 2023 · 0 comments · Fixed by #16116

Comments

@md384
Copy link
Contributor

md384 commented Aug 16, 2023

Crash Report

Mypy will crash when creating a TypedDict using keyword unpacking on a loaded JSON-encoded dictionary when also generating reports. In our case, it happens when --cobertura-xml-report option is used.

Traceback

Traceback (most recent call last):
  File "PATH/.nox/mypy-3-11/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "mypy/build.py", line 2069, in wrap_context
  File "mypy/build.py", line 2405, in finish_passes
  File "mypy/build.py", line 872, in report_file
  File "mypy/report.py", line 91, in file
  File "mypy/report.py", line 632, in on_file
  File "mypy/nodes.py", line 377, in accept
  File "mypy/stats.py", line 119, in visit_mypy_file
  File "mypy/traverser.py", line 114, in visit_mypy_file
  File "mypy/nodes.py", line 1309, in accept
  File "mypy/stats.py", line 218, in visit_assignment_stmt
  File "mypy/traverser.py", line 164, in visit_assignment_stmt
  File "mypy/nodes.py", line 1903, in accept
  File "mypy/stats.py", line 255, in visit_call_expr
  File "mypy/stats.py", line 264, in record_call_target_precision
  File "mypy/stats.py", line 278, in record_callable_target_precision
  File "mypy/argmap.py", line 130, in map_formals_to_actuals
  File "mypy/argmap.py", line 84, in map_actuals_to_formals
  File "mypy/stats.py", line 283, in <lambda>
KeyError: <mypy.nodes.CallExpr object at 0x1147d3400>
FILENAME: : note: use --pdb to drop into pdb
Generated Cobertura report: PATH/reports/cobertura.xml

To Reproduce

from typing import TypedDict
import json


class MyDict(TypedDict):
    a: int
    b: int


data = {"a": 1, "b": 2}
foo: MyDict = MyDict(**json.loads(json.dumps(data)))  # type: ignore[typeddict-item]
mypy . --no-namespace-packages --strict-equality --no-implicit-optional --show-error-codes --warn-redundant-casts --warn-unused-ignores --warn-unused-configs --strict-equality --extra-checks --check-untyped-defs --cobertura-xml-report=PATH_TO_REPORTS

Your Environment

  • Mypy version used: 1.5.0
  • Mypy command-line flags: --no-namespace-packages --strict-equality --no-implicit-optional --show-error-codes --warn-redundant-casts --warn-unused-ignores --warn-unused-configs --strict-equality --extra-checks --check-untyped-defs --cobertura-xml-report=PATH_TO_REPORTS
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11
  • Operating system and version: MacOS 11.7.3
@md384 md384 added the crash label Aug 16, 2023
ilevkivskyi added a commit that referenced this issue Sep 15, 2023
Fixes #16107
Fixes #15891

I only vaguely remember why I added those context managers, it seemed to
me giving full TypedDict as context may cause false positives. But since
the current way causes crashes, let's just not do this (we will see if
there will be actual false positives).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
@md384 @AlexWaygood and others