Repository navigation
Assertion error, importing library with typehints #8481
Copy link
Copy link
Closed
Description
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 Rapptz/discord.py#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)
AssertionErrorSame issue with latest mypy from master.
Reactions are currently unavailable
Activity
[-][Crash][/-][+][Crash] Assertion error, importing library with typehints[/+]ilevkivskyi commentedon Mar 6, 2020
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.
[-][Crash] Assertion error, importing library with typehints[/-][+]Assertion error, importing library with typehints[/+]bryanforbes commentedon Mar 12, 2020
@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.
emmatyping commentedon Mar 13, 2020
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).bryanforbes commentedon Apr 4, 2020
I was able to fix this crash.
webhook.pyiwas importingdiscord.state.ConnectionStateand this was causing the import cycles. I hadn't stubbedConnectionStatebecause it's not part of the public API, so I stubbed it as an opaque structure soWebhook.from_state()can use it. @Stonedestroyer, update your checkout to bryanforbes/discord.py@ea11c83 and let me know if the problem persists.bryanforbes commentedon Apr 4, 2020
I did find an issue with an import cycle using
from .channel import *indiscord/__init__.pyi. I had to fix it by specifying exactly what to import fromdiscord.channel(@Stonedestroyer you'll want to update your checkout to bryanforbes/discord.py@30fb161 ). This seems to have been introduced in0.770.emmatyping commentedon Apr 5, 2020
@bryanforbes thank you for investigating this further! If you could find a more minimal reproduction that would be greatly appreciated.
bryanforbes commentedon Apr 5, 2020
@ethanhs I'll see what I can do
bryanforbes commentedon Apr 5, 2020
@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
reprodirectory and I put all of those files into it. I then run.venv/bin/mypy repro/user.pyiand get the crash. If I change__init__.pyito import all of the classes inchannel.pyiindividually (from .channel import TextChannel as TextChannel, ...), the crash goes away.emmatyping commentedon Apr 6, 2020
@bryanforbes, thank you so much, that will be helpful in finding the source of the issue.
bluebird75 commentedon Nov 22, 2020
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 :
rraval commentedon Dec 6, 2020
I managed to minimize the reproduction from https://github.com/bluebird75/winpdb, please see https://github.com/rraval/mypy-8481
This fails:
This seems to work:
Version info:
My naive guess is that this is some interaction between
--no-implicit-reexportandfrom pkg.a import *. Both seem to be necessary to make the assertion trigger occur.chore(github-actions): fix for python/mypy#8481
ngie-eign commentedon Oct 10, 2021
I just ran into this issue with a large $work project as well, and we're sort of stuck with an older version of mypy that supports 2.7 and 3.8:
Do folks have any suggestions for how the import cycle can be tracked down other than maybe use
--pdb(which wouldn't work with CI build processes)?PS. I know this isn't the last version that supports both; this is just the version I standardized things on 1.5 years ago when I added it to our development environment.
emmatyping commentedon Oct 11, 2021
@rraval thank you for the minimal reproduction, that is very helpful, I believe that the
from pkg.a import *does indeed seem to be related to the cause.@ngie-eign I would look for a patter such as an import cycle, where you do
from module import *, wheremoduleconditionally imports some other thing withif TYPE_CHECKING.ngie-eign commentedon Oct 11, 2021
@ethanhs : thanks! I think I figured out the issue — it was environmental (unclean build; old cached files).
Fix crash involving explicit reexport, import cycle, wildcard (#11632)
Fix crash involving explicit reexport, import cycle, wildcard (#11632)
Fix crash involving explicit reexport, import cycle, wildcard (python…