Mypy: Assertion error, importing library with typehints

Created on 3 Mar 2020  路  10Comments  路  Source: python/mypy

Note: if you are reporting a wrong signature of a function or a class in
the standard library, then the typeshed tracker is better suited
for this report: https://github.com/python/typeshed/issues

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request?
    Bug, assertionerror
  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

Library used https://github.com/Rapptz/discord.py/pull/1497.

Code

import discord

Running mypy --strict file.py crashes it but if you do normal run without strict and then strict it works until you do strict again. It seems doing strict off the bat without having ran without --strict previous crashes it.

Traceback

Traceback (most recent call last):
  File "C:\Python38\lib\runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Python38\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\stonedestroyer\bot\.venv\Scripts\mypy.exe\__main__.py", line 7, in <module>
  File "c:\stonedestroyer\bot\.venv\lib\site-packages\mypy\__main__.py", line 8, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "c:\stonedestroyer\bot\.venv\lib\site-packages\mypy\main.py", line 89, in main
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "c:\stonedestroyer\bot\.venv\lib\site-packages\mypy\build.py", line 180, in build
    result = _build(
  File "c:\stonedestroyer\bot\.venv\lib\site-packages\mypy\build.py", line 249, in _build
    graph = dispatch(sources, manager, stdout)
  File "c:\stonedestroyer\bot\.venv\lib\site-packages\mypy\build.py", line 2649, in dispatch
    process_graph(graph, manager)
  File "c:\stonedestroyer\bot\.venv\lib\site-packages\mypy\build.py", line 2956, in process_graph
    process_stale_scc(graph, scc, manager)
  File "c:\stonedestroyer\bot\.venv\lib\site-packages\mypy\build.py", line 3074, in process_stale_scc
    graph[id].write_cache()
  File "c:\stonedestroyer\bot\.venv\lib\site-packages\mypy\build.py", line 2266, in write_cache
    new_interface_hash, self.meta = write_cache(
  File "c:\stonedestroyer\bot\.venv\lib\site-packages\mypy\build.py", line 1441, in write_cache
    data = tree.serialize()
  File "c:\stonedestroyer\bot\.venv\lib\site-packages\mypy\nodes.py", line 302, in serialize
    'names': self.names.serialize(self._fullname),
  File "c:\stonedestroyer\bot\.venv\lib\site-packages\mypy\nodes.py", line 3097, in serialize
    data[key] = value.serialize(fullname, key)
  File "c:\stonedestroyer\bot\.venv\lib\site-packages\mypy\nodes.py", line 3033, in serialize
    assert not isinstance(self.node, PlaceholderNode)
AssertionError

Same issue with latest mypy from master.

crash priority-0-high

All 10 comments

Unfortunately I can't reproduce this, mypy just doesn't find the stubs (even though I installed from the right branch). It however looks like something bad (and it seems to me we had seen another similar report recently), so worth more investigation.

@ilevkivskyi let me know if you need help reproducing this. I'm the maintainer of the stubs and I'm experiencing the same issue on 0.770.

I can repro this crash, I needed to install aiohttp and websockets from requirements.txt for it to happen. It seems that the assertion failure happens for discord.channel.BaseUser. My guess is there a large import cycle in the channel/user pairing that is causing the issue, but I am not sure (I'm afraid I don't have more time to investigate this further).

I was able to fix this crash. webhook.pyi was importing discord.state.ConnectionState and this was causing the import cycles. I hadn't stubbed ConnectionState because it's not part of the public API, so I stubbed it as an opaque structure so Webhook.from_state() can use it. @Stonedestroyer, update your checkout to bryanforbes/discord.py@ea11c83675a0304139dc943388148b66310d4469 and let me know if the problem persists.

I did find an issue with an import cycle using from .channel import * in discord/__init__.pyi. I had to fix it by specifying exactly what to import from discord.channel (@Stonedestroyer you'll want to update your checkout to bryanforbes/discord.py@30fb1617eac467b1a82babbfedeb465daa6f5dfa ). This seems to have been introduced in 0.770.

@bryanforbes thank you for investigating this further! If you could find a more minimal reproduction that would be greatly appreciated.

@ethanhs I'll see what I can do

@ethanhs I was able to reproduce the import cycle in the following gist: https://gist.github.com/bryanforbes/024ad256677d07faff0ed4dc5c5dd21a. In my reproduction project, I have a repro directory and I put all of those files into it. I then run .venv/bin/mypy repro/user.pyi and get the crash. If I change __init__.pyi to import all of the classes in channel.pyi individually (from .channel import TextChannel as TextChannel, ...), the crash goes away.

@bryanforbes, thank you so much, that will be helpful in finding the source of the issue.

I am running into the same issue. I also suspect it is a kind of import cycle crash.

I have tried to reduce the example but by reducing it, the problem goes away. I also suspect some kind of import cycle because removing some module imports removes the problem.

You can easily reproduce it on my open source project :

d:\work\winpdb\winpdb1>mypy --strict rpdb\rpc.py
Traceback (most recent call last):
  File "c:\program files (x86)\python37-32\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\program files (x86)\python37-32\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Program Files (x86)\Python37-32\Scripts\mypy.exe\__main__.py", line 7, in <module>
  File "c:\program files (x86)\python37-32\lib\site-packages\mypy\__main__.py", line 8, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "c:\program files (x86)\python37-32\lib\site-packages\mypy\main.py", line 90, in main
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "c:\program files (x86)\python37-32\lib\site-packages\mypy\build.py", line 181, in build
    sources, options, alt_lib_path, flush_errors, fscache, stdout, stderr, extra_plugins
  File "c:\program files (x86)\python37-32\lib\site-packages\mypy\build.py", line 254, in _build
    graph = dispatch(sources, manager, stdout)
  File "c:\program files (x86)\python37-32\lib\site-packages\mypy\build.py", line 2630, in dispatch
    process_graph(graph, manager)
  File "c:\program files (x86)\python37-32\lib\site-packages\mypy\build.py", line 2953, in process_graph
    process_stale_scc(graph, scc, manager)
  File "c:\program files (x86)\python37-32\lib\site-packages\mypy\build.py", line 3070, in process_stale_scc
    graph[id].write_cache()
  File "c:\program files (x86)\python37-32\lib\site-packages\mypy\build.py", line 2236, in write_cache
    self.manager)
  File "c:\program files (x86)\python37-32\lib\site-packages\mypy\build.py", line 1445, in write_cache
    data = tree.serialize()
  File "c:\program files (x86)\python37-32\lib\site-packages\mypy\nodes.py", line 303, in serialize
    'names': self.names.serialize(self._fullname),
  File "c:\program files (x86)\python37-32\lib\site-packages\mypy\nodes.py", line 3098, in serialize
    data[key] = value.serialize(fullname, key)
  File "c:\program files (x86)\python37-32\lib\site-packages\mypy\nodes.py", line 3034, in serialize
    assert not isinstance(self.node, PlaceholderNode)
AssertionError
Was this page helpful?
0 / 5 - 0 ratings