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

[html report] Error creating TypedDict from **kwargs #16107

Closed
james-emerton opened this issue Sep 13, 2023 · 4 comments · Fixed by #16116
Closed

[html report] Error creating TypedDict from **kwargs #16107

james-emerton opened this issue Sep 13, 2023 · 4 comments · Fixed by #16116

Comments

@james-emerton
Copy link

Crash Report

When initializing a TypedDict using **kwargs syntax and generating an --html-report, mypy crashes with the following traceback. Possibly related to #5382.

Traceback

(spire) PS C:\Users\James\dev\spire\spire-server> mypy --html-report=../../mypy_report --show-traceback mypy_test.py
mypy_test.py:11: error: Missing keys ("access_token", "token_type") for TypedDict "Token"  [typeddict-item]
mypy_test.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.7.0+dev.66fbf5b526ad8cfa127dd5cca68dcb2f770b1dd7
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\James\dev\spire\.venv\Scripts\mypy.exe\__main__.py", line 7, in <module>
    sys.exit(console_entry())
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\__main__.py", line 15, in console_entry
    main()
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\main.py", line 99, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\main.py", line 178, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\build.py", line 189, in build
    result = _build(
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\build.py", line 262, in _build
    graph = dispatch(sources, manager, stdout)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\build.py", line 2938, in dispatch
    process_graph(graph, manager)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\build.py", line 3336, in process_graph
    process_stale_scc(graph, scc, manager)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\build.py", line 3441, in process_stale_scc
    graph[id].finish_passes()
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\build.py", line 2364, in finish_passes
    with self.wrap_context():
  File "C:\Users\James\AppData\Local\Programs\Python\Python311-32\Lib\contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\build.py", line 2063, in wrap_context
    yield
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\build.py", line 2399, in finish_passes
    manager.report_file(self.tree, self.type_map(), self.options)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\build.py", line 866, in report_file
    self.reports.file(file, self.modules, type_map, options)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\report.py", line 91, in file
    reporter.on_file(tree, modules, type_map, options)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\report.py", line 506, in on_file
    tree.accept(visitor)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\nodes.py", line 366, in accept
    return visitor.visit_mypy_file(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\stats.py", line 119, in visit_mypy_file
    super().visit_mypy_file(o)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\traverser.py", line 114, in visit_mypy_file
    d.accept(self)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\nodes.py", line 776, in accept
    return visitor.visit_func_def(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\stats.py", line 171, in visit_func_def
    super().visit_func_def(o)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\traverser.py", line 133, in visit_func_def
    self.visit_func(o)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\traverser.py", line 130, in visit_func
    o.body.accept(self)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\nodes.py", line 1212, in accept
    return visitor.visit_block(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\traverser.py", line 118, in visit_block
    s.accept(self)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\nodes.py", line 1408, in accept
    return visitor.visit_return_stmt(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\traverser.py", line 187, in visit_return_stmt
    o.expr.accept(self)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\nodes.py", line 1893, in accept
    return visitor.visit_call_expr(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\stats.py", line 255, in visit_call_expr
    self.record_call_target_precision(o)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\stats.py", line 264, in record_call_target_precision
    self.record_callable_target_precision(o, callee_type)
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\stats.py", line 278, in record_callable_target_precision
    actual_to_formal = map_formals_to_actuals(
                       ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\argmap.py", line 130, in map_formals_to_actuals
    formal_to_actual = map_actuals_to_formals(
                       ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\argmap.py", line 84, in map_actuals_to_formals
    actualt = get_proper_type(actual_arg_type(ai))
                              ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\James\dev\spire\.venv\Lib\site-packages\mypy\stats.py", line 283, in <lambda>
    lambda n: typemap[o.args[n]],
              ~~~~~~~^^^^^^^^^^^
KeyError: <mypy.nodes.NameExpr object at 0x04826538>
mypy_test.py: : note: use --pdb to drop into pdb
Generated HTML report (via XSLT): C:\Users\James\dev\mypy_report\index.html

To Reproduce

Run mypy against the following minimal example with --html-report.

from typing import TypedDict


class Token(TypedDict):
    access_token: str
    token_type: str


def get_token(some_dict: dict):
    return Token(**some_dict)

Your Environment

  • Mypy version used: master (1.7.0+dev.66fbf5b526ad8cfa127dd5cca68dcb2f770b1dd7)
  • Mypy command-line flags: mypy --html-report=./mypy_report --show-traceback mypy_test.py
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.11.1
  • Operating system and version: Windows 10
@sobolevn
Copy link
Member

This patch solves this issue:

diff --git mypy/stats.py mypy/stats.py
index b8803e03b..5de59a3b2 100644
--- mypy/stats.py
+++ mypy/stats.py
@@ -280,7 +280,7 @@ class StatisticsVisitor(TraverserVisitor):
             o.arg_names,
             callee.arg_kinds,
             callee.arg_names,
-            lambda n: typemap[o.args[n]],
+            lambda n: callee.arg_types[n],
         )
         for formals in actual_to_formal:
             for n in formals:
                 

And produces:

» mypy --html-report=./mypy_report --show-traceback ex.py
ex.py:9: error: Missing key "a" for TypedDict "Token"  [typeddict-item]
Generated HTML report (via XSLT): /Users/sobolev/Desktop/mypy/mypy_report/index.html
Found 1 error in 1 file (checked 1 source file)
Снимок экрана 2023-09-14 в 10 53 52

But, I've never worked with reports and not sure that any of this is correct :)

@sobolevn sobolevn changed the title Error creating TypedDict from **kwargs [html report] Error creating TypedDict from **kwargs Sep 14, 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).
@sshishov
Copy link

sshishov commented Jul 3, 2024

Sorry guys for commenting on dead thread, but...

I have a qustion why all keys of typed dict are marked as Any? It completely ruins the coverage of codebase if we are using a lot of TypedDict

@sobolevn @ilevkivskyi

@ilevkivskyi
Copy link
Member

This is a known problem. And actually can cause crashes in certain scenarios. I am going to address it in coming weeks, you can track #10007. When working on that issue I will also add a coverage test case.

@sshishov
Copy link

sshishov commented Jul 3, 2024

Hi @ilevkivskyi , thank you for explanation. Unfortunately before receiving the feedback I have decided to create the Bug which can be found here: #17477

Feel free to handle it the way you think it will be proper from your point of view.

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.

5 participants